Completed
Push — master ( 60e2c0...1e59af )
by Дмитрий
03:24 queued 55s
created

Expression::getNodeName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 22
ccs 0
cts 7
cp 0
rs 9.2
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler;
7
8
use InvalidArgumentException;
9
use PHPSA\Check;
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PhpParser\Node;
13
use PHPSA\Definition\ClassDefinition;
14
use PHPSA\Exception\RuntimeException;
15
use PHPSA\Variable;
16
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
17
18
class Expression
0 ignored issues
show
Complexity introduced by
This class has a complexity of 133 which exceeds the configured maximum of 50.

The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.

Some resources for further reading:

You can also find more detailed suggestions for refactoring in the “Code” section of your repository.

Loading history...
19
{
20
    /**
21
     * @var Context
22
     */
23
    protected $context;
24
25
    /**
26
     * @param Context $context
27
     */
28 363
    public function __construct(Context $context)
29
    {
30 363
        $this->context = $context;
31 363
    }
32
33
    /**
34
     * @param $expr
35
     * @return ExpressionCompilerInterface|AbstractExpressionCompiler
36
     */
37 353
    protected function factory($expr)
38
    {
39 353
        switch (get_class($expr)) {
40
            /**
41
             * Call(s)
42
             */
43 353
            case 'PhpParser\Node\Expr\MethodCall':
44
                return new Expression\MethodCall();
45 353
            case 'PhpParser\Node\Expr\FuncCall':
46
                return new Expression\FunctionCall();
47 353
            case 'PhpParser\Node\Expr\StaticCall':
48
                return new Expression\StaticCall();
49
            /**
50
             * Operators
51
             */
52 353
            case 'PhpParser\Node\Expr\New_':
53
                return new Expression\Operators\NewOp();
54 353
            case 'PhpParser\Node\Expr\Instanceof_':
55
                return new Expression\Operators\InstanceOfOp();
56 353
            case 'PhpParser\Node\Expr\BinaryOp\Identical':
57 28
                return new Expression\BinaryOp\Identical();
58 325
            case 'PhpParser\Node\Expr\BinaryOp\Concat':
59
                return new Expression\Operators\Contact();
60 325
            case 'PhpParser\Node\Expr\BinaryOp\NotIdentical':
61 14
                return new Expression\BinaryOp\NotIdentical();
62 311
            case 'PhpParser\Node\Expr\BinaryOp\Equal':
63 34
                return new Expression\BinaryOp\Equal();
64 277
            case 'PhpParser\Node\Expr\BinaryOp\NotEqual':
65 17
                return new Expression\BinaryOp\NotEqual();
66
            /**
67
             * @link http://php.net/manual/en/language.operators.increment.php
68
             */
69 260
            case 'PhpParser\Node\Expr\PostInc':
70 4
                return new Expression\Operators\PostInc();
71 256
            case 'PhpParser\Node\Expr\PostDec':
72 4
                return new Expression\Operators\PostDec();
73
            /**
74
             * Arithmetical
75
             */
76 252
            case 'PhpParser\Node\Expr\BinaryOp\Div':
77 37
                return new Expression\Operators\Arithmetical\Div();
78 215
            case 'PhpParser\Node\Expr\BinaryOp\Plus':
79 41
                return new Expression\Operators\Arithmetical\Plus();
80 174
            case 'PhpParser\Node\Expr\BinaryOp\Minus':
81 18
                return new Expression\Operators\Arithmetical\Minus();
82 156
            case 'PhpParser\Node\Expr\BinaryOp\Mul':
83 35
                return new Expression\Operators\Arithmetical\Mul();
84 121
            case 'PhpParser\Node\Expr\BinaryOp\Mod':
85 35
                return new Expression\Operators\Arithmetical\Mod();
86
            /**
87
             * Bitwise
88
             * @link http://php.net/manual/ru/language.operators.bitwise.php
89
             */
90 86
            case 'PhpParser\Node\Expr\BinaryOp\BitwiseOr':
91
                return new Expression\Operators\Bitwise\BitwiseOr();
92 86
            case 'PhpParser\Node\Expr\BinaryOp\BitwiseXor':
93
                return new Expression\Operators\Bitwise\BitwiseXor();
94 86
            case 'PhpParser\Node\Expr\BinaryOp\BitwiseAnd':
95
                return new Expression\Operators\Bitwise\BitwiseAnd();
96 86
            case 'PhpParser\Node\Expr\BinaryOp\ShiftRight':
97
                return new Expression\Operators\Bitwise\ShiftRight();
98 86
            case 'PhpParser\Node\Expr\BinaryOp\ShiftLeft':
99
                return new Expression\Operators\Bitwise\ShiftLeft();
100 86
            case 'PhpParser\Node\Expr\BitwiseNot':
101
                return new Expression\Operators\Bitwise\BitwiseNot();
102
            /**
103
             * Logical
104
             */
105 86
            case 'PhpParser\Node\Expr\BinaryOp\BooleanOr':
106 10
                return new Expression\Operators\Logical\BooleanOr();
107 76
            case 'PhpParser\Node\Expr\BinaryOp\BooleanAnd':
108 5
                return new Expression\Operators\Logical\BooleanAnd();
109 71
            case 'PhpParser\Node\Expr\BooleanNot':
110 5
                return new Expression\Operators\Logical\BooleanNot();
111
            /**
112
             * Comparison
113
             */
114 66
            case 'PhpParser\Node\Expr\BinaryOp\Greater':
115 12
                return new Expression\Operators\Comparison\Greater();
116 54
            case 'PhpParser\Node\Expr\BinaryOp\GreaterOrEqual':
117 12
                return new Expression\Operators\Comparison\GreaterOrEqual();
118 42
            case 'PhpParser\Node\Expr\BinaryOp\Smaller':
119 12
                return new Expression\Operators\Comparison\Smaller();
120 30
            case 'PhpParser\Node\Expr\BinaryOp\SmallerOrEqual':
121 12
                return new Expression\Operators\Comparison\SmallerOrEqual();
122
            /**
123
             * Another
124
             */
125 18
            case 'PhpParser\Node\Expr\UnaryMinus':
126 9
                return new Expression\Operators\UnaryMinus();
127 9
            case 'PhpParser\Node\Expr\UnaryPlus':
128 9
                return new Expression\Operators\UnaryPlus();
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     * @param object|string $expr
136
     * @return CompiledExpression
137
     */
138 363
    public function compile($expr)
0 ignored issues
show
Complexity introduced by
This operation has 320 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
139
    {
140 363
        if (is_string($expr)) {
141
            return new CompiledExpression(CompiledExpression::STRING, $expr);
142
        }
143
144 363
        if (!is_object($expr)) {
145
            throw new InvalidArgumentException('$expr must be string/object');
146
        }
147
148 363
        $className = get_class($expr);
149
        switch ($className) {
150 363
            case 'PhpParser\Node\Expr\PropertyFetch':
151
                return $this->passPropertyFetch($expr);
152 363
            case 'PhpParser\Node\Stmt\Property':
153
                return $this->passProperty($expr);
154 363
            case 'PhpParser\Node\Expr\ClassConstFetch':
155
                return $this->passConstFetch($expr);
156 363
            case 'PhpParser\Node\Expr\Assign':
157
                return $this->passSymbol($expr);
158 363
            case 'PhpParser\Node\Expr\AssignRef':
159
                return $this->passSymbolByRef($expr);
160 363
            case 'PhpParser\Node\Expr\Variable':
161
                return $this->passExprVariable($expr);
162
            /**
163
             * Cast operators
164
             */
165 363
            case 'PhpParser\Node\Expr\Cast\Bool_':
166
                return $this->passCastBoolean($expr);
167 363
            case 'PhpParser\Node\Expr\Cast\Int_':
168
                return $this->passCastInt($expr);
169 363
            case 'PhpParser\Node\Expr\Cast\Double':
170
                return $this->passCastFloat($expr);
171 363
            case 'PhpParser\Node\Expr\Cast\String_':
172
                return $this->passCastString($expr);
173 363
            case 'PhpParser\Node\Expr\Cast\Unset_':
174
                return $this->passCastUnset($expr);
175
            /**
176
             * Expressions
177
             */
178 363
            case 'PhpParser\Node\Expr\Array_':
179 11
                return $this->getArray($expr);
180 362
            case 'PhpParser\Node\Expr\ConstFetch':
181
                return $this->constFetch($expr);
182 362
            case 'PhpParser\Node\Name':
183
                return $this->getNodeName($expr);
184
            /**
185
             * Simple Scalar(s)
186
             */
187 362
            case 'PHPSA\Node\Scalar\Nil':
188 8
                return new CompiledExpression(CompiledExpression::NULL);
189 361
            case 'PhpParser\Node\Scalar\LNumber':
190 256
                return new CompiledExpression(CompiledExpression::INTEGER, $expr->value);
191 358
            case 'PhpParser\Node\Scalar\DNumber':
192 131
                return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value);
193 357
            case 'PhpParser\Node\Scalar\String_':
194 8
                return new CompiledExpression(CompiledExpression::STRING, $expr->value);
195 355
            case 'PHPSA\Node\Scalar\Boolean':
196 60
                return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value);
197 353
            case 'PHPSA\Node\Scalar\Fake':
198 29
                return new CompiledExpression($expr->type, $expr->value);
199
        }
200
201 353
        $expressionCompiler = $this->factory($expr);
202 353
        if (!$expressionCompiler) {
203
            $this->context->debug("Expression compiler is not implemented for {$className}");
204
            return new CompiledExpression(CompiledExpression::UNIMPLEMENTED);
205
        }
206
207 353
        $result = $expressionCompiler->pass($expr, $this->context);
208 353
        if (!$result instanceof CompiledExpression) {
209
            throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler));
210
        }
