Completed
Push — master ( cacb89...b59b39 )
by Дмитрий
9s
created

ClassMethod   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 10

Test Coverage

Coverage 54.22%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 203
ccs 45
cts 83
cp 0.5422
rs 10
wmc 25
lcom 3
cbo 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C compile() 0 68 11
A run() 0 4 1
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 getReturnType() 0 4 1
A addReturnPossibleValue() 0 4 1
A getPossibleReturnValues() 0 4 1
A getParams() 0 4 1
A addNewType() 0 8 2
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 11
    public function __construct($name, Node\Stmt\ClassMethod $statement, $type)
45
    {
46 11
        $this->name = $name;
47 11
        $this->statement = $statement;
48 11
        $this->type = $type;
49 11
    }
50
51
    /**
52
     * @param Context $context
53
     * @return boolean|null
54
     */
55 10
    public function compile(Context $context)
56
    {
57 10
        $context->getEventManager()->fire(
58 10
            Event\StatementBeforeCompile::EVENT_NAME,
59 10
            new Event\StatementBeforeCompile(
60 10
                $this->statement,
61
                $context
62 10
            )
63 10
        );
64
65 10
        $this->compiled = true;
66 10
        $context->scopePointer = $this->getPointer();
67
68 10
        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 10
        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 10
        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 10
        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 10
        foreach ($this->statement->stmts as $st) {
120 10
            \PHPSA\nodeVisitorFactory($st, $context);
121 10
        }
122 10
    }
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 10
    public function isAbstract()
139
    {
140 10
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
141
    }
142
143
    /**
144
     * @return bool
145
     */
146 10
    public function isStatic()
147
    {
148 10
        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 10
    public function addNewType($newType)
179
    {
180 10
        if ($this->returnType != CompiledExpression::VOID) {
181 1
            $this->returnType = $this->returnType | $newType;
182 1
        } else {
183 10
            $this->returnType = $newType;
184
        }
185 10
    }
186
187
    /**
188
     * @return int
189
     */
190
    public function getReturnType()
191
    {
192
        return $this->returnType;
193
    }
194
195
    /**
196
     * @param $value
197
     */
198 10
    public function addReturnPossibleValue($value)
199
    {
200 10
        $this->possibleReturnValues[] = $value;
201 10
    }
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