Completed
Push — master ( c67701...7e8f6f )
by Дмитрий
06:59 queued 04:15
created

Compiler::compile()   C

Complexity

Conditions 11
Paths 45

Size

Total Lines 54
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 22.1441

Importance

Changes 0
Metric Value
cc 11
eloc 23
c 0
b 0
f 0
nc 45
nop 1
dl 0
loc 54
ccs 17
cts 31
cp 0.5484
crap 22.1441
rs 6.6153

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA;
7
8
use PHPSA\Definition\ClassDefinition;
9
use PHPSA\Definition\FunctionDefinition;
10
use PHPSA\Definition\RuntimeClassDefinition;
11
use PHPSA\Definition\TraitDefinition;
12
use ReflectionClass;
13
14
class Compiler
15
{
16
    /**
17
     * @var ClassDefinition[]
18
     */
19
    protected $classes = array();
20
21
    /**
22
     * @var TraitDefinition[]
23
     */
24
    protected $traits = array();
25
26
    /**
27
     * @var FunctionDefinition[]
28
     */
29
    protected $functions = array();
30
31
    /**
32
     * @param ClassDefinition $class
33
     */
34 25
    public function addClass(ClassDefinition $class)
35
    {
36 25
        $this->classes[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
37 25
    }
38
39
    /**
40
     * @param TraitDefinition $class
41
     */
42
    public function addTrait(TraitDefinition $class)
43
    {
44
        $this->traits[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
45
    }
46
47
    /**
48
     * @param FunctionDefinition $function
49
     */
50 1
    public function addFunction(FunctionDefinition $function)
51
    {
52 1
        $this->functions[] = $function;
53 1
    }
54
55
    /**
56
     * @param Context $context
57
     */
58 25
    public function compile(Context $context)
59
    {
60 25
        $context->scopePointer = null;
61
62
        /**
63
         * @todo Implement class map...
64
         */
65 25
        foreach ($this->classes as $class) {
66 25
            $extends = $class->getExtendsClass();
67 25
            if ($extends) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extends of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68
                if (isset($this->classes[$extends])) {
69
                    $class->setExtendsClassDefinition($this->classes[$extends]);
70
                } else {
71
                    if (class_exists($extends, true)) {
72
                        $class->setExtendsClassDefinition(
73
                            new RuntimeClassDefinition(
74
                                new ReflectionClass(
75
                                    $extends
76
                                )
77
                            )
78
                        );
79
                    }
80
                }
81
            }
82 25
        }
83
84 25
        foreach ($this->functions as $function) {
85
            /**
86
             * @todo Configuration
87
             *
88
             * Ignore functions compiling from vendor
89
             */
90 1
            $checkVendor = strpos($function->getFilepath(), './vendor');
91 1
            if ($checkVendor !== false && $checkVendor < 3) {
92
                continue;
93
            }
94
95 1
            $function->compile($context);
96 25
        }
97
98 25
        foreach ($this->classes as $class) {
99
            /**
100
             * @todo Configuration
101
             *
102
             * Ignore Classes compiling from vendor
103
             */
104 25
            $checkVendor = strpos($class->getFilepath(), './vendor');
105 25
            if ($checkVendor !== false && $checkVendor < 3) {
106
                continue;
107
            }
108
109 25
            $class->compile($context);
110 25
        }
111 25
    }
112
113
    /**
114
     * Try to find function with $namespace from pre-compiled function(s)
115
     *
116
     * @param string $name
117
     * @param string|null $namespace
118
     * @return bool|FunctionDefinition
119
     */
120 10
    public function getFunctionNS($name, $namespace = null)
121
    {
122 10
        foreach ($this->functions as $function) {
123 1
            if ($function->getName() == $name && $function->getNamespace() == $namespace) {
124
                return $function;
125
            }
126 10
        }
127
128 10
        return false;
129
    }
130
131
    /**
132
     * Try to find function from pre-compiled function(s)
133
     *
134
     * @param string $name
135
     * @return bool|FunctionDefinition
136
     */
137 1
    public function getFunction($name)
138
    {
139 1
        foreach ($this->functions as $function) {
140 1
            if ($function->getName() == $name) {
141
                return $function;
142
            }
143 1
        }
144
145 1
        return false;
146
    }
147
}
148