211
212 353
        return $result;
213
    }
214
215
    /**
216
     * @todo Implement
217
     *
218
     * @param Node\Stmt\Property $st
219
     * @return CompiledExpression
220
     */
221
    public function passProperty(Node\Stmt\Property $st)
0 ignored issues
show
Unused Code introduced by
The parameter $st 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...
Comprehensibility introduced by
Avoid variables with short names like $st. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
222
    {
223
        return new CompiledExpression();
224
    }
225
226
    /**
227
     * @param Node\Expr\Variable $expr
228
     * @return CompiledExpression
229
     */
230
    public function declareVariable(Node\Expr\Variable $expr)
231
    {
232
        $variable = $this->context->getSymbol($expr->name);
233
        if ($variable) {
234
            $variable->incGets();
235
            return new CompiledExpression($variable->getType(), $variable->getValue(), $variable);
236
        }
237
238
        $symbol = new Variable($expr->name, null, CompiledExpression::UNKNOWN, $this->context->getCurrentBranch());
0 ignored issues
show
Bug introduced by
It seems like $expr->name can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Variable::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
239
        $this->context->addVariable($symbol);
240
241
        return new CompiledExpression;
242
    }
243
244
    /**
245
     * @param Node\Name $expr
246
     * @return CompiledExpression
247
     */
