1 | <?php |
||
31 | final class SpecificationFactory implements FactoryInterface |
||
32 | { |
||
33 | /** |
||
34 | * Creates a SpecificationInterface object based on the ignore and extension parameters. |
||
35 | * |
||
36 | * @var (\phpDocumentor\Path|string)[] $paths |
||
37 | * @var (\phpDocumentor\Path|string)[] $ignore |
||
38 | * @var string[] $extensions |
||
39 | */ |
||
40 | 4 | public function create(array $paths, array $ignore, array $extensions): SpecificationInterface |
|
41 | { |
||
42 | 4 | $pathSpec = null; |
|
43 | 4 | foreach ($paths as $path) { |
|
44 | 3 | if ($pathSpec === null) { |
|
45 | 3 | $pathSpec = $this->inPath((string) $path); |
|
46 | 3 | continue; |
|
47 | } |
||
48 | |||
49 | 1 | $pathSpec = $this->orSpec($this->inPath($path), $pathSpec); |
|
|
|||
50 | } |
||
51 | |||
52 | 4 | $ignoreSpec = null; |
|
53 | 4 | foreach ($ignore['paths'] ?? [] as $path) { |
|
54 | 2 | if ($ignoreSpec === null) { |
|
55 | 2 | $ignoreSpec = $this->inPath((string) $path); |
|
56 | 2 | continue; |
|
57 | } |
||
58 | |||
59 | 1 | $ignoreSpec = $this->orSpec($this->inPath((string) $path), $ignoreSpec); |
|
60 | } |
||
61 | |||
62 | 4 | if (($ignore['hidden'] ?? false) === true) { |
|
63 | 1 | $ignoreSpec = $ignoreSpec === null |
|
64 | 1 | ? new IsHidden() |
|
65 | 1 | : $this->orSpec(new IsHidden(), $ignoreSpec); |
|
66 | } |
||
67 | |||
68 | 4 | $result = new HasExtension($extensions); |
|
69 | 4 | if ($ignoreSpec !== null) { |
|
70 | 3 | $result = $this->andSpec($result, $this->notSpec($ignoreSpec)); |
|
71 | } |
||
72 | 4 | if ($pathSpec !== null) { |
|
73 | 3 | $result = $this->andSpec($pathSpec, $result); |
|
74 | } |
||
75 | |||
76 | 4 | return $result; |
|
77 | } |
||
78 | |||
79 | 4 | private function inPath(string $path): InPath |
|
83 | |||
84 | 2 | private function orSpec(SpecificationInterface $or, SpecificationInterface $spec): SpecificationInterface |
|
88 | |||
89 | 3 | private function notSpec(SpecificationInterface $ignoreSpec): SpecificationInterface |
|
93 | |||
94 | 4 | private function andSpec(SpecificationInterface $spec, SpecificationInterface $spec2): SpecificationInterface |
|
98 | } |
||
99 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.