Completed
Pull Request — master (#258)
by Enrico
08:55
created

MissingDocblock::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
ccs 15
cts 15
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Stmt;
6
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
7
use PHPSA\Context;
8
use PHPSA\Analyzer\Pass\Metadata;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
11
class MissingDocblock implements AnalyzerPassInterface
12
{
13
    const DESCRIPTION = 'Checks for a missing docblock for: class, property, class constant, trait, interface, class method, function.';
14
15
    /**
16
     * Contains the Nodes that are registered
17
     */
18
    public $register = [];
19
20
    /**
21
     * @param array $config The config values for the analyzer
22
     */
23 1
    public function __construct(array $config)
24
    {
25 1
        if ($config["class"] == true) {
26 1
            $this->register[] = Stmt\Class_::class;
27 1
        }
28
29 1
        if ($config["class_method"] == true) {
30 1
            $this->register[] = Stmt\ClassMethod::class;
31 1
        }
32
33 1
        if ($config["class_const"] == true) {
34 1
            $this->register[] = Stmt\ClassConst::class;
35 1
        }
36
37 1
        if ($config["class_property"] == true) {
38 1
            $this->register[] = Stmt\Property::class;
39 1
        }
40
41 1
        if ($config["function"] == true) {
42 1
            $this->register[] = Stmt\Function_::class;
43 1
        }
44
45 1
        if ($config["interface"] == true) {
46 1
            $this->register[] = Stmt\Interface_::class;
47 1
        }
48
        
49 1
        if ($config["trait"] == true) {
50 1
            $this->register[] = Stmt\Trait_::class;
51 1
        }
52 1
    }
53
54
    /**
55
     * @param Stmt $stmt
56
     * @param Context $context
57
     * @return bool
58
     */
59 10
    public function pass(Stmt $stmt, Context $context)
60
    {
61 10
        if ($stmt->getDocComment() === null) {
62 10
            $context->notice(
63 10
                'missing_docblock',
64 10
                'Missing Docblock',
65
                $stmt
66 10
            );
67
68 10
            return true;
69
        }
70
        
71 3
        return false;
72
    }
73
74
    /**
75
     * @return array
76
     */
77 1
    public function getRegister()
78
    {
79 1
        return $this->register;
80
    }
81
82
    /**
83
     * @return Metadata
84
     */
85 43
    public static function getMetadata()
86
    {
87 43
        $treebuilder = new TreeBuilder();
88 43
        $config = $treebuilder->root("missing_docblock")
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method canBeDisabled() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. 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...
89 43
            ->info(self::DESCRIPTION)
90 43
            ->canBeDisabled()
91 43
            ->children()
92 43
                ->booleanNode("class")->defaultTrue()->end()
93 43
                ->booleanNode("class_method")->defaultTrue()->end()
94 43
                ->booleanNode("class_const")->defaultTrue()->end()
95 43
                ->booleanNode("class_property")->defaultTrue()->end()
96 43
                ->booleanNode("function")->defaultTrue()->end()
97 43
                ->booleanNode("interface")->defaultTrue()->end()
98 43
                ->booleanNode("trait")->defaultTrue()->end()
99 43
            ->end();
100
101 43
        return new Metadata("missing_docblock", $config, self::DESCRIPTION);
102
    }
103
}
104