Completed
Pull Request — master (#219)
by
unknown
04:22
created

ClassMethod::addReturnPossibleValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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