1 | <?php declare(strict_types=1); |
||
25 | class ExtractorDefinitionBuilder implements ExtractorDefinitionBuilderInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $type_regexp = '/ |
||
31 | ^ |
||
32 | ( |
||
33 | (?<name> |
||
34 | [-_\w]* |
||
35 | ) |
||
36 | (?: |
||
37 | \s* |
||
38 | (?<group> |
||
39 | \( |
||
40 | \s* |
||
41 | (?<param> |
||
42 | (?-4)* |
||
43 | | |
||
44 | [\w\\\\]+ |
||
45 | ) |
||
46 | \s* |
||
47 | \) |
||
48 | ) |
||
49 | )? |
||
50 | (?: |
||
51 | \s* |
||
52 | (?<arr>(?:\s*\[\s*\]\s*)+) |
||
53 | )? |
||
54 | (?: |
||
55 | \s* |
||
56 | \| |
||
57 | \s* |
||
58 | (?<alt>(?-6)) |
||
59 | )? |
||
60 | ) |
||
61 | $ |
||
62 | /xi'; |
||
63 | |||
64 | protected $type_regexp2 = '/ |
||
65 | ^ |
||
66 | (?:((\w+\b(?:\(.*\))?(?:\s*\[\s*\])?)(?:\s*\|\s*(?-1))*)) |
||
67 | | |
||
68 | (?:\(\s*(?-2)\s*\)(?:\s*\[\s*\])?) |
||
69 | | |
||
70 | (\[\s*\]) |
||
71 | $ |
||
72 | /xi'; |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 19 | public function build(string $definition): ExtractorDefinitionInterface |
|
78 | { |
||
79 | 19 | $definition = trim($definition); |
|
80 | |||
81 | 19 | if (!$definition) { |
|
82 | 1 | throw new ExtractorDefinitionBuilderException('Definition must be non-empty string'); |
|
83 | } |
||
84 | |||
85 | try { |
||
86 | 18 | if (preg_match($this->type_regexp, $definition, $matches)) { |
|
87 | 17 | $extractor = $this->buildExtractor($matches['name'], $matches['param'] ?? '', $matches['alt'] ?? '', $this->getDepth($matches), $this->hasGroups($matches)); |
|
88 | |||
89 | 16 | return $extractor; |
|
90 | } |
||
91 | 2 | } catch (ExtractorDefinitionBuilderException $e) { |
|
92 | // We don't care about what specific issue we hit inside, |
||
93 | // for API user it means that the definition is invalid |
||
94 | } |
||
95 | |||
96 | 3 | throw new ExtractorDefinitionBuilderException("Unable to parse definition: '{$definition}'"); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $name |
||
101 | * @param null|string $param |
||
102 | * @param null|string $alt_definitions |
||
103 | * @param int $depth |
||
104 | * @param bool $groups |
||
105 | * |
||
106 | * @return ExtractorDefinitionInterface |
||
107 | * @throws ExtractorDefinitionBuilderException |
||
108 | */ |
||
109 | 17 | protected function buildExtractor(string $name, string $param, string $alt_definitions, int $depth, bool $groups): ExtractorDefinitionInterface |
|
141 | |||
142 | /** |
||
143 | * @param PlainExtractorDefinitionInterface $definition |
||
144 | * @param string $alt_definitions |
||
145 | * |
||
146 | * @return VariableExtractorDefinition |
||
147 | * @throws ExtractorDefinitionBuilderException |
||
148 | */ |
||
149 | 6 | protected function buildVariableDefinition(PlainExtractorDefinitionInterface $definition, string $alt_definitions): VariableExtractorDefinition |
|
168 | |||
169 | /** |
||
170 | * @param null|ExtractorDefinitionInterface $definition |
||
171 | * @param int $depth |
||
172 | * |
||
173 | * @return ExtractorDefinitionInterface |
||
174 | */ |
||
175 | 5 | protected function buildArrayDefinition(?ExtractorDefinitionInterface $definition, int $depth): ExtractorDefinitionInterface |
|
185 | |||
186 | /** |
||
187 | * @param array $matches |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | 17 | private function getDepth(array $matches): int |
|
199 | |||
200 | 17 | private function hasGroups(array $matches): bool |
|
204 | } |
||
205 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.