Completed
Pull Request — master (#264)
by Enrico
11:14
created

Compiler   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 66.67%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 161
ccs 42
cts 63
cp 0.6667
rs 10
c 2
b 0
f 1
wmc 26
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addClass() 0 4 1
A addFunction() 0 4 1
A addTrait() 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
class Compiler
15
{
16
    /**
17
     * @var ClassDefinition[]
18
     */
19
    protected $classes = [];
20
21
    /**
22
     * @var TraitDefinition[]
23
     */
24
    protected $traits = [];
25
26
    /**
27
     * @var FunctionDefinition[]
28
     */
29
    protected $functions = [];
30
31
    /**
32
     * @param ClassDefinition $class
33
     */
34 46
    public function addClass(ClassDefinition $class)
35
    {
36 46
        $this->classes[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
37 46
    }
38
39
    /**
40
     * @param TraitDefinition $class
41
     */
42 1
    public function addTrait(TraitDefinition $class)
43
    {
44 1
        $this->traits[implode('\\', [$class->getNamespace(), $class->getName()])] = $class;
45 1
    }
46
47
    /**
48
     * @param FunctionDefinition $function
49
     */
50 5
    public function addFunction(FunctionDefinition $function)
51
    {
52 5
        $this->functions[] = $function;
53 5
    }
54
55
    /**
56
     * @param Context $context
57
     */
58 46
    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...
59
    {
60 46
        $context->scopePointer = null;
61
62
        /**
63
         * @todo Implement class map...
64
         */
65 46
        foreach ($this->classes as $class) {
66 46
            $extends = $class->getExtendsClass();
67 46
            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 46
        }
83
84 46
        foreach ($this->functions as $function) {
85
            /**
86
             * @todo Configuration
87
             *
88
             * Ignore functions compiling from vendor
89
             */
90 5
            $checkVendor = strpos($function->getFilepath(), './vendor');
91 5
            if ($checkVendor !== false && $checkVendor < 3) {
92
                continue;
93 1
            }
94
95 5
            $function->compile($context);
96 46
        }
97
98 46
        foreach ($this->traits as $trait) {
99
            /**
100
             * @todo Configuration
101
             *
102
             * Ignore traits compiling from vendor
103
             */
104 1
            $checkVendor = strpos($trait->getFilepath(), './vendor');
105 1
            if ($checkVendor !== false && $checkVendor < 3) {
106
                continue;
107
            }
108
109 1
            $trait->compile($context);
110 46
        }
111
112 46
        foreach ($this->classes as $class) {
113
            /**
114
             * @todo Configuration
115
             *
116
             * Ignore Classes compiling from vendor
117
             */
118 46
            $checkVendor = strpos($class->getFilepath(), './vendor');
119 46
            if ($checkVendor !== false && $checkVendor < 3) {
120
                continue;
121
            }
122
123 46
            $class->compile($context);
124 46
        }
125 46
    }
126
127
    /**
128
     * Try to find function with $namespace from pre-compiled function(s)
129
     *
130
     * @param string $name
131
     * @param string|null $namespace
132
     * @return bool|FunctionDefinition
133
     */
134 9
    public function getFunctionNS($name, $namespace = null)
135
    {
136 9
        foreach ($this->functions as $function) {
137 1
            if ($function->getName() == $name && $function->getNamespace() == $namespace) {
138
                return $function;
139
            }
140 9
        }
141
142 9
        return false;
143
    }
144
145
    /**
146
     * @param string $name
147
     * @return TraitDefinition|null
148
     */
149
    public function getTrait($name)
150
    {
151
        if (isset($this->traits[$name])) {
152
            return $this->traits[$name];
153
        }
154
155
        return null;
156
    }
157
158
    /**
159
     * Try to find function from pre-compiled function(s)
160
     *
161
     * @param string $name
162
     * @return bool|FunctionDefinition
163
     */
164 1
    public function getFunction($name)
165
    {
166 1
        foreach ($this->functions as $function) {
167 1
            if ($function->getName() == $name) {
168
                return $function;
169
            }
170 1
        }
171
172 1
        return false;
173
    }
174
}
175