Completed
Push — master ( 719ca2...90cd0d )
by Tim
01:51
created

Modules::getAvailableApacheModules()   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 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\State as State;
7
use \SimpleSAML\Module\monitor\TestCase as TestCase;
8
use \SimpleSAML\Module\monitor\TestData as TestData;
9
10
class Modules extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var array
14
     */
15
    private $requiredApacheModules = array();
16
17
    /**
18
     * @var array
19
     */
20
    // Important!!  Modules-names are handled case-sensitive!!
21
    private $storeApacheDependencies = array();
0 ignored issues
show
Unused Code introduced by
The property $storeApacheDependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /**
24
     * @var array
25
     */
26
    private $moduleApacheDependencies = array(
27
        'negotiateext' => 'mod_auth_kerb|mod_auth_gssapi'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private $requiredPhpModules = array();
34
35
    /**
36
     * @var array
37
     */
38
    private $storePhpDependencies = array(
39
        'memcache' => 'memcached|memcache',
40
        'phpsession' => 'session',
41
        'redis' => 'redis',
42
        'redissentinel' => 'redis',
43
        'riak:Store' => 'riak',
44
        'sql' => 'PDO'
45
    );
46
47
    /**
48
     * @var array
49
     */
50
    private $modulePhpDependencies = array(
51
        'authfacebook' => array('curl', 'json'),
52
        'authYubiKey' => 'curl',
53
// TODO: consent only requires pdo when database backend is used.. Should probably add this to required-list when processing metadata
54
//        'consent' => 'PDO',
55
        'consentAdmin' => 'PDO',
56
        'ldap' => 'ldap',
57
        'memcacheMonitor' => 'memcached|memcache',
58
        'negotiate' => 'krb5',
59
        'radius' => 'radius',
60
        'riak' => 'riak',
61
        'sqlauth' => 'PDO'
62
    );
63
64
    /**
65
     * @param TestData|null $testData
66
     *
67
     * @return void
68
     */
69
    protected function initialize($testData = null)
70
    {
71
        $this->setRequiredApacheModules();
0 ignored issues
show
Bug introduced by
The method setRequiredApacheModules() does not seem to exist on object<SimpleSAML\Module...itor\TestSuite\Modules>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        $this->setRequiredPhpModules();
73
        $this->setRequiredSspModules();
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    private function addRequiredPhpModule($module)
80
    {
81
        if (!in_array($module, $this->requiredPhpModules)) {
82
            $this->requiredPhpModules[] = $module;
83
        }
84
    }
85
86
87
    /**
88
     * @return void
89
     */
90
    private function setRequiredPhpModules()
91
    {
92
        // PHP modules required
93
        $composerFile = \SimpleSAML\Utils\System::resolvePath('composer.json');
94
        $composerData = file_get_contents($composerFile);
95
        $composer = json_decode($composerData, true);
96
        $composerRequired = $composer['require'];
97
98
        foreach ($composerRequired as $ext => $ver) {
99
            if (preg_match('/^ext-/', $ext)) {
100
                $this->addRequiredPhpModule(substr($ext, 4));
101
            }
102
        }
103
104
        // Determine extra required modules
105
        $configuration = $this->getConfiguration();
106
        $globalConfig = $configuration->getGlobalConfig();
107
        $store = $globalConfig->getValue('store.type');
108
        if (array_key_exists($store, $this->storePhpDependencies)) {
109
            $this->addRequiredPhpModule($this->storePhpDependencies[$store]);
110
        }
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    private function setRequiredSspModules()
117
    {
118
        $modules = \SimpleSAML\Module::getModules();
119
        foreach ($modules as $module) {
120
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
121
                if (array_key_exists($module, $this->moduleApacheDependencies)) {
122
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->moduleApacheDependencies[$module]);
123
                    foreach ($dependencies as $dependency) {
124
                        $this->addRequiredApacheModule($dependency);
0 ignored issues
show
Bug introduced by
The method addRequiredApacheModule() does not seem to exist on object<SimpleSAML\Module...itor\TestSuite\Modules>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
                    }
126
                }
127
                if (array_key_exists($module, $this->modulePhpDependencies)) {
128
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->modulePhpDependencies[$module]);
129
                    foreach ($dependencies as $dependency) {
130
                        $this->addRequiredPhpModule($dependency);
131
                    }
132
                }
133
            }
134
        }
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getAvailableApacheModules()
141
    {
142
        $configuration = $this->getConfiguration();
143
        return $configuration->getAvailableApacheModules();
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getAvailablePhpModules()
150
    {
151
        $configuration = $this->getConfiguration();
152
        return $configuration->getAvailablePhpModules();
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    private function getRequiredApacheModules()
159
    {
160
        return $this->requiredApacheModules;
161
    }
162
163
164
    /**
165
     * @return array
166
     */
167
    private function getRequiredPhpModules()
168
    {
169
        return $this->requiredPhpModules;
170
    }
171
172
173
    /**
174
     * @return array<string,array>
175
     */
176
    private function getModuleDependencies()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
177
    {
178
        return ['Apache' => $this->moduleApacheDependencies, 'Php' => $this->modulePhpDependencies];
179
    }
180
181
    /**
182
     * @return void
183
     */
184
    protected function invokeTestSuite()
185
    {
186
        $configuration = $this->getConfiguration();
187
188
        // Test Apache modules
189
        $testData = new TestData([
190
            'available' => $configuration->getAvailableApacheModules(),
191
            'required' => $this->getRequiredApacheModules(),
192
            'dependencies' => $this->moduleApacheDependencies,
193
            'type' => 'Apache',
194
            'testClass' => TestCase\Module\Apache,
195
        ]);
196
        $apacheTest = new Modules\ModuleSet($configuration, $testData);
197
        $apacheTestResult = $apacheTest->getTestResult();
198
199
        // Test Php modules
200
        $testData = new TestData([
201
            'available' => $configuration->getAvailablePhpModules(),
202
            'required' => $this->getRequiredPhpModules(),
203
            'dependencies' => $this->modulePhpDependencies,
204
            'type' => 'Php',
205
            'testClass' => TestCase\Module\Php,
206
        ]);
207
        $phpTest = new Modules\ModuleSet($configuration, $testData);
208
        $phpTestResult = $phpTest->getTestResult();
209
210
        // TODO:  Sort out code below
211
        $this->processTestResults($apacheTestResult, $phpTestResult);
0 ignored issues
show
Documentation introduced by
$apacheTestResult is of type object<SimpleSAML\Module\monitor\TestResult>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Modules::processTestResults() has too many arguments starting with $phpTestResult.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
212
213
        $this->calculateState();
214
    }
215
216
    /**
217
     * @param array $output
218
     *
219
     * @return void
220
     */
221
    protected function processTestResults($output)
222
    {
223
        $availableModules = $this->getAvailableModules();
0 ignored issues
show
Bug introduced by
The method getAvailableModules() does not exist on SimpleSAML\Module\monitor\TestSuite\Modules. Did you maybe mean getAvailablePhpModules()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
224
        $tests = $this->getTests();
225
226
        foreach ($availableModules as $category => $available) {
227
            $categories = array_fill(0, count($tests), $category);
228
            if (!isSet($output[$category])) {
229
                $modules = array_map(
230
                  function($test, $category) {
231
                    return ($test->getCategory() === $category) ? $test->getModuleName() : false;
232
                  }, $tests, $categories
233
                );
234
                $modules = array_diff($modules, array(false));
235
                $output[$category][] = array(State::OK, $category, implode(', ', $modules), "All required modules are loaded");
236
            }
237
            $this->addMessages($output[$category], $category);
238
        }
239
    }
240
}
241