Completed
Push — master ( a323dc...8dfe2a )
by Enrico
04:19
created

FinalStaticUsage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 12
cts 14
cp 0.8571
rs 10
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 19 4
A getRegister() 0 6 1
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 PHPSA\Definition\ClassDefinition;
9
10
class FinalStaticUsage implements AnalyzerPassInterface
11
{
12
    /**
13
     * @param Expr\StaticCall $expr
14
     * @param Context $context
15
     * @return bool
16
     */
17 1
    public function pass(Expr\StaticCall $expr, Context $context)
18
    {
19 1
        $classObject = $context->scope->getPointer()->getObject();
20 1
        if (!$classObject instanceof ClassDefinition || !$classObject->isFinal()) {
21
            return false;
22
        }
23
24 1
        if ($expr->class->getFirst() !== 'static') {
0 ignored issues
show
Bug introduced by
The method getFirst does only exist in PhpParser\Node\Name, but not in PhpParser\Node\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
25
            return false;
26
        }
27
28 1
        $context->notice(
29 1
            'error.final-static-usage',
30 1
            'Don\'t use static:: in final class',
31
            $expr
32 1
        );
33
34 1
        return true;
35
    }
36
37
    /**
38
     * @return array
39
     */
40 1
    public function getRegister()
41
    {
42
        return [
43 1
            Expr\StaticCall::class,
44 1
        ];
45
    }
46
}
47