248
    public function getNodeName(Node\Name $expr)
249
    {
250
        if ($expr->parts[0] === 'null') {
251
            return new CompiledExpression(CompiledExpression::NULL);
252
        }
253
254
        if (in_array($expr, ['self', 'static'])) {
255
            return CompiledExpression::fromZvalValue($this->context->scope);
256
//            $scopePointer = $this->context->scopePointer;
257
//            if ($scopePointer) {
258
//                if ($scopePointer->isClassMethod()) {
259
//                    $classMethod = $scopePointer->getObject();
260
//                    if ($classMethod && $classMethod->isStatic()) {
261
//                        return CompiledExpression::fromZvalValue($this->context->scope);
262
//                    }
263
//                }
264
//            }
265
        }
266
267
        $this->context->debug('[Unknown] How to get Node\Name for ' . $expr);
268
        return new CompiledExpression();
269
    }
270
271
    /**
272
     * (bool) {$expr}
273
     *
274
     * @param Node\Expr\Cast\Bool_ $expr
275
     * @return CompiledExpression
276
     */
277
    protected function passCastBoolean(Node\Expr\Cast\Bool_ $expr)
278
    {
279
        $expression = new Expression($this->context);
280
        $compiledExpression = $expression->compile($expr->expr);
281
282
        switch ($compiledExpression->getType()) {
283
            case CompiledExpression::BOOLEAN:
284
                $this->context->notice('stupid-cast', "You are trying to cast 'boolean' to 'boolean'", $expr);
285
                return $compiledExpression;
286
            case CompiledExpression::DOUBLE:
287
            case CompiledExpression::INTEGER:
288
                return new CompiledExpression(CompiledExpression::BOOLEAN, (bool) $compiledExpression->getValue());
289
        }
290
291
        return new CompiledExpression();
292
    }
293
294
    /**
295
     * (int) {$expr}
296
     *
297
     * @param Node\Expr\Cast\Int_ $expr
298
     * @return CompiledExpression
299
     */
300
    protected function passCastInt(Node\Expr\Cast\Int_ $expr)
