Cancelled
Branch master (244451)
by Tim
10:31
created

Module::getModuleName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
9
class Module extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /**
12
     * @var array
13
     */
14
    private $parsed;
15
16
    /**
17
     * @var array|null
18
     */
19
    private $available;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $module;
25
26
    /**
27
     * @param TestData $testData
28
     *
29
     * @return void
30
     */
31
    protected function initialize($testData)
32
    {
33
        $this->module = $testData->getInput('required');
0 ignored issues
show
Documentation Bug introduced by
It seems like $testData->getInput('required') can also be of type array. However, the property $module is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
34
        $this->available = $testData->getInput('available');
35
        $this->parsed = explode('|', $this->module);
0 ignored issues
show
Bug introduced by
It seems like $this->module can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        $this->parsed = explode('|', /** @scrutinizer ignore-type */ $this->module);
Loading history...
36
37
        parent::initialize($testData);
38
    }
39
40
    /**
41
     * @return void
42
     */
43
    public function invokeTest()
44
    {
45
        $testResult = new TestResult($this->getCategory(), $this->getModuleName());
46
47
        $state = State::ERROR;
48
        $available = $this->getAvailable();
49
50
        foreach ($this->parsed as $module) {
51
            if (in_array($module, $available)) {
52
                $state = State::OK;
53
                break 1;
54
            }
55
        }
56
57
        if ($state == State::OK) {
58
            $testResult->setMessage('Module loaded');
59
        } else {
60
            $testResult->setMessage('Module not loaded');
61
        }
62
63
        $testResult->setState($state);
64
        $this->setTestResult($testResult);
65
    }
66
67
    /**
68
     * @return array|null
69
     */
70
    private function getAvailable()
71
    {
72
        assert(is_array($this->available));
73
        return $this->available;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    private function getModule()
80
    {
81
        assert(is_string($this->module));
82
        return $this->module;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getModuleName()
89
    {
90
        $available = $this->available;
91
92
        foreach ($this->parsed as $module) {
93
            if (in_array($module, $available)) {
0 ignored issues
show
Bug introduced by
It seems like $available can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

93
            if (in_array($module, /** @scrutinizer ignore-type */ $available)) {
Loading history...
94
                return $module;
95
            }
96
        }
97
98
        return $this->getModule();
99
    }
100
}
101