Completed
Push — master ( aa0be4...66a6c7 )
by Tim
02:45
created

Remote::invokeTest()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 32
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 45
rs 8.8571
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\Cert;
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
// We're cheating here, because this TestCase doesn't decent from Cert, like Data and File do.
11
final class Remote extends \SimpleSAML\Module\monitor\TestCaseFactory
12
{
13
    /*
14
     * @var string|null
15
     */
16
    private $connectString = null;
17
18
    /*
19
     * @var resource|null
20
     */
21
    private $context = null;
22
23
    /*
24
     * @param TestData $testData
25
     */
26
    protected function initialize($testData)
27
    {
28
        $hostname = $testData->getInput('hostname');
29
        $port = $testData->getInput('port');
30
        $this->setCategory($testData->getInput('category'));
31
        $this->connectString = 'ssl://' . $hostname . ':' . $port;
32
        $this->context = stream_context_create([
33
            "ssl" => array(
34
                "capture_peer_cert" => true,
35
                "verify_peer" => false,
36
                "verify_peer_name" => false
37
            )
38
        ]);
39
40
        parent::initialize($testData);
41
    }
42
43
    protected function invokeTest()
44
    {
45
        $testsuite = $this->getTestSuite();
46
        $testResult = new TestResult();
47
48
        $testData = new TestData([
49
            'uri' => $this->connectString,
50
            'context' => $this->context
51
        ]);
52
        $connTest = new TestCase\Network\ConnectUri(
53
            $testsuite,
54
            $testData
55
        );
56
        $connTestResult = $connTest->getTestResult();
57
58
        $connState = $testResult->getState();
59
60
        if ($connState === State::OK) { // Connection OK
61
            $connection = $connTestResult->getOutput('connection');
0 ignored issues
show
Unused Code introduced by
$connection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
            $certData = $connTestResult->getOutput('certData');
63
64
            if ($certData !== null) {
65
                $testData = new TestData([
66
                    'certData' => $certData,
67
                    'category' => $this->getCategory()
68
                ]);
69
                
70
                $certTest = new TestCase\Cert\Data(
71
                    $testsuite,
72
                    $testData
73
                );
74
                $certTestResult = $certTest->getTestResult();
75
                $testResult->setState($certTestResult->getState());
76
                $testResult->setMessage($certTestResult->getMessage());
77
            } else {
78
                $testResult->setState(State::SKIPPED);
79
                $testResult->setMessage(State::SKIPPED, $this->getCategory(), $this->getSubject(), 'Unable to capture peer certificate');
0 ignored issues
show
Unused Code introduced by
The call to TestResult::setMessage() has too many arguments starting with $this->getCategory().

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...
80
            }
81
        } else {
82
            $testResult->setState(State::FATAL);
83
            $testResult->setMessage($connTestResult->getMessage());
84
        }
85
        $this->setTestResult($testResult);
86
        unset($connTestResult, $certTestResult);
87
    }
88
}
89