301
    {
302
        $expression = new Expression($this->context);
303
        $compiledExpression = $expression->compile($expr->expr);
304
305
        switch ($compiledExpression->getType()) {
306
            case CompiledExpression::INTEGER:
307
                $this->context->notice('stupid-cast', "You are trying to cast 'int' to 'int'", $expr);
308
                return $compiledExpression;
309
            case CompiledExpression::BOOLEAN:
310
            case CompiledExpression::DOUBLE:
311
            case CompiledExpression::STRING:
312
                return new CompiledExpression(CompiledExpression::INTEGER, (int) $compiledExpression->getValue());
313
        }
314
315
        return new CompiledExpression();
316
    }
317
318
    /**
319
     * (float) {$expr}
320
     *
321
     * @param Node\Expr\Cast\Double $expr
322
     * @return CompiledExpression
323
     */
324
    protected function passCastFloat(Node\Expr\Cast\Double $expr)
325
    {
326
        $expression = new Expression($this->context);
327
        $compiledExpression = $expression->compile($expr->expr);
328
329
        switch ($compiledExpression->getType()) {
330
            case CompiledExpression::DOUBLE:
331
                $this->context->notice('stupid-cast', "You are trying to cast 'float' to 'float'", $expr);
332
                return $compiledExpression;
333
            case CompiledExpression::BOOLEAN:
334
            case CompiledExpression::INTEGER:
335
            case CompiledExpression::STRING:
336
                return new CompiledExpression(CompiledExpression::DOUBLE, (float) $compiledExpression->getValue());
337
        }
338
339
        return new CompiledExpression();
340
    }
341
342
    /**
343
     * (string) {$expr}
344
     *
345
     * @param Node\Expr\Cast\String_ $expr
346
     * @return CompiledExpression
347
     */
348
    protected function passCastString(Node\Expr\Cast\String_ $expr)
349
    {
350
        $expression = new Expression($this->context);
351
        $compiledExpression = $expression->compile($expr->expr);
352
353
        switch ($compiledExpression->getType()) {
354
            case CompiledExpression::STRING:
355
                $this->context->notice('stupid-cast', "You are trying to cast 'string' to 'string'", $expr);
356
                return $compiledExpression;
357
            case CompiledExpression::BOOLEAN:
358
            case CompiledExpression::INTEGER:
359
            case CompiledExpression::DOUBLE:
360
                return new CompiledExpression(CompiledExpression::DOUBLE, (string) $compiledExpression->getValue());
361
        }
362
363
        return new CompiledExpression();
364
    }
365
366
    /**
367
     * (unset) {$expr}
368
     *
369
     * @param Node\Expr\Cast\Unset_ $expr
370
     * @return CompiledExpression
371
     */
372
    protected function passCastUnset(Node\Expr\Cast\Unset_ $expr)
373
    {
374
        $expression = new Expression($this->context);
375
        $compiledExpression = $expression->compile($expr->expr);
376
377
        switch ($compiledExpression->getType()) {
378
            case CompiledExpression::NULL:
379
                $this->context->notice('stupid-cast', "You are trying to cast 'unset' to 'null'", $expr);
380
                return $compiledExpression;
381
        }
382
383
        return new CompiledExpression(CompiledExpression::NULL, null);
384
    }
385
386
    /**
387
     * @param Node\Expr\PropertyFetch $expr
388
     * @return CompiledExpression
389
     */
390
    protected function passPropertyFetch(Node\Expr\PropertyFetch $expr)
391
    {
392
        $propertNameCE = $this->compile($expr->name);
393
394
        $scopeExpression = $this->compile($expr->var);
395
        if ($scopeExpression->isObject()) {
396
            $scopeExpressionValue = $scopeExpression->getValue();
397
            if ($scopeExpressionValue instanceof ClassDefinition) {
398
                $propertyName = $propertNameCE->isString() ? $propertNameCE->getValue() : false;
399
                if ($propertyName) {
400
                    if ($scopeExpressionValue->hasProperty($propertyName, true)) {
401
                        $property = $scopeExpressionValue->getProperty($propertyName, true);
402
                        return $this->compile($property);
403
                    } else {
404
                        $this->context->notice(
405
                            'undefined-property',
406
                            sprintf(
407
                                'Property %s does not exist in %s scope',
408
                                $propertyName,
409
                                $scopeExpressionValue->getName()
410
                            ),
411
                            $expr
412
                        );
413
                    }
414
                }
415
            }
416
        } elseif (!$scopeExpression->canBeObject()) {
417
            return new CompiledExpression(CompiledExpression::UNKNOWN);
418
        }
419
420
        $this->context->notice(
421
            'property-fetch-on-non-object',
422
            "It's not possible to fetch property on not object",
423
            $expr,
424
            Check::CHECK_BETA
425
        );
426
427
        return new CompiledExpression(CompiledExpression::UNKNOWN);
428
    }
