EncapsulationViolation::after()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Solidifier\Visitors\Encapsulation;
4
5
use Solidifier\Parser\Visitors\ContextualVisitor;
6
use PhpParser\Node;
7
use PhpParser\Node\Stmt\Property;
8
use PhpParser\Node\Stmt\ClassMethod;
9
use PhpParser\Node\Stmt\Class_;
10
11
class EncapsulationViolation extends ContextualVisitor
12
{
13
    private
14
        $privateAttributes,
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 $privateAttributes.

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
        $publicMethods;
16
    
17
    protected function enter(Node $node)
18
    {
19
        if($node instanceof Class_)
20
        {
21
            $this->privateAttributes = array();
22
            $this->publicMethods = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
23
        } 
24
        elseif($node instanceof Property)
25
        {
26
            $this->enterProperty($node);
27
        } 
28
        elseif($node instanceof ClassMethod)
29
        {
30
            $this->enterClassMethod($node);
31
        }
32
    }
33
    
34
    private function enterProperty(Property $node)
35
    {
36
        if($node->isPrivate())
37
        {
38
            foreach($node->props as $property)
39
            {
40
                $this->privateAttributes[$property->name] = $node;
41
            }
42
        }
43
    }
44
    
45
    private function enterClassMethod(ClassMethod $node)
46
    {
47
        if($node->isPublic())
48
        {
49
            $methodName = $node->name;
50
51
            if($this->isGetterOrSetter($methodName))
52
            {
53
                $correspondingAttribute = strtolower(substr($methodName, 3));
54
                
55
                if(! isset($this->publicMethods[$correspondingAttribute]))
56
                {
57
                    $this->publicMethods[$correspondingAttribute] = array();
58
                }
59
                
60
                $this->publicMethods[$correspondingAttribute][] = $methodName;    
61
            }
62
        }
63
    }
64
    
65
    private function isGetterOrSetter($methodName)
66
    {
67
        $prefix = strtolower(substr($methodName, 0, 3));
68
        
69
        return $prefix === 'get' || $prefix === 'set';
70
    }
71
    
72
    protected function leave(Node $node)
73
    {
74
        if($node instanceof Class_)
75
        {
76
            foreach($this->privateAttributes as $attribute => $attributeNode)
77
            {
78
                $key = strtolower($attribute);
79
                if(isset($this->publicMethods[$key]))
80
                {
81
                    if(count($this->publicMethods[$key]) >= 2)
82
                    {
83
                        $this->dispatch(
84
                            new \Solidifier\Defects\EncapsulationViolation($attribute, $attributeNode)
85
                        );
86
                    }
87
                }
88
            }
89
        }   
90
    }
91
    
92
    protected function after(array $nodes)
93
    {
94
        
95
    }   
96
}