Completed
Push — master ( 124462...3bbcdb )
by Дмитрий
02:56
created

ClassMethod::compile()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 68
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 27.0318

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 38
c 2
b 0
f 0
nc 14
nop 1
dl 0
loc 68
ccs 25
cts 51
cp 0.4902
crap 27.0318
rs 5.8371

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\Definition;
7
8
use PhpParser\Node;
9
use PHPSA\CompiledExpression;
10
use PHPSA\Compiler\Expression;
11
use PHPSA\Compiler\Parameter;
12
use PHPSA\Compiler\Types;
13
use PHPSA\Compiler\Event;
14
use PHPSA\Context;
15
16
class ClassMethod extends AbstractDefinition
17
{
18
    protected $type;
19
20
    /**
21
     * @var Node\Stmt\ClassMethod
22
     */
23
    protected $statement;
24
25
    /**
26
     * Return type
27
     *
28
     * @var int
29
     */
30
    protected $returnType = CompiledExpression::VOID;
31
32
    /**
33
     * Array of possible return values
34
     *
35
     * @var array
36
     */
37
    protected $possibleReturnValues = array();
38
39
    /**
40
     * @param string $name
41
     * @param Node\Stmt\ClassMethod $statement
42
     * @param integer $type
43
     */
44 9
    public function __construct($name, Node\Stmt\ClassMethod $statement, $type)
45
    {
46 9
        $this->name = $name;
47 9
        $this->statement = $statement;
48 9
        $this->type = $type;
49 9
    }
50
51
    /**
52
     * @param Context $context
53
     * @return boolean|null
54
     */
55 8
    public function compile(Context $context)
56
    {
57 8
        $context->getEventManager()->fire(
58 8
            Event\StatementBeforeCompile::EVENT_NAME,
59 8
            new Event\StatementBeforeCompile(
60 8
                $this->statement,
61
                $context
62 8
            )
63 8
        );
64
65 8
        $this->compiled = true;
66 8
        $context->scopePointer = $this->getPointer();
67
68 8
        if ($this->statement->getDocComment() === null) {
69
            $context->notice(
70
                'missing-docblock',
71
                sprintf('Missing docblock for %s() method', $this->name),
72
                $this->statement
73
            );
74
        }
75
76
        /**
77
         * It's not needed to compile empty method via it's abstract
78
         */
79 8
        if ($this->isAbstract()) {
80
            /** @var ClassDefinition $scope */
81
            $scope = $context->scope;
82
            if (!$scope->isAbstract()) {
83
                $context->notice(
84
                    'not-abstract-class-with-abstract-method',
85
                    'Class must be an abstract',
86
                    $this->statement
87
                );
88
            }
89
90
            return true;
91
        }
92
93 8
        if (count($this->statement->stmts) == 0) {
94
            return $context->notice(
95
                'not-implemented-method',
96
                sprintf('Method %s() is not implemented', $this->name),
97
                $this->statement
98
            );
99
        }
100
101 8
        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...
102 1
            foreach ($this->statement->params as $parameter) {
103 1
                $type = CompiledExpression::UNKNOWN;
104
105 1
                if ($parameter->type) {
106
                    if (is_string($parameter->type)) {
107
                        $type = Types::getType($parameter->type);
108
                    } elseif ($parameter->type instanceof Node\Name\FullyQualified) {
109
                        $type = CompiledExpression::OBJECT;
110
                    }
111
                }
112
113 1
                $context->addVariable(
114 1
                    new Parameter($parameter->name, null, $type, $parameter->byRef)
115 1
                );
116 1
            }
117 1
        }
118
119 8
        foreach ($this->statement->stmts as $st) {
120 8
            \PHPSA\nodeVisitorFactory($st, $context);
121 8
        }
122 8
    }
123
124
    /**
125
     * @param Context $context
126
     * @param CompiledExpression[] $args
127
     * @return CompiledExpression
128
     * @throws \PHPSA\Exception\RuntimeException
129
     */
130
    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...
131
    {
132
        return new CompiledExpression();
133
    }
134
135
    /**
136
     * @return bool
137
     */
138 8
    public function isAbstract()
139
    {
140 8
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
141
    }
142
143
    /**
144
     * @return bool
145
     */
146 8
    public function isStatic()
147
    {
148 8
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_STATIC);
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function isPublic()
155
    {
156
        return ($this->type & Node\Stmt\Class_::MODIFIER_PUBLIC) !== 0 || $this->type === 0;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function isProtected()
163
    {
164
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PROTECTED);
165
    }
166
167
    /**
168
     * @return bool
169
     */
170
    public function isPrivate()
171
    {
172
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PRIVATE);
173
    }
174
175
    /**
176
     * @param integer $newType
177
     */
178 8
    public function addNewType($newType)
179
    {
180 8
        if ($this->returnType != CompiledExpression::VOID) {
181
            $this->returnType = $this->returnType | $newType;
182
        } else {
183 8
            $this->returnType = $newType;
184
        }
185 8
    }
186
187
    /**
188
     * @return int
189
     */
190
    public function getReturnType()
191
    {
192
        return $this->returnType;
193
    }
194
195
    /**
196
     * @param $value
197
     */
198 8
    public function addReturnPossibleValue($value)
199
    {
200 8
        $this->possibleReturnValues[] = $value;
201 8
    }
202
203
    /**
204
     * @return array
205
     */
206
    public function getPossibleReturnValues()
207
    {
208
        return $this->possibleReturnValues;
209
    }
210
211
    /**
212
    * @return array
213
    */
214 1
    public function getParams()
215
    {
216 1
        return $this->statement->params;
217
    }
218
}
219