Completed
Push — master ( d6a494...e1144e )
by Дмитрий
11s
created

ClassMethod::compile()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11.551

Importance

Changes 0
Metric Value
cc 9
eloc 28
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 52
ccs 26
cts 38
cp 0.6842
crap 11.551
rs 6.5703

How to fix   Long Method   

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\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