429
430
    /**
431
     * @param Node\Expr\ClassConstFetch $expr
432
     * @return CompiledExpression
433
     */
434
    protected function passConstFetch(Node\Expr\ClassConstFetch $expr)
435
    {
436
        $leftCE = $this->compile($expr->class);
437
        if ($leftCE->isObject()) {
438
            $leftCEValue = $leftCE->getValue();
439
            if ($leftCEValue instanceof ClassDefinition) {
440
                if (!$leftCEValue->hasConst($expr->name)) {
441
                    $this->context->notice(
442
                        'undefined-const',
443
                        sprintf('Constant %s does not exist in %s scope', $expr->name, $expr->class),
444
                        $expr
445
                    );
446
                    return new CompiledExpression(CompiledExpression::UNKNOWN);
447
                }
448
449
                return new CompiledExpression();
450
            }
451
        }
452
453
        $this->context->debug('Unknown const fetch');
454
        return new CompiledExpression();
455
    }
456
457
    /**
458
     * @param Node\Expr\Assign $expr
459
     * @return CompiledExpression
460
     */
461
    protected function passSymbol(Node\Expr\Assign $expr)
462
    {
463
        $expression = new Expression($this->context);
464
        $compiledExpression = $expression->compile($expr->expr);
465
466
        if ($expr->var instanceof Node\Expr\List_) {
467
            $isCorrectType = false;
468
469
            switch ($compiledExpression->getType()) {
470
                case CompiledExpression::ARR:
471
                    $isCorrectType = true;
472
                    break;
473
            }
474
475
            if ($expr->var->vars) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expr->var->vars of type PhpParser\Node\Expr[] 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...
476
                foreach ($expr->var->vars as $key => $var) {
477
                    if ($var instanceof Node\Expr\Variable) {
478
                        $name = $expr->var->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in PhpParser\Node\Expr\List_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
479
480
                        $symbol = $this->context->getSymbol($name);
481
                        if (!$symbol) {
482
                            $symbol = new Variable(
483
                                $name,
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->var->name on line 478 can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Variable::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
484
                                null,
485
                                CompiledExpression::UNKNOWN,
486
                                $this->context->getCurrentBranch()
487
                            );
488
                            $this->context->addVariable($symbol);
489
                        }
490
491
                        if (!$isCorrectType) {
492
                            $symbol->modify(CompiledExpression::NULL, null);
493
                        }
494
495
                        $symbol->incSets();
496
                    }
497
                }
498
            }
499
500
            return new CompiledExpression();
501
        }
502
503
        if ($expr->var instanceof Node\Expr\Variable) {
504
            $name = $expr->var->name;
505
506
            $symbol = $this->context->getSymbol($name);
507
            if ($symbol) {
508
                $symbol->modify($compiledExpression->getType(), $compiledExpression->getValue());
509
            } else {
510
                $symbol = new Variable(
511
                    $name,
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->var->name on line 504 can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Variable::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
512
                    $compiledExpression->getValue(),
513
                    $compiledExpression->getType(),
514
                    $this->context->getCurrentBranch()
515
                );
516
                $this->context->addVariable($symbol);
517
            }
518
519
            $symbol->incSets();
520
            return $compiledExpression;
521
        }
522
523
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
524
            $compiledExpression = $this->compile($expr->var->var);
525
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
526
                $objectDefinition = $compiledExpression->getValue();
527
                if ($objectDefinition instanceof ClassDefinition) {
528
                    if (is_string($expr->var->name)) {
529
                        if ($objectDefinition->hasProperty($expr->var->name)) {
530
                            return $this->compile($objectDefinition->getProperty($expr->var->name));
531
                        }
532
                    }
533
                }
534
            }
535
        }
536
537
        $this->context->debug('Unknown how to pass symbol');
538
        return new CompiledExpression();
539
    }
540
541
    /**
542
     * @param Node\Expr\AssignRef $expr
543
     * @return CompiledExpression
544
     */
545
    protected function passSymbolByRef(Node\Expr\AssignRef $expr)
