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

Casts   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 71
ccs 47
cts 47
cp 1
rs 10
wmc 11
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegister() 0 12 1
C pass() 0 47 10
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