Completed
Push — master ( 88ff74...459ac7 )
by Enrico
13:32
created

ClassMethod::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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 41
    public function __construct($name, Node\Stmt\ClassMethod $statement, $type)
49
    {
50 41
        $this->name = $name;
51 41
        $this->statement = $statement;
52 41
        $this->type = $type;
53 41
    }
54
55
    /**
56
     * @param Context $context
57
     * @return boolean|null
58
     */
59 40
    public function compile(Context $context)
60
    {
61 40
        $context->getEventManager()->fire(
62 40
            Event\StatementBeforeCompile::EVENT_NAME,
63 40
            new Event\StatementBeforeCompile(
64 40
                $this->statement,
65
                $context
66 40
            )
67 40
        );
68
69 40
        $this->compiled = true;
70 40
        $context->scopePointer = $this->getPointer();
71
72
        /**
73
         * It's not needed to compile empty method via it's abstract
74
         */
75 40
        if ($this->isAbstract()) {
76
            /** @var ClassDefinition $scope */
77 1
            $scope = $context->scope;
78 1
            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 1
            return true;
87
        }
88
89 40
        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...
90 4
            foreach ($this->statement->params as $parameter) {
91 4
                $type = CompiledExpression::UNKNOWN;
92
93 4
                if ($parameter->type) {
94
                    if (is_string($parameter->type)) {
95
                        $type = Types::getType($parameter->type);
96
                    } elseif ($parameter->type instanceof Node\Name\FullyQualified) {
97
                        $type = CompiledExpression::OBJECT;
98
                    }
99
                }
100
101 4
                $context->addVariable(
102 4
                    new Parameter($parameter->name, null, $type, $parameter->byRef)
103 4
                );
104 4
            }
105 4
        }
106
107 40
        foreach ($this->statement->stmts as $st) {
108 39
            \PHPSA\nodeVisitorFactory($st, $context);
109 40
        }
110 40
    }
111
112
    /**
113
     * @param Context $context
114
     * @param CompiledExpression[] $args
115
     * @return CompiledExpression
116
     * @throws \PHPSA\Exception\RuntimeException
117
     */
118
    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...
119
    {
120
        return new CompiledExpression();
121
    }
122
123
    /**
124
     * @return bool
125
     */
126 40
    public function isAbstract()
127
    {
128 40
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
129
    }
130
131
    /**
132
     * @return bool
133
     */
134 40
    public function isStatic()
135
    {
136 40
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_STATIC);
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function isPublic()
143
    {
144
        return ($this->type & Node\Stmt\Class_::MODIFIER_PUBLIC) !== 0 || $this->type === 0;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    public function isProtected()
151
    {
152
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PROTECTED);
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function isPrivate()
159
    {
160
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_PRIVATE);
161
    }
162
163
    /**
164
     * @param integer $newType
165
     */
166 29
    public function addNewType($newType)
167
    {
168 29
        if ($this->returnType != CompiledExpression::VOID) {
169 3
            $this->returnType = $this->returnType | $newType;
170 3
        } else {
171 29
            $this->returnType = $newType;
172
        }
173 29
    }
174
175
    /**
176
     * @return int
177
     */
178
    public function getReturnType()
179
    {
180
        return $this->returnType;
181
    }
182
183
    /**
184
     * @param $value
185
     */
186 29
    public function addReturnPossibleValue($value)
187
    {
188 29
        $this->possibleReturnValues[] = $value;
189 29
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function getPossibleReturnValues()
195
    {
196
        return $this->possibleReturnValues;
197
    }
198
199
    /**
200
    * @return Node\Param[]
201
    */
202 1
    public function getParams()
203
    {
204 1
        return $this->statement->params;
205
    }
206
207
    /**
208
     * @return int
209
     */
210
    public function getModifier()
211
    {
212
        return $this->type;
213
    }
214
215
    /**
216
     * @param int $type
217
     */
218
    public function setModifier($type)
219
    {
220
        $this->type = $type;
221
    }
222
}
223