546
    {
547
        if ($expr->var instanceof \PhpParser\Node\Expr\List_) {
548
            return new CompiledExpression();
549
        }
550
551
        if ($expr->var instanceof Node\Expr\Variable) {
552
            $name = $expr->var->name;
553
554
            $expression = new Expression($this->context);
555
            $compiledExpression = $expression->compile($expr->expr);
556
557
            $symbol = $this->context->getSymbol($name);
558
            if ($symbol) {
559
                $symbol->modify($compiledExpression->getType(), $compiledExpression->getValue());
560
561
                if ($expr->expr instanceof Node\Expr\Variable) {
562
                    $rightVarName = $expr->expr->name;
563
564
                    $rightSymbol = $this->context->getSymbol($rightVarName);
565
                    if ($rightSymbol) {
566
                        $rightSymbol->incUse();
567
                        $symbol->setReferencedTo($rightSymbol);
568
                    } else {
569
                        $this->context->debug('Cannot fetch variable by name: ' . $rightVarName);
570
                    }
571
                }
572
573
                $this->context->debug('Unknown how to pass referenced to symbol: ' . get_class($expr->expr));
574
            } else {
575
                $symbol = new Variable(
576
                    $name,
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->var->name on line 552 can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Variable::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
577
                    $compiledExpression->getValue(),
578
                    $compiledExpression->getType(),
579
                    $this->context->getCurrentBranch()
580
                );
581
                $this->context->addVariable($symbol);
582
            }
583
584
            $symbol->incSets();
585
            return $compiledExpression;
586
        }
587
588
        $this->context->debug('Unknown how to pass symbol by ref');
589
        return new CompiledExpression();
590
    }
591
592
    /**
593
     * @param Node\Expr\Variable $expr
594
     * @return CompiledExpression
595
     */
596
    protected function passExprVariable(Node\Expr\Variable $expr)
597
    {
598
        $variable = $this->context->getSymbol($expr->name);
599
        if ($variable) {
600
            $variable->incGets();
601
            return new CompiledExpression($variable->getType(), $variable->getValue(), $variable);
602
        }
603
604
        $this->context->notice(
605
            'undefined-variable',
606
            sprintf('You trying to use undefined variable $%s', $expr->name),
607
            $expr
608
        );
609
610
        return new CompiledExpression();
611
    }
612
613
    /**
614
     * Compile Array_ expression to CompiledExpression
615
     *
616
     * @param Node\Expr\Array_ $expr
617
     * @return CompiledExpression
618
     */
619 11
    protected function getArray(Node\Expr\Array_ $expr)
620
    {
621 11
        if ($expr->items === array()) {
622 8
            return new CompiledExpression(CompiledExpression::ARR, array());
623
        }
624
625 3
        $resultArray = array();
626
627 3
        foreach ($expr->items as $item) {
628 3
            $compiledValueResult = $this->compile($item->value);
629 3
            if ($item->key) {
630 1
                $compiledKeyResult = $this->compile($item->key);
631 1
                switch ($compiledKeyResult->getType()) {
632 1
                    case CompiledExpression::INTEGER:
633 1
                    case CompiledExpression::DOUBLE:
634 1
                    case CompiledExpression::BOOLEAN:
635 1
                    case CompiledExpression::NULL:
636 1
                    case CompiledExpression::STRING:
637 1
                        $resultArray[$compiledKeyResult->getValue()] = $compiledValueResult->getValue();
638 1
                        break;
639
                    default:
640
                        $this->context->debug("Type {$compiledKeyResult->getType()} is not supported for key value");
641
                        return new CompiledExpression(CompiledExpression::ARR);
642
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
643 1
                }
644 1
            } else {
645 2
                $resultArray[] = $compiledValueResult->getValue();
646
            }
647 3
        }
648
649 3
        return new CompiledExpression(CompiledExpression::ARR, $resultArray);
650
    }
651
652
    /**
653
     * Convert const fetch expr to CompiledExpression
654
     *
655
     * @param Node\Expr\ConstFetch $expr
656
     * @return CompiledExpression
657
     */
658
    protected function constFetch(Node\Expr\ConstFetch $expr)
659
    {
660
        if ($expr->name instanceof Node\Name) {
661
            if ($expr->name->parts[0] === 'true') {
662
                return new CompiledExpression(CompiledExpression::BOOLEAN, true);
663
            }
664
665
            if ($expr->name->parts[0] === 'false') {
666
                return new CompiledExpression(CompiledExpression::BOOLEAN, false);
667
            }
668
        }
669
670
        /**
671
         * @todo Implement check
672
         */
673
674
        $expression = new Expression($this->context);
675
        $compiledExpr = $expression->compile($expr->name);
676
677
        return $compiledExpr;
678
    }
679
}
680