Completed
Pull Request — master (#287)
by algo13
02:34
created

PropertyDefinitionDefaultValue::getRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 1
    public function pass($stmt, Context $context)
26
    {
27 1
        if ($stmt->default instanceof Node\Expr) {
28 1
            $compiled = $context->getExpressionCompiler()->compile($stmt->default);
29 1
            if ($compiled->getType() == CompiledExpression::NULL) {
30 1
                $context->notice(
31 1
                    'property_definition_default_value',
32 1
                    'null is default and is not needed.',
33
                    $stmt
34 1
                );
35 1
                return true;
36
            }
37
        }
38 1
        return false;
39
    }
40
41
    /**
42
     * @return array
43
     */
44 1
    public function getRegister()
45
    {
46
        return [
47 1
            PropertyProperty::class,
48 1
        ];
49
    }
50
}
51