InterfaceSegregation::checkIfTypeIsAnInterface()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 2
1
<?php
2
3
namespace Solidifier\Visitors\DependencyInjection;
4
5
use Solidifier\Parser\Visitors\ContextualVisitor;
6
use Solidifier\Visitors\PreAnalyze\ObjectTypes;
7
use PhpParser\Node;
8
use PhpParser\Node\Param;
9
use PhpParser\Node\Name;
10
use Solidifier\Visitors\ObjectType;
11
use Solidifier\Defects\DependencyUponImplementation;
12
13
class InterfaceSegregation extends ContextualVisitor
14
{
15
    private
16
        $objectTypes,
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 $objectTypes.

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...
17
        $types;
18
    
19
    public function __construct(ObjectTypes $objectTypes)
20
    {
21
        parent::__construct();
22
        
23
        $this->objectTypes = $objectTypes;
24
        $this->types = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
25
    }    
26
    
27
    public function before(array $nodes)
28
    {
29
        $this->types = $this->objectTypes->getObjectTypes();
30
    }
31
    
32
    protected function enter(Node $node)
33
    {
34
        if($node instanceof Param)
35
        {
36
            if($this->currentMethod !== null)
37
            {
38
                if($node->type instanceof Name)
39
                {
40
                    $this->checkIfTypeIsAnInterface($node, $node->type);
41
                }
42
            }
43
        }
44
    }
45
    
46
    private function checkIfTypeIsAnInterface(Node $node, Name $type)
47
    {
48
        $name = (string) $type;
49
        
50
        if($type->isFullyQualified() === false)
51
        {
52
            $name = $this->currentNamespace . '\\' . $name;
53
        }
54
        
55
        if(isset($this->types[$name]))
56
        {
57
            $objectTypeType= $this->types[$name];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

Loading history...
58
            
59
            if($objectTypeType !== ObjectType::TYPE_INTERFACE)
60
            {
61
                $defect = new DependencyUponImplementation(
62
                    $node,
63
                    $name,
64
                    $objectTypeType,
65
                    $this->currentMethod->name
66
                );
67
                
68
                $defect->setContext($this->currentMethod);
69
                
70
                $this->dispatch($defect);
71
            }
72
        }
73
    }
74
}