Completed
Pull Request — master (#325)
by Enrico
02:57
created

FinalStaticUsage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 86.96%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 63
ccs 20
cts 23
cp 0.8696
rs 10
wmc 8
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegister() 0 6 1
A getMetadata() 0 7 1
B pass() 0 28 6
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Expression;
4
5
use PhpParser\Node\Expr;
6
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
7
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
8
use PHPSA\Context;
9
use PHPSA\Definition\ClassDefinition;
10
11
class FinalStaticUsage implements AnalyzerPassInterface
12
{
13
    use DefaultMetadataPassTrait {
14
        DefaultMetadataPassTrait::getMetadata as defaultMetadata;
15
    }
16
17
    const DESCRIPTION = 'Checks for use of `static::` inside a final class.';
18
19
    /**
20
     * @param Expr\StaticCall $expr
21
     * @param Context $context
22
     * @return bool
23
     */
24 3
    public function pass(Expr\StaticCall $expr, Context $context)
25
    {
26 3
        if (!$context->scope) {
27
            return false;
28
        }
29
30 3
        $scopePointer = $context->scope->getPointer();
31 3
        if (!$scopePointer) {
32
            return false;
33
        }
34
35 3
        $classObject = $scopePointer->getObject();
36 3
        if (!$classObject instanceof ClassDefinition || !$classObject->isFinal()) {
37 2
            return false;
38
        }
39
40 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...
41
            return false;
42
        }
43
44 1
        $context->notice(
45 1
            'error.final-static-usage',
46 1
            'Don\'t use static:: in final class',
47
            $expr
48 1
        );
49
50 1
        return true;
51
    }
52
53
    /**
54
     * @return array
55
     */
56 2
    public function getRegister()
57
    {
58
        return [
59 2
            Expr\StaticCall::class,
60 2
        ];
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 53
    public static function getMetadata()
67
    {
68 53
        $metadata = self::defaultMetadata();
69 53
        $metadata->setRequiredPhpVersion('5.3'); //static:: since PHP 5.3
70
71 53
        return $metadata;
72
    }
73
}
74