Completed
Push — master ( 90cd0d...49c547 )
by Tim
01:35
created

ModuleSet::setRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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
9
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
10
{
11
    /**
12
     * @var array
13
     */
14
    private $required;
15
16
    /**
17
     * @var array
18
     */
19
    private $available;
20
 
21
    /**
22
     * @var array
23
     */
24
    private $dependencies;
25
26
    /**
27
     * @var string
28
     */
29
    private $type;
30
31
    /**
32
     * @var TestCase
33
     */
34
    private $testCase;
35
36
    /**
37
     * @param TestData $testData
38
     *
39
     * @return void
40
     */
41
    protected function initialize($testData)
42
    {
43
        $this->setRequired($testData->getInput('required'));
44
        $this->setAvailable($testData->getInput('available'));
45
        $this->setDependencies($testData->getInput('dependencies'));
46
        $this->setType($testData->getInput('type'));
47
        $this->setTestCase($testData->getInput('testCase'));
48
    }
49
50
    /**
51
     * @param array
52
     */
53
    private function setRequired($required)
54
    {
55
        assert(is_array($required));
56
        $this->required = $required;
57
    }
58
59
60
    /**
61
     * @param array
62
     */
63
    private function setAvailable($available)
64
    {
65
        assert(is_array($available));
66
        $this->available = $available;
67
    }
68
69
70
    /**
71
     * @param array
72
     */
73
    private function setDependencies($dependencies)
74
    {
75
        assert(is_array($dependencies));
76
        $this->dependencies = $dependencies;
77
    }
78
79
80
    /**
81
     * @param string
82
     */
83
    private function setType($type)
84
    {
85
        assert(is_string($type));
86
        $this->type = $type;
87
    }
88
89
90
    /**
91
     * @param TestCase
92
     */
93
    private function setTestCase($testCase)
94
    {
95
        assert($testCase instanceof TestCase);
0 ignored issues
show
Bug introduced by
The class SimpleSAML\Module\monitor\TestCase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
96
        $this->testCase = $testCase;
97
    }
98
99
    
100
    /**
101
     * @return void
102
     */
103
    protected function invokeTestSuite()
104
    {
105
        $testResult = new TestResult($this->type, implode(', ', $this->required));
106
107
        if (empty($this->available)) {
108
            $testResult->setState(State::SKIPPED);
109
        } else {
110
            foreach ($this->required as $module) {
111
                $testData = new TestData([
112
                    'required' => $module,
113
                    'available' => $this->available
114
                ]);
115
        
116
                $moduleTest = new $this->testCase($this, $testData);
117
                $moduleTestResult = $moduleTest->getTestResult();
118
                if ($moduleTestResult->getState() !== State::OK) {
119
                    $missing = array();
120
                    while ($dependency = array_search($this->required, $this->dependencies)) {
121
                        if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
122
                            $missing[] = $dependency;
123
                        }
124
                        unset($dependencies[$dependency]);
125
                    }
126
                    if (!empty($missing)) {
127
                        $moduleTestResult->setSubject($moduleTest->getModuleName());
128
                        $moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing));
129
                    }
130
                    $this->addTestResult($moduleTestResult);
131
                }
132
            }
133
            $this->calculateState();
134
        }
135
136
        $state = $this->getState();
137 View Code Duplication
        if ($state === State::OK) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
            $testResult->setMessage('All required modules are loaded');
139
        } elseif ($state === State::SKIPPED) {
140
            $testResult->setMessage('Unable to verify installed modules');
141
        } else {
142
            $testResult->setMessage('Not all required modules are loaded');
143
        }
144
        $this->setTestResult($testResult);
145
    }
146
}
147