Completed
Pull Request — master (#134)
by Enrico
04:05
created

MissingVisibility::pass()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 4
nop 2
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 6
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Stmt\Property;
6
use PhpParser\Node\Stmt\ClassMethod;
7
use PhpParser\Node\Stmt\Class_;
8
use PhpParser\Node\Stmt;
9
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
10
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
11
use PHPSA\Context;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
14
class MissingVisibility implements ConfigurablePassInterface, AnalyzerPassInterface
15
{
16
    /**
17
     * @param Stmt $stmt
18
     * @param Context $context
19
     * @return bool
20
     */
21 24
    public function pass(Stmt $stmt, Context $context)
22
    {
23
        // if it is private, protected or public return false
24 24
        if ($stmt->isPrivate() || $stmt->isProtected() || ($stmt->type & Class_::MODIFIER_PUBLIC) !== 0) {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in PhpParser\Node\Stmt.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpParser\Node\Stmt as the method isPrivate() does only exist in the following sub-classes of PhpParser\Node\Stmt: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpParser\Node\Stmt as the method isProtected() does only exist in the following sub-classes of PhpParser\Node\Stmt: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
25 24
            return false;
26
        }
27
28 1
        if ($stmt instanceof Property) {
29 1
            $context->notice(
30 1
                'missing_visibility',
31 1
                'Class property was defined with the deprecated var keyword. Use a visibility modifier instead.',
32
                $stmt
33 1
            );
34 1
        } elseif ($stmt instanceof ClassMethod) {
35 1
            $context->notice(
36 1
                'missing_visibility',
37 1
                'Class method was defined without a visibility modifier.',
38
                $stmt
39 1
            );
40 1
        }
41
42 1
        return true;
43
    }
44
45
    /**
46
     * @return TreeBuilder
47
     */
48
    public function getConfiguration()
49
    {
50
        $treeBuilder = new TreeBuilder();
51
        $treeBuilder->root('missing_visibility')
52
            ->canBeDisabled()
53
        ;
54
55
        return $treeBuilder;
56
    }
57
58
    /**
59
     * @return array
60
     */
61 1
    public function getRegister()
62
    {
63
        return [
64 1
            Property::class,
65
            ClassMethod::class
66 1
        ];
67
    }
68
}
69