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") |
|
|
|
|
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
|
|
|
|
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: