Passed
Push — master ( 99e070...420f80 )
by Tim
01:30
created

ModuleSet::invokeTest()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 2
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\Modules;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
use \SimpleSAML\Module\monitor\TestResult as TestResult;
9
10
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var array
14
     */
15
    private $required;
16
17
    /**
18
     * @var array
19
     */
20
    private $available;
21
 
22
    /**
23
     * @var array
24
     */
25
    private $dependencies;
26
27
    /**
28
     * @var string
29
     */
30
    private $type;
31
32
    /**
33
     * @param TestData $testData
34
     *
35
     * @return void
36
     */
37
    protected function initialize($testData)
38
    {
39
        $this->setRequired($testData->getInputItem('required'));
40
        $this->setAvailable($testData->getInputItem('available'));
41
        $this->setDependencies($testData->getInputItem('dependencies'));
42
        $this->setType($testData->getInputItem('type'));
43
        $this->setCategory($this->type.' modules');
44
    }
45
46
    /**
47
     * @param array
48
     */
49
    private function setRequired($required)
50
    {
51
        assert(is_array($required));
52
        $this->required = $required;
53
    }
54
55
56
    /**
57
     * @param array
58
     */
59
    private function setAvailable($available)
60
    {
61
        assert(is_array($available));
62
        $this->available = $available;
63
    }
64
65
66
    /**
67
     * @param array
68
     */
69
    private function setDependencies($dependencies)
70
    {
71
        assert(is_array($dependencies));
72
        $this->dependencies = $dependencies;
73
    }
74
75
76
    /**
77
     * @param string
78
     */
79
    private function setType($type)
80
    {
81
        assert(is_string($type));
82
        $this->type = $type;
83
    }
84
85
86
    /**
87
     * @return void
88
     */
89
    public function invokeTest()
90
    {
91
        if (empty($this->available)) {
92
            $state = State::SKIPPED;
0 ignored issues
show
Unused Code introduced by
The assignment to $state is dead and can be removed.
Loading history...
93
        } else {
94
            foreach ($this->required as $module) {
95
                $testData = new TestData([
96
                    'required' => $module,
97
                    'available' => $this->available,
98
                    'type' => $this->type,
99
                ]);
100
101
                $moduleTest = new TestCase\Module($testData);
102
                $moduleTestResult = $moduleTest->getTestResult();
103
                if ($moduleTestResult->getState() !== State::OK) {
104
                    $missing = $this->findMissingDependencies($module);
105
                    if (!empty($missing)) {
106
                        $moduleTestResult->setSubject($moduleTest->getModuleName());
107
                        $moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing));
108
                    }
109
                }
110
                $this->addTestResult($moduleTestResult);
111
            }
112
            $state = $this->calculateState();
113
        }
114
115
        $testResult = new TestResult($this->type, implode(', ', $this->required));
116
        $this->setTestResult($testResult);
0 ignored issues
show
Bug introduced by
The call to SimpleSAML\Module\monito...uleSet::setTestResult() has too few arguments starting with state. ( Ignorable by Annotation )

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

116
        $this->/** @scrutinizer ignore-call */ 
117
               setTestResult($testResult);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
117
    }
118
119
    /**
120
     * @param TestResult $testResult
121
     * @param State $state
122
     * return void
123
     */
124
    protected function setTestResult($testResult, $state)
125
    {
126
        if ($state === State::OK) {
0 ignored issues
show
introduced by
The condition $state === SimpleSAML\Module\monitor\State::OK is always false.
Loading history...
127
            $testResult->setMessage('All required modules are loaded');
128
        } elseif ($state === State::SKIPPED) {
129
            $testResult->setMessage('Unable to verify installed modules');
130
        } else {
131
            $testResult->setMessage('Not all required modules are loaded');
132
        }
133
        $testResult->setState($state);
0 ignored issues
show
Bug introduced by
$state of type SimpleSAML\Module\monitor\State is incompatible with the type integer expected by parameter $state of SimpleSAML\Module\monitor\TestResult::setState(). ( Ignorable by Annotation )

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

133
        $testResult->setState(/** @scrutinizer ignore-type */ $state);
Loading history...
134
        parent::setTestResult($testResult);
135
    }
136
137
    /**
138
     * @param string $module
139
     * @return array
140
     */
141
    private function findMissingDependencies($module)
142
    {
143
        $dependencies = $this->dependencies;
144
        $missing = array();
145
        while ($dependency = array_search($module, $dependencies)) {
146
            if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
147
                $missing[] = $dependency;
148
            }
149
            unset($dependencies[$dependency]);
150
        }
151
        return $missing;
152
    }
153
}
154