ConfigurationHandler::addVisitors()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 5
nop 3
1
<?php
2
3
namespace Solidifier;
4
5
use Solidifier\Visitors\Encapsulation\PublicAttributes;
6
use Solidifier\Visitors\GetterSetter\FluidSetters;
7
use Solidifier\Visitors\Encapsulation\EncapsulationViolation;
8
use Solidifier\Visitors\DependencyInjection\MagicalInstantiation;
9
use Solidifier\Visitors\DependencyInjection\StrongCoupling;
10
use Solidifier\Visitors\PreAnalyze\ObjectTypes;
11
use Solidifier\Visitors\DependencyInjection\InterfaceSegregation;
12
use Solidifier\Visitors\Encapsulation\PublicClass;
13
14
class ConfigurationHandler
15
{
16
    private
17
        $configuration,
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 $configuration.

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...
18
        $objectTypesList,
19
        $visitors;
20
    
21
    public function __construct(array $configuration, ObjectTypes $objectTypesList)
22
    {
23
        $this->configuration = $configuration;
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...
24
        $this->objectTypesList = $objectTypesList;    
25
        
26
        $this->initializeVisitors();
27
    }
28
    
29
    public function configure(VisitableAnalyzer $analyzer)
30
    {
31
        $this->addPreAnalyzeVisitors($analyzer);            
32
        $this->addAnalyzeVisitors($analyzer);            
33
    }
34
    
35
    private function addPreAnalyzeVisitors(VisitableAnalyzer $analyzer)
36
    {
37
        $traverseName = 'preAnalyze';
38
        
39
        // this visitor must not be disabled
40
        $analyzer->addVisitor($traverseName, $this->objectTypesList);
41
        
42
        $visitors = array();
43
        
44
        return $this->addVisitors($analyzer, $visitors, $traverseName);
45
    }
46
    
47
    private function initializeVisitors()
48
    {        
49
        $this->visitors = array(
50
                        
51
            'property.public' => function(array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
                return new PublicAttributes();    
53
            },
54
            
55
            'class.public' => function(array $config) {
56
                $visitor = new PublicClass();
57
58
                if(isset($config['threshold']) && is_numeric($config['threshold']))
59
                {
60
                    $visitor->setThreshold($config['threshold']);
61
                }
62
63
                if(isset($config['minMethodCount']) && is_numeric($config['minMethodCount']))
64
                {
65
                    $visitor->setMinMethodCount($config['minMethodCount']);
66
                }
67
                
68
                return $visitor;
69
            },
70
            
71
            'getterSetter.fluid' => function(array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
                return new FluidSetters();    
73
            },
74
            
75
            'getterSetter.encapsulationViolation' => function(array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
                return new EncapsulationViolation();    
77
            },
78
            
79
            'dependency.magical' => function(array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
                return new MagicalInstantiation();    
81
            },
82
            
83
            'dependency.strongCoupling' => function(array $config) {
84
                $visitor = new StrongCoupling();
85
                
86
                $visitor->addExcludePattern('~Iterator$~')
87
                    ->addExcludePattern('~^Null~')
88
                    ->addExcludePattern('~Exception$~');
89
                
90
                if(isset($config['excludePatterns']))
91
                {
92
                    foreach($config['excludePatterns'] as $pattern)
93
                    {
94
                        $visitor->addExcludePattern("~$pattern~");
95
                    }            
96
                }
97
                
98
                if(isset($config['allowedClasses']))
99
                {
100
                    foreach($config['allowedClasses'] as $fullyQualifiedClassName)
101
                    {
102
                        $visitor->addAllowedClass($fullyQualifiedClassName);
103
                    }            
104
                }
105
                
106
               return $visitor;
107
            },
108
            
109
            'dependency.interfaceSegregation' => function(array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
                return new InterfaceSegregation($this->objectTypesList);    
111
            },
112
        );
113
    }
114
    
115
    private function addAnalyzeVisitors(VisitableAnalyzer $analyzer)
116
    {
117
        return $this->addVisitors($analyzer, $this->visitors, 'analyze');
118
    }
119
    
120
    private function addVisitors(VisitableAnalyzer $analyzer, array $visitors, $traverse)
121
    {
122
        foreach($visitors as $key => $closure)
123
        {
124
            $config = array();
125
            if(isset($this->configuration[$key]))
126
            {
127
                $config = $this->configuration[$key];
128
            }
129
            
130
            if(! isset($config['enabled']) || $config['enabled'] === true)
131
            {
132
                $analyzer->addVisitor($traverse, $closure($config));
133
            }
134
        }
135
    }
136
}