Completed
Push — master ( cf2c14...d3a74f )
by Дмитрий
10s
created

Casts::getRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Expression;
4
5
use PhpParser\Node\Expr;
6
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
7
use PHPSA\Context;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use PHPSA\CompiledExpression;
10
11
class Casts implements AnalyzerPassInterface
12
{
13
    /**
14
     * @param Expr $expr
15
     * @param Context $context
16
     * @return bool
17
     */
18 1
    public function pass(Expr $expr, Context $context)
19
    {
20 1
        $castType = CompiledExpression::UNKNOWN;
21
22 1
        switch (get_class($expr)) {
23 1
            case Expr\Cast\Array_::class:
24 1
                $castType = CompiledExpression::ARR;
25 1
                break;
26 1
            case Expr\Cast\Bool_::class:
27 1
                $castType = CompiledExpression::BOOLEAN;
28 1
                break;
29 1
            case Expr\Cast\Int_::class:
30 1
                $castType = CompiledExpression::INTEGER;
31 1
                break;
32 1
            case Expr\Cast\Double::class:
33 1
                $castType = CompiledExpression::DOUBLE;
34 1
                break;
35 1
            case Expr\Cast\Object_::class:
36 1
                $castType = CompiledExpression::OBJECT;
37 1
                break;
38 1
            case Expr\Cast\String_::class:
39 1
                $castType = CompiledExpression::STRING;
40 1
                break;
41 1
        }
42
43 1
        $compiledExpression = $context->getExpressionCompiler()->compile($expr->expr);
0 ignored issues
show
Bug introduced by
The property expr does not seem to exist in PhpParser\Node\Expr.

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...
44 1
        $exprType = $compiledExpression->getType();
45 1
        $typeName = $compiledExpression->getTypeName();
46
        
47 1
        if ($castType === $exprType) {
48 1
            $context->notice(
49 1
                'stupid.cast',
50 1
                sprintf("You are trying to cast '%s' to '%s'.", $typeName, $typeName),
51
                $expr
52 1
            );
53 1
            return true;
54 1
        } elseif (get_class($expr) == Expr\Cast\Unset_::class && $exprType === CompiledExpression::NULL) {
55 1
            $context->notice(
56 1
                'stupid.cast',
57 1
                "You are trying to cast 'null' to 'unset' (null).",
58
                $expr
59 1
            );
60 1
            return true;
61
        }
62
        
63 1
        return false;
64
    }
65
66
    /**
67
     * @return TreeBuilder
68
     */
69 1
    public function getRegister()
70
    {
71
        return [
72 1
            Expr\Cast\Array_::class,
73 1
            Expr\Cast\Bool_::class,
74 1
            Expr\Cast\Int_::class,
75 1
            Expr\Cast\Double::class,
76 1
            Expr\Cast\Object_::class,
77 1
            Expr\Cast\String_::class,
78 1
            Expr\Cast\Unset_::class,
79 1
        ];
80
    }
81
}
82