PropertyDefinitionDefaultValue::pass()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 12
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Stmt;
6
use PhpParser\Node;
7
use PhpParser\Node\Expr;
8
use PhpParser\Node\Stmt\PropertyProperty;
9
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
use PHPSA\CompiledExpression;
13
14
class PropertyDefinitionDefaultValue implements Pass\AnalyzerPassInterface
15
{
16
    use DefaultMetadataPassTrait;
17
18
    const DESCRIPTION = 'Checks if any Property Definition is done with a default null value (not needed). For example: `$a = null`';
19
20
    /**
21
     * @param $stmt
22
     * @param Context $context
23
     * @return bool
24
     */
25
    public function pass($stmt, Context $context)
26
    {
27
        if ($stmt->default instanceof Node\Expr) {
28
            $compiled = $context->getExpressionCompiler()->compile($stmt->default);
29
            if ($compiled->getType() == CompiledExpression::NULL) {
30
                $context->notice(
31
                    'property_definition_default_value',
32
                    'null is default and is not needed.',
33
                    $stmt
34
                );
35
                return true;
36
            }
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getRegister()
46
    {
47
        return [
48
            PropertyProperty::class,
49
        ];
50
    }
51
}
52