Completed
Pull Request — master (#116)
by Enrico
04:25
created

Casts   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegister() 0 12 1
C pass() 0 49 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
21 1
        $castType = CompiledExpression::UNKNOWN;
22
23 1
        switch (get_class($expr)) {
24 1
            case Expr\Cast\Array_::class:
25 1
                $castType = CompiledExpression::ARR;
26 1
                break;
27 1
            case Expr\Cast\Bool_::class:
28 1
                $castType = CompiledExpression::BOOLEAN;
29 1
                break;
30 1
            case Expr\Cast\Int_::class:
31 1
                $castType = CompiledExpression::INTEGER;
32 1
                break;
33 1
            case Expr\Cast\Double::class:
34 1
                $castType = CompiledExpression::DOUBLE;
35 1
                break;
36 1
            case Expr\Cast\Object_::class:
37 1
                $castType = CompiledExpression::OBJECT;
38 1
                break;
39 1
            case Expr\Cast\String_::class:
40 1
                $castType = CompiledExpression::STRING;
41 1
                break;
42 1
        }
43
44 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...
45 1
        $ExprType = $compiledExpression->getType();
46 1
        $typeName = $compiledExpression->getTypeName();
47
        
48 1
        if ($castType === $ExprType) {
49 1
            $context->notice(
50 1
                'stupid.cast',
51 1
                sprintf("You are trying to cast '%s' to '%s'.", $typeName, $typeName),
52
                $expr
53 1
            );
54 1
            return true;
55 1
        } elseif (get_class($expr) == Expr\Cast\Unset_::class && $ExprType === CompiledExpression::NULL) {
56 1
            $context->notice(
57 1
                'stupid.cast',
58 1
                "You are trying to cast 'unset' to 'null'.",
59
                $expr
60 1
            );
61 1
            return true;
62
        }
63
        
64
65 1
        return false;
66
    }
67
68
    /**
69
     * @return TreeBuilder
70
     */
71 1
    public function getRegister()
72
    {
73
        return [
74 1
            Expr\Cast\Array_::class,
75 1
            Expr\Cast\Bool_::class,
76 1
            Expr\Cast\Int_::class,
77 1
            Expr\Cast\Double::class,
78 1
            Expr\Cast\Object_::class,
79 1
            Expr\Cast\String_::class,
80 1
            Expr\Cast\Unset_::class,
81 1
        ];
82
    }
83
}
84