Completed
Pull Request — master (#283)
by Enrico
04:27
created

Compiler   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.26%

Importance

Changes 0
Metric Value
dl 0
loc 161
ccs 51
cts 62
cp 0.8226
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addClass() 0 4 1
A addTrait() 0 4 1
A addFunction() 0 4 1
A getFunctionNS() 0 10 4
A getTrait() 0 8 2
A getFunction() 0 10 3
C compile() 0 68 14
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
/**
15
 * Compiler component
16
 */
17
class Compiler
18
{
19
    /**
20
     * @var ClassDefinition[]
21
     */
22
    protected $classes = [];
23
24
    /**
25
     * @var TraitDefinition[]
26
     */
27
    protected $traits = [];
28
29
    /**
30
     * @var FunctionDefinition[]
31
     */
32
    protected $functions = [];
33
34
    /**
35
     * @param ClassDefinition $class
36
     */
37 50
    public function addClass(ClassDefinition $class)
38
    {
39 50
        $this->classes[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
40 50
    }
41
42
    /**
43
     * @param TraitDefinition $class
44
     */
45 1
    public function addTrait(TraitDefinition $class)
46
    {
47 1
        $this->traits[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
48 1
    }
49
50
    /**
51
     * @param FunctionDefinition $function
52
     */
53 5
    public function addFunction(FunctionDefinition $function)
54
    {
55 5
        $this->functions[] = $function;
56 5
    }
57
58
    /**
59
     * @param Context $context
60
     */
61 50
    public function compile(Context $context)
0 ignored issues
show
Complexity introduced by
This operation has 320 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
62
    {
63 50
        $context->scopePointer = null;
64
65
        /**
66
         * @todo Implement class map...
67
         */
68 50
        foreach ($this->classes as $class) {
69 50
            $extends = $class->getExtendsClass();
70 50
            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...
71 2
                if (isset($this->classes[$extends])) {
72
                    $class->setExtendsClassDefinition($this->classes[$extends]);
73
                } else {
74 2
                    if (class_exists($extends, true)) {
75 2
                        $class->setExtendsClassDefinition(
76 2
                            new RuntimeClassDefinition(
77 2
                                new ReflectionClass(
78
                                    $extends
79 2
                                )
80 2
                            )
81 2
                        );
82 2
                    }
83
                }
84 2
            }
85 50
        }
86
87 50
        foreach ($this->functions as $function) {
88
            /**
89
             * @todo Configuration
90
             *
91
             * Ignore functions compiling from vendor
92
             */
93 5
            $checkVendor = strpos($function->getFilepath(), './vendor');
94 5
            if ($checkVendor !== false && $checkVendor < 3) {
95
                continue;
96
            }
97
98 5
            $function->compile($context);
99 50
        }
100
101 50
        foreach ($this->traits as $trait) {
102
            /**
103
             * @todo Configuration
104
             *
105
             * Ignore traits compiling from vendor
106
             */
107 1
            $checkVendor = strpos($trait->getFilepath(), './vendor');
108 1
            if ($checkVendor !== false && $checkVendor < 3) {
109
                continue;
110
            }
111
112 1
            $trait->compile($context);
113 50
        }
114
115 50
        foreach ($this->classes as $class) {
116
            /**
117
             * @todo Configuration
118
             *
119
             * Ignore Classes compiling from vendor
120
             */
121 50
            $checkVendor = strpos($class->getFilepath(), './vendor');
122 50
            if ($checkVendor !== false && $checkVendor < 3) {
123
                continue;
124
            }
125
126 50
            $class->compile($context);
127 50
        }
128 50
    }
129
130
    /**
131
     * Try to find function with $namespace from pre-compiled function(s)
132
     *
133
     * @param string $name
134
     * @param string|null $namespace
135
     * @return bool|FunctionDefinition
136
     */
137 11
    public function getFunctionNS($name, $namespace = null)
138
    {
139 11
        foreach ($this->functions as $function) {
140 1
            if ($function->getName() == $name && $function->getNamespace() == $namespace) {
141
                return $function;
142
            }
143 11
        }
144
145 11
        return false;
146
    }
147
148
    /**
149
     * @param string $name
150
     * @return TraitDefinition|null
151
     */
152
    public function getTrait($name)
153
    {
154
        if (isset($this->traits[$name])) {
155
            return $this->traits[$name];
156
        }
157
158
        return null;
159
    }
160
161
    /**
162
     * Try to find function from pre-compiled function(s)
163
     *
164
     * @param string $name
165
     * @return bool|FunctionDefinition
166
     */
167 1
    public function getFunction($name)
168
    {
169 1
        foreach ($this->functions as $function) {
170 1
            if ($function->getName() == $name) {
171
                return $function;
172
            }
173 1
        }
174
175 1
        return false;
176
    }
177
}
178