Completed
Pull Request — master (#116)
by Enrico
03:35
created

Casts::pass()   C

Complexity

Conditions 10
Paths 21

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 10.0018

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 37
nc 21
nop 2
dl 0
loc 49
ccs 37
cts 38
cp 0.9737
crap 10.0018
rs 5.5471
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 (true) {
24 1
            case ($expr instanceof Expr\Cast\Array_):
25 1
                $castType = CompiledExpression::ARR;
26 1
                break;
27 1
            case ($expr instanceof Expr\Cast\Bool_):
28 1
                $castType = CompiledExpression::BOOLEAN;
29 1
                break;
30 1
            case ($expr instanceof Expr\Cast\Int_):
31 1
                $castType = CompiledExpression::INTEGER;
32 1
                break;
33 1
            case ($expr instanceof Expr\Cast\Double):
34 1
                $castType = CompiledExpression::DOUBLE;
35 1
                break;
36 1
            case ($expr instanceof Expr\Cast\Object_):
37 1
                $castType = CompiledExpression::OBJECT;
38 1
                break;
39 1
            case ($expr instanceof Expr\Cast\String_):
40 1
                $castType = CompiledExpression::STRING;
41 1
                break;
42
        }
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
                'You are trying to cast \''.$typeName.'\' to \''.$typeName.'\'.',
52
                $expr
53 1
            );
54 1
            return true;
55 1
        } elseif ($expr instanceof Expr\Cast\Unset_ && $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