1 | <?php |
||
2 | |||
3 | namespace Leankoala\HealthFoundation\Config; |
||
4 | |||
5 | use Leankoala\HealthFoundation\Check\Check; |
||
6 | use Leankoala\HealthFoundation\Filter\Filter; |
||
7 | use Leankoala\HealthFoundation\HealthFoundation; |
||
8 | use PhmLabs\Components\Init\Init; |
||
9 | |||
10 | class HealthFoundationFactory |
||
11 | { |
||
12 | public static function from($configArray) |
||
13 | { |
||
14 | if (!is_array($configArray)) { |
||
15 | throw new \RuntimeException('The given value is not an array.'); |
||
16 | } |
||
17 | |||
18 | $healthFoundation = new HealthFoundation(); |
||
19 | |||
20 | if (!array_key_exists('checks', $configArray)) { |
||
21 | throw new \RuntimeException('The mandatory config element "checks" is missing.'); |
||
22 | } |
||
23 | |||
24 | $healthFoundation = self::initChecks($configArray, $healthFoundation); |
||
25 | |||
26 | return $healthFoundation; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param array $configArray |
||
31 | * |
||
32 | * @return HealthFoundation |
||
33 | */ |
||
34 | private static function initChecks($configArray, HealthFoundation $healthFoundation) |
||
35 | { |
||
36 | foreach ($configArray['checks'] as $key => $checkArray) { |
||
37 | |||
38 | /** @var Check $check */ |
||
39 | $check = Init::initialize($checkArray, 'check'); |
||
0 ignored issues
–
show
|
|||
40 | |||
41 | if (array_key_exists('description', $checkArray)) { |
||
42 | $description = $checkArray['description']; |
||
43 | } else { |
||
44 | $description = ""; |
||
45 | } |
||
46 | |||
47 | if (array_key_exists('identifier', $checkArray)) { |
||
48 | $identifier = $checkArray['identifier']; |
||
49 | } else { |
||
50 | $identifier = $key; |
||
51 | } |
||
52 | |||
53 | if (array_key_exists('filter', $checkArray)) { |
||
54 | foreach ($checkArray['filter'] as $decorator) { |
||
55 | $decorator = Init::initialize($decorator, 'filter'); |
||
56 | |||
57 | if ($decorator instanceof Filter) { |
||
58 | $decorator->setCheck($check); |
||
59 | $check = $decorator; |
||
60 | } else { |
||
61 | throw new \RuntimeException('The given decorator must implement the decorator interface.'); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | $healthFoundation->registerCheck($check, $identifier, $description); |
||
67 | } |
||
68 | |||
69 | return $healthFoundation; |
||
70 | } |
||
71 | } |
||
72 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.