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

Bind   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 9 1
B invokeTest() 0 22 4
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap;
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
final class Bind extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /*
12
     * @var \SimpleSAML_Auth_LDAP|null
13
     */
14
    private $connection = null;
15
16
    /*
17
     * @var string|null
18
     */
19
    private $username = null;
20
21
    /*
22
     * @var string|null
23
     */
24
25
    private $password = null;
26
27
28
    /*
29
     * @param TestData $testData
30
     *
31
     * @return void
32
     */
33
    protected function initialize($testData)
34
    {
35
        $this->connection = $testData->getInput('connection');
0 ignored issues
show
Documentation Bug introduced by
It seems like $testData->getInput('connection') can also be of type array. However, the property $connection is declared as type null|SimpleSAML_Auth_LDAP. 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...
36
        $authSourceData = $testData->getInput('authSourceData');
37
38
        $this->username = $authSourceData['search.username'];
39
        $this->password = $authSourceData['search.password'];
40
41
        parent::initialize($testData);
42
    }
43
44
   
45
    /*
46
     * @return void
47
     */
48
    public function invokeTest()
49
    {
50
        try {
51
            $bind = $this->connection->bind($this->username, $this->password);
0 ignored issues
show
Bug introduced by
The method bind() does not exist on null. ( Ignorable by Annotation )

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

51
            /** @scrutinizer ignore-call */ 
52
            $bind = $this->connection->bind($this->username, $this->password);

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...
52
        } catch (\Exception $error) {
53
            // Fallthru
54
        }
55
56
        $testResult = new TestResult('LDAP Bind', $this->username);
57
        if (isSet($error)) {
58
            $msg = str_replace('Library - LDAP bind(): ', '', $error->getMessage());
59
            $testResult->setState(State::FATAL);
60
        } elseif ($bind === true) {
61
            $msg = 'Bind succesful';
62
            $testResult->setState(State::OK);
63
        } else {
64
            $msg = ldap_error($this->connection).' ('.ldap_errno($this->connection).')';
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type SimpleSAML_Auth_LDAP; however, parameter $link_identifier of ldap_error() does only seem to accept resource, 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

64
            $msg = ldap_error(/** @scrutinizer ignore-type */ $this->connection).' ('.ldap_errno($this->connection).')';
Loading history...
Bug introduced by
It seems like $this->connection can also be of type SimpleSAML_Auth_LDAP; however, parameter $link_identifier of ldap_errno() does only seem to accept resource, 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

64
            $msg = ldap_error($this->connection).' ('.ldap_errno(/** @scrutinizer ignore-type */ $this->connection).')';
Loading history...
65
            $testResult->setState(State::ERROR);
66
        }
67
68
        $testResult->setMessage($msg);
69
        $this->setTestResult($testResult);
70
    }
71
}
72