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

FinalStaticUsage::pass()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.2373

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 28
ccs 13
cts 16
cp 0.8125
crap 6.2373
rs 8.439
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