Passed
Push — master ( e05dd3...01a0a7 )
by Nils
02:57
created

HealthFoundationFactory::initChecks()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 13
nop 2
dl 0
loc 36
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Config;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Decorator\Decorator;
7
use Leankoala\HealthFoundation\HealthFoundation;
8
use PhmLabs\Components\Init\Init;
9
10
class HealthFoundationFactory
11
{
12
    public static function from($configArray)
13
    {
14
        $healthFoundation = new HealthFoundation();
15
16
        if (!array_key_exists('checks', $configArray)) {
17
            throw new \RuntimeException('The mandatory config element "checks" is missing.');
18
        }
19
20
        $healthFoundation = self::initChecks($configArray, $healthFoundation);
21
22
        return $healthFoundation;
23
    }
24
25
    /**
26
     * @param array $configArray
27
     *
28
     * @return HealthFoundation
29
     */
30
    private static function initChecks($configArray, HealthFoundation $healthFoundation)
31
    {
32
        foreach ($configArray['checks'] as $key => $checkArray) {
33
34
            /** @var Check $check */
35
            $check = Init::initialize($checkArray, 'check');
0 ignored issues
show
Unused Code introduced by
The call to PhmLabs\Components\Init\Init::initialize() has too many arguments starting with 'check'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            /** @scrutinizer ignore-call */ 
36
            $check = Init::initialize($checkArray, 'check');

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.

Loading history...
36
37
            if (array_key_exists('description', $checkArray)) {
38
                $description = $checkArray['description'];
39
            } else {
40
                $description = "";
41
            }
42
43
            if (array_key_exists('identifier', $checkArray)) {
44
                $identifier = $checkArray['identifier'];
45
            } else {
46
                $identifier = $key;
47
            }
48
49
            if (array_key_exists('decorators', $checkArray)) {
50
                foreach ($checkArray['decorators'] as $decorator) {
51
                    $decorator = Init::initialize($decorator, 'decorator');
52
53
                    if ($decorator instanceof Decorator) {
54
                        $decorator->setCheck($check);
55
                        $check = $decorator;
56
                    } else {
57
                        throw new \RuntimeException('The given decorator must implement the decorator interface.');
58
                    }
59
                }
60
            }
61
62
            $healthFoundation->registerCheck($check, $identifier, $description);
63
        }
64
65
        return $healthFoundation;
66
    }
67
}
68