Completed
Pull Request — master (#325)
by Enrico
02:57
created

ClassMethod   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 10

Test Coverage

Coverage 65.82%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 214
ccs 52
cts 79
cp 0.6582
rs 10
wmc 26
lcom 3
cbo 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
C compile() 0 52 9
A isAbstract() 0 4 1
A isStatic() 0 4 1
A isPublic() 0 4 2
A isProtected() 0 4 1
A isPrivate() 0 4 1
A addNewType() 0 8 2
A getReturnType() 0 4 1
A addReturnPossibleValue() 0 4 1
A getPossibleReturnValues() 0 4 1
A getParams() 0 4 1
A getModifier() 0 4 1
A setModifier() 0 4 1
A run() 0 4 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PhpParser\Node;
9
use PHPSA\CompiledExpression;
10
use PHPSA\Compiler\Parameter;
11
use PHPSA\Compiler\Types;
12
use PHPSA\Compiler\Event;
13
use PHPSA\Context;
14
15
/**
16
 * Class Method Definition
17
 */
18
class ClassMethod extends AbstractDefinition
19
{
20
    /**
21
     * Contains a number representing all modifiers (public, static, ...)
22
     *
23
     * @var int
24
     */
25
    protected $type;
26
27
    /**
28
     * @var Node\Stmt\ClassMethod
29
     */
30
    protected $statement;
31
32
    /**
33
     * Return type
34
     *
35
     * @var int
36
     */
37
    protected $returnType = CompiledExpression::UNKNOWN;
38
39
    /**
40
     * Array of possible return values
41
     *
42
     * @var array
43
     */
44
    protected $possibleReturnValues = [];
45
46
    /**
47
     * @param string $name
48
     * @param Node\Stmt\ClassMethod $statement
49
     * @param integer $type
50
     */
51 50
    public function __construct($name, Node\Stmt\ClassMethod $statement, $type)
52
    {
53 50
        $this->name = $name;
54 50
        $this->statement = $statement;
55 50
        $this->type = $type;
56
57
        // @todo Better support...
58 50
        $returnType = $statement->getReturnType();
59 50
        if ($returnType == 'void') {
60 1
            $this->returnType = CompiledExpression::VOID;
61 1
        }
62 50
    }
63
64
    /**
65
     * @param Context $context
66
     * @return boolean|null
67
     */
68 49
    public function compile(Context $context)
69
    {
70 49
        $context->getEventManager()->fire(
71 49
            Event\StatementBeforeCompile::EVENT_NAME,
72 49
            new Event\StatementBeforeCompile(
73 49
                $this->statement,
74
                $context
75 49
            )
76 49
        );
77
78 49
        $this->compiled = true;
79 49
        $context->scopePointer = $this->getPointer();
80
81
        /**
82
         * It's not needed to compile empty method via it's abstract
83
         */
84 49
        if ($this->isAbstract()) {
85
            /** @var ClassDefinition $scope */
86 1
            $scope = $context->scope;
87 1
            if (!$scope->isAbstract()) {
88
                $context->notice(
89
                    'not-abstract-class-with-abstract-method',
90
                    'Class must be abstract',
91
                    $this->statement
92
                );
93
            }
94
95 1
            return true;
96
        }
97
98 49
        if ($this->statement->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statement->params of type PhpParser\Node\Param[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99 6
            foreach ($this->statement->params as $parameter) {
100 6
                $type = CompiledExpression::UNKNOWN;
101
102 6
                if ($parameter->type) {
103
                    if (is_string($parameter->type)) {
104
                        $type = Types::getType($parameter->type);
105
                    } elseif ($parameter->type instanceof Node\Name) {
106
                        $type = CompiledExpression::OBJECT;
107
                    }
108
                }
109
110 6
                $context->addVariable(
111 6
                    new Parameter($parameter->name, null, $type, $parameter->byRef)
112 6
                );
113 6
            }
114 6
        }
115
116 49
        foreach ($this->statement->stmts as $st) {
117 48
            \PHPSA\nodeVisitorFactory($st, $context);
118 49
        }
119 49
    }
120
121
    /**
122
     * @param Context $context
123
     * @param CompiledExpression[] $args
124
     * @return CompiledExpression
125
     * @throws \PHPSA\Exception\RuntimeException
126
     */
127
    public function run(Context $context, array $args = null)
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
Unused Code introduced by
The parameter $args 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...
128
    {
129
        return new CompiledExpression();
130
    }
131
132
    /**
133
     * @return bool
134
     */
135 49
    public function isAbstract()
136
    {
137 49
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143 49
    public function isStatic()
144
    {
145 49
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_STATIC);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isPublic()
152
    {
153
        return ($this->type & Node\Stmt\Class_::MODIFIER_PUBLIC) !== 0 || $this->type === 0;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function isProtected()
160
    {
161
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PROTECTED);
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isPrivate()
168
    {
169
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PRIVATE);
170
    }
171
172
    /**
173
     * @param integer $newType
174
     */
175 35
    public function addNewType($newType)
176
    {
177 35
        if ($this->returnType != CompiledExpression::VOID) {
178 35
            $this->returnType = $this->returnType | $newType;
179 35
        } else {
180 1
            $this->returnType = $newType;
181
        }
182 35
    }
183
184
    /**
185
     * @return int
186
     */
187 1
    public function getReturnType()
188
    {
189 1
        return $this->returnType;
190
    }
191
192
    /**
193
     * @param $value
194
     */
195 35
    public function addReturnPossibleValue($value)
196
    {
197 35
        $this->possibleReturnValues[] = $value;
198 35
    }
199
200
    /**
201
     * @return array
202
     */
203
    public function getPossibleReturnValues()
204
    {
205
        return $this->possibleReturnValues;
206
    }
207
208
    /**
209
    * @return Node\Param[]
210
    */
211 1
    public function getParams()
212
    {
213 1
        return $this->statement->params;
214
    }
215
216
    /**
217
     * @return int
218
     */
219
    public function getModifier()
220
    {
221
        return $this->type;
222
    }
223
224
    /**
225
     * @param int $type
226
     */
227
    public function setModifier($type)
228
    {
229
        $this->type = $type;
230
    }
231
}
232