Passed
Push — master ( 7f6baf...50f068 )
by Satoshi
02:16
created

Component::checkPatterns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace DependencyAnalyzer\Inspector\RuleViolationDetector;
5
6
use DependencyAnalyzer\Patterns\QualifiedNamePattern;
7
8
class Component
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $name;
14
15
    /**
16
     * @var QualifiedNamePattern
17
     */
18
    protected $pattern;
19
20
    /**
21
     * @var array|QualifiedNamePattern[]
22
     */
23
    protected $dependerPatterns;
24
25
    /**
26
     * @var array|QualifiedNamePattern[]
27
     */
28
    protected $dependeePatterns;
29
30
    /**
31
     * Component constructor.
32
     * @param string $name
33
     * @param QualifiedNamePattern $pattern
34
     * @param QualifiedNamePattern $dependerPatterns
35
     * @param QualifiedNamePattern $dependeePatterns
36
     */
37
    public function __construct(string $name, QualifiedNamePattern $pattern, QualifiedNamePattern $dependerPatterns = null, QualifiedNamePattern $dependeePatterns = null)
38
    {
39
        $this->name = $name;
40
        $this->pattern = $pattern;
41
        $this->dependerPatterns = $dependerPatterns;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dependerPatterns can also be of type DependencyAnalyzer\Patterns\QualifiedNamePattern. However, the property $dependerPatterns is declared as type DependencyAnalyzer\Patte...fiedNamePattern[]|array. 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...
42
        $this->dependeePatterns = $dependeePatterns;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dependeePatterns can also be of type DependencyAnalyzer\Patterns\QualifiedNamePattern. However, the property $dependeePatterns is declared as type DependencyAnalyzer\Patte...fiedNamePattern[]|array. 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...
43
    }
44
45
    public function getName(): string
46
    {
47
        return $this->name;
48
    }
49
50
    public function isBelongedTo(string $className): bool
51
    {
52
        return $this->pattern->isMatch($className);
53
    }
54
55
    public function verifyDepender(string $className): bool
56
    {
57
        if ($this->isBelongedTo($className)) {
58
            return true;
59
        } elseif (is_null($this->dependerPatterns)) {
0 ignored issues
show
introduced by
The condition is_null($this->dependerPatterns) is always false.
Loading history...
60
            return true;
61
        }
62
63
        return $this->dependerPatterns->isMatch($className);
64
//        return $this->checkPatterns($className, $this->dependerPatterns);
65
    }
66
67
    public function verifyDependee(string $className): bool
68
    {
69
        if ($this->isBelongedTo($className)) {
70
            return true;
71
        } elseif (is_null($this->dependeePatterns)) {
0 ignored issues
show
introduced by
The condition is_null($this->dependeePatterns) is always false.
Loading history...
72
            return true;
73
        }
74
75
        return $this->dependeePatterns->isMatch($className);
76
//        return $this->checkPatterns($className, $this->dependeePatterns);
77
    }
78
79
    /**
80
     * @param string $className
81
     * @param QualifiedNamePattern[] $patterns
82
     * @return bool
83
     */
84
    protected function checkPatterns(string $className, array $patterns): bool
85
    {
86
        foreach ($patterns as $pattern) {
87
            if ($pattern->isMatch($className)) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
}
95