Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
33 | class InlineCommentSniff implements Sniff |
||
34 | { |
||
35 | /** |
||
36 | * @var int Limit defining long comments. |
||
37 | * Long comments count $longCommentLimit or more lines. |
||
38 | */ |
||
39 | public $longCommentLimit = 5; |
||
40 | |||
41 | /** |
||
42 | * Returns an array of tokens this test wants to listen for. |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public function register() |
||
52 | |||
53 | /** |
||
54 | * Processes this test, when one of its tokens is encountered. |
||
55 | * |
||
56 | * @param File $phpcsFile The current file being scanned. |
||
57 | * @param int $stackPtr The position of the current token |
||
58 | * in the stack passed in $tokens. |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | public function process(File $phpcsFile, $stackPtr) |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start |
||
84 | * with '//'. |
||
85 | * |
||
86 | * @param File $phpcsFile The current file being scanned. |
||
87 | * @param int $stackPtr The position of the current token |
||
88 | * that has to be a comment. |
||
89 | * |
||
90 | * @return bool TRUE if the content of the token pointed by $stackPtr starts |
||
91 | * with //, FALSE if an error was added to $phpcsFile. |
||
92 | */ |
||
93 | private function _checkCommentStyle(File $phpcsFile, $stackPtr) |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Gather into an array all comment lines to which $stackPtr belongs. |
||
117 | * |
||
118 | * @param File $phpcsFile The current file being scanned. |
||
119 | * @param int $stackPtr Pointer to the first comment line. |
||
120 | * |
||
121 | * @return type array Pointers to tokens making up the comment block. |
||
|
|||
122 | */ |
||
123 | private function _getCommentBlock(File $phpcsFile, $stackPtr) |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
||
146 | * |
||
147 | * @param File $phpcsFile The current file being scanned. |
||
148 | * @param array $commentLines Lines of the comment block being checked. |
||
149 | * |
||
150 | * @return bool TRUE if $commentLines is enclosed with at least a blank line |
||
151 | * before and after, FALSE otherwise. |
||
152 | */ |
||
153 | private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines) |
||
184 | |||
185 | }//end class |
||
186 | |||
187 | ?> |
||
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.