Completed
Pull Request — master (#258)
by Enrico
10:11
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 1
        if ($config["class_method"] == true) {
29 1
            $this->register[] = Stmt\ClassMethod::class;
30 1
        }
31 1
        if ($config["class_const"] == true) {
32 1
            $this->register[] = Stmt\ClassConst::class;
33 1
        }
34 1
        if ($config["class_property"] == true) {
35 1
            $this->register[] = Stmt\Property::class;
36 1
        }
37 1
        if ($config["function"] == true) {
38 1
            $this->register[] = Stmt\Function_::class;
39 1
        }
40 1
        if ($config["interface"] == true) {
41 1
            $this->register[] = Stmt\Interface_::class;
42 1
        }
43 1
        if ($config["trait"] == true) {
44 1
            $this->register[] = Stmt\Trait_::class;
45 1
        }
46 1
    }
47
48
    /**
49
     * @param Stmt $stmt
50
     * @param Context $context
51
     * @return bool
52
     */
53 10
    public function pass(Stmt $stmt, Context $context)
54
    {
55 10
        if ($stmt->getDocComment() === null) {
56 10
            $context->notice(
57 10
                'missing_docblock',
58 10
                'Missing Docblock',
59
                $stmt
60 10
            );
61
62 10
            return true;
63
        }
64
        
65 3
        return false;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 1
    public function getRegister()
72
    {
73 1
        return $this->register;
74
    }
75
76
    /**
77
     * @return Metadata
78
     */
79 43
    public static function getMetadata()
80
    {
81 43
        $treebuilder = new TreeBuilder();
82 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...
83 43
            ->info(self::DESCRIPTION)
84 43
            ->canBeDisabled()
85 43
            ->children()
86 43
                ->booleanNode("class")->defaultTrue()->end()
87 43
                ->booleanNode("class_method")->defaultTrue()->end()
88 43
                ->booleanNode("class_const")->defaultTrue()->end()
89 43
                ->booleanNode("class_property")->defaultTrue()->end()
90 43
                ->booleanNode("function")->defaultTrue()->end()
91 43
                ->booleanNode("interface")->defaultTrue()->end()
92 43
                ->booleanNode("trait")->defaultTrue()->end()
93 43
            ->end();
94
95 43
        return new Metadata("missing_docblock", $config, self::DESCRIPTION);
96
    }
97
}
98