1 | <?php |
||
18 | class Extractor extends Model |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Max number of rows to parse |
||
23 | */ |
||
24 | const MAX_LINES = 5000; |
||
25 | |||
26 | /** |
||
27 | * Max columns in a single line to parse |
||
28 | */ |
||
29 | const MAX_COLS = 500; |
||
30 | |||
31 | /** |
||
32 | * Pattern to extract hook arguments |
||
33 | */ |
||
34 | const PATTERN_HOOK = '/->fire\s*\(\s*(.+?)\s*\)/s'; |
||
35 | |||
36 | /** |
||
37 | * Pattern to extract method names |
||
38 | */ |
||
39 | const PATTERN_FUNCTION = '/function\s+(\w+)\s*\(/'; |
||
40 | |||
41 | /** |
||
42 | * Pattern to extract class namespaces |
||
43 | */ |
||
44 | const PATTERN_NAMESPACE = '/^namespace\s+(.+?)\s*;/'; |
||
45 | |||
46 | /** |
||
47 | * Pattern to extract class names |
||
48 | */ |
||
49 | const PATTERN_CLASS = '/(?:^| )class\s+(.+?)(\s+|\n\r|$)/'; |
||
50 | |||
51 | /** |
||
52 | * The current method while parsing hooks |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $current_function; |
||
56 | |||
57 | /** |
||
58 | * The current class namespace while parsing hooks |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $current_namespace; |
||
62 | |||
63 | /** |
||
64 | * The current class while parsing hooks |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $current_class; |
||
68 | |||
69 | /** |
||
70 | * Language model instance |
||
71 | * @var \gplcart\core\models\Language $language |
||
72 | */ |
||
73 | protected $language; |
||
74 | |||
75 | /** |
||
76 | * Constructor |
||
77 | * @param LanguageModel $language |
||
78 | */ |
||
79 | public function __construct(LanguageModel $language) |
||
85 | |||
86 | /** |
||
87 | * Returns an array of hook scopes/types |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getHookScopes() |
||
146 | |||
147 | /** |
||
148 | * Prepares extracted hook data |
||
149 | * @param array $data |
||
150 | * @return array |
||
151 | */ |
||
152 | public function prepareHook(array $data) |
||
176 | |||
177 | /** |
||
178 | * Returns an array of prepared hook arguments |
||
179 | * @param array $arguments |
||
180 | * @param array $data |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function prepareHookArguments(array $arguments, array $data) |
||
201 | |||
202 | /** |
||
203 | * Returns an array of extracted hooks |
||
204 | * @param array $options |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getHooks(array $options) |
||
208 | { |
||
209 | $scanned = $this->scan($options['directory']); |
||
210 | |||
211 | // Pager |
||
212 | if (!empty($options['limit'])) { |
||
213 | list($start, $length) = $options['limit']; |
||
214 | $scanned = array_slice($scanned, $start, $length, true); |
||
215 | } |
||
216 | |||
217 | if (empty($scanned)) { |
||
218 | return array(); |
||
219 | } |
||
220 | |||
221 | $success = $errors = array(); |
||
222 | |||
223 | foreach ($scanned as $file) { |
||
|
|||
224 | foreach ($this->parse($file) as $extracted) { |
||
225 | |||
226 | $extracted['file'] = gplcart_relative_path($file); |
||
227 | $prepared = $this->prepareHook($extracted); |
||
228 | |||
229 | // Filter by scopes |
||
230 | if (!empty($options['scopes']) && !$this->inScope($prepared['hook']['name'], $options['scopes'])) { |
||
231 | continue; |
||
232 | } |
||
233 | |||
234 | // Test extracted class and method |
||
235 | if (!method_exists($prepared['namespaced_class'], $prepared['function'])) { |
||
236 | trigger_error("Extracted method {$prepared['namespaced_class']}::{$prepared['function']} does not exist"); |
||
237 | $errors[$prepared['hook']['name']] = $prepared; |
||
238 | continue; |
||
239 | } |
||
240 | |||
241 | $success[$prepared['hook']['name']] = $prepared; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return array('success' => $success, 'errors' => $errors, 'files' => $scanned); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Whether a given hook name is in array of scopes |
||
250 | * @param string $hook |
||
251 | * @param array $scopes |
||
252 | * @return boolean |
||
253 | */ |
||
254 | protected function inScope($hook, array $scopes) |
||
264 | |||
265 | /** |
||
266 | * Returns an array of extracted hooks from a single file |
||
267 | * @param string $file |
||
268 | * @return array |
||
269 | */ |
||
270 | public function parse($file) |
||
330 | |||
331 | /** |
||
332 | * Returns an array of scanned files to extract hooks from or counts extracted items |
||
333 | * @param string $directory |
||
334 | * @return integer|array |
||
335 | */ |
||
336 | public function scan($directory, $count = false) |
||
349 | |||
350 | } |
||
351 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.