|
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") |
|
|
|
|
|
|
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
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: