PropertyDefinitionDefaultValue   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 16 3
A getRegister() 0 6 1
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