-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDisallowedNamespaceFactory.php
More file actions
74 lines (63 loc) · 2.7 KB
/
Copy pathDisallowedNamespaceFactory.php
File metadata and controls
74 lines (63 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types = 1);
namespace Spaze\PHPStan\Rules\Disallowed;
use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;
class DisallowedNamespaceFactory
{
private Formatter $formatter;
private Normalizer $normalizer;
private AllowedConfigFactory $allowedConfigFactory;
public function __construct(Formatter $formatter, Normalizer $normalizer, AllowedConfigFactory $allowedConfigFactory)
{
$this->formatter = $formatter;
$this->normalizer = $normalizer;
$this->allowedConfigFactory = $allowedConfigFactory;
}
/**
* @param array<array{namespace?:string|list<string>, class?:string|list<string>, exclude?:string|list<string>, excludeWithAttribute?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, allowInUse?:bool, errorIdentifier?:string, errorTip?:string|list<string>}> $config
* @return list<DisallowedNamespace>
* @throws ShouldNotHappenException
*/
public function createFromConfig(array $config): array
{
$disallowedNamespaces = [];
foreach ($config as $disallowed) {
$namespaces = $disallowed['namespace'] ?? $disallowed['class'] ?? null;
unset($disallowed['namespace'], $disallowed['class']);
if (!$namespaces) {
throw new ShouldNotHappenException("Either 'namespace' or 'class' must be set in configuration items");
}
$excludes = [];
foreach ((array)($disallowed['exclude'] ?? []) as $exclude) {
$excludes[] = $this->normalizer->normalizeNamespace($exclude);
}
$excludeWithAttributes = [];
foreach ((array)($disallowed['excludeWithAttribute'] ?? []) as $excludeWithAttribute) {
$excludeWithAttributes[] = $this->normalizer->normalizeNamespace($excludeWithAttribute);
}
$namespaces = (array)$namespaces;
try {
foreach ($namespaces as $namespace) {
$disallowedNamespace = new DisallowedNamespace(
$this->normalizer->normalizeNamespace($namespace),
$excludes,
$excludeWithAttributes,
$disallowed['message'] ?? null,
$this->allowedConfigFactory->getConfig($disallowed),
$disallowed['allowInUse'] ?? false,
$disallowed['errorIdentifier'] ?? null,
$disallowed['errorTip'] ?? []
);
$disallowedNamespaces[$disallowedNamespace->getNamespace()] = $disallowedNamespace;
}
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($namespaces), $e->getMessage()));
}
}
return array_values($disallowedNamespaces);
}
}