1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Zenify |
7
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ZenifyCodingStandard\Sniffs\Commenting; |
11
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
13
|
|
|
use PHP_CodeSniffer_Standards_AbstractVariableSniff; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Rules: |
18
|
|
|
* - Property should have docblock comment (except for {@inheritdoc}). |
19
|
|
|
* |
20
|
|
|
* @see PHP_CodeSniffer_Standards_AbstractVariableSniff is used, because it's very difficult to |
21
|
|
|
* separate properties from variables (in args, method etc.). This class does is for us. |
22
|
|
|
*/ |
23
|
|
|
final class VarPropertyCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param PHP_CodeSniffer_File $file |
28
|
|
|
* @param int $position |
29
|
|
|
*/ |
30
|
1 |
|
protected function processMemberVar(PHP_CodeSniffer_File $file, $position) |
31
|
|
|
{ |
32
|
1 |
|
$commentString = $this->getPropertyComment($file, $position); |
33
|
|
|
|
34
|
1 |
|
if (strpos($commentString, '@var') !== FALSE) { |
35
|
1 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$file->addError('Property should have docblock comment.', $position); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param PHP_CodeSniffer_File $file |
44
|
|
|
* @param int $position |
45
|
|
|
*/ |
46
|
1 |
|
protected function processVariable(PHP_CodeSniffer_File $file, $position) |
47
|
|
|
{ |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param PHP_CodeSniffer_File $file |
53
|
|
|
* @param int $position |
54
|
|
|
*/ |
55
|
|
|
protected function processVariableInString(PHP_CodeSniffer_File $file, $position) |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
private function getPropertyComment(PHP_CodeSniffer_File $file, int $position) : string |
61
|
|
|
{ |
62
|
1 |
|
$commentEnd = $file->findPrevious([T_DOC_COMMENT_CLOSE_TAG], $position); |
63
|
1 |
|
if ($commentEnd === FALSE) { |
64
|
1 |
|
return ''; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$tokens = $file->getTokens(); |
68
|
1 |
|
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { |
69
|
|
|
return ''; |
70
|
|
|
|
71
|
|
|
} else { |
72
|
|
|
// Make sure the comment we have found belongs to us. |
73
|
1 |
|
$commentFor = $file->findNext(T_VARIABLE, $commentEnd + 1); |
74
|
1 |
|
if ($commentFor !== $position) { |
75
|
1 |
|
return ''; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$commentStart = $file->findPrevious(T_DOC_COMMENT_OPEN_TAG, $position); |
80
|
1 |
|
return $file->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.