StrongCoupling::extractClassNameFromFullName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Solidifier\Visitors\DependencyInjection;
4
5
use Solidifier\Parser\Visitors\ContextualVisitor;
6
use PhpParser\Node;
7
use PhpParser\Node\Name;
8
use PhpParser\Node\Expr\New_;
9
use Solidifier\Visitors\ObjectType;
10
11
class StrongCoupling extends ContextualVisitor
12
{
13
    private
14
        $allowedClass,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $allowedClass.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
        $excludePattern;
16
    
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        
21
        $this->allowedClass = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
        $this->excludePattern = array();
23
    }
24
    
25
    public function addAllowedClass($fullyQualifiedClassName)
26
    {
27
        $this->allowedClass[] = $fullyQualifiedClassName;
28
        
29
        return $this;
30
    }
31
    
32
    public function addExcludePattern($regex)
33
    {
34
        $this->excludePattern[] = $regex;
35
        
36
        return $this;
37
    }
38
    
39
    protected function enter(Node $node)
40
    {
41
        if($this->currentObjectType instanceof ObjectType)
42
        {
43
            if($node instanceof New_)
44
            {
45
                if($node->class instanceof Name)
46
                {
47
                    return $this->enterNewName($node);
48
                }
49
            }
50
        }
51
    }
52
    
53
    private function enterNewName(New_ $node)
54
    {
55
        if($this->isCurrentClassAllowedToInstanciateObjects() === false)
56
        {
57
            if($this->isAnAllowedObjectType($node->class) === false)
58
            {
59
                $defect = new \Solidifier\Defects\StrongCoupling($this->currentObjectType, $node);
0 ignored issues
show
Bug introduced by
It seems like $this->currentObjectType can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
60
                $defect->setContext($this->nodeStack->top());
61
                $this->dispatch($defect);
62
            }
63
        }
64
    }
65
    
66
    private function isCurrentClassAllowedToInstanciateObjects()
67
    {
68
        return in_array($this->currentObjectType->fullname, $this->allowedClass);
69
    }
70
    
71
    private function isAnAllowedObjectType($objectType)
72
    {
73
        $allowed = false;
74
75
        $objectType = $this->extractClassNameFromFullName($objectType);
76
77
        foreach($this->excludePattern as $pattern)
78
        {
79
            if(preg_match($pattern, $objectType))
80
            {
81
                $allowed = true;
82
                break;
83
            }
84
        }
85
86
        return $allowed;
87
    }
88
    
89
    private function extractClassNameFromFullName($classFullname)
90
    {
91
        $classname = $classFullname;
92
        
93
        $lastNamespaceDelimiterPosition = strrpos($classFullname, '\\');
94
        if($lastNamespaceDelimiterPosition !== false)
95
        {
96
            $classname = substr($classFullname, $lastNamespaceDelimiterPosition + 1);
97
        }
98
99
        return $classname;
100
    }
101
}
102