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