Complex classes like FunctionCommentSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FunctionCommentSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class CodingStandard_Sniffs_Commenting_FunctionCommentSniff extends Squiz_Sniffs_Commenting_FunctionCommentSniff |
||
35 | { |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Processes this test, when one of its tokens is encountered. |
||
40 | * |
||
41 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
42 | * @param int $stackPtr The position of the current token |
||
43 | * in the stack passed in $tokens. |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | 1 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
75 | |||
76 | /** |
||
77 | * Process the function parameter comments. |
||
78 | * |
||
79 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
80 | * @param int $stackPtr The position of the current token |
||
81 | * in the stack passed in $tokens. |
||
82 | * @param int $commentStart The position in the stack where the comment started. |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | 1 | protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
95 | |||
96 | |||
97 | /** |
||
98 | * Process the return comment of this function comment. |
||
99 | * |
||
100 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
101 | * @param int $stackPtr The position of the current token |
||
102 | * in the stack passed in $tokens. |
||
103 | * @param int $commentStart The position in the stack where the comment started. |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 1 | protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
135 | |||
136 | /** |
||
137 | * Process the short description of a function comment. |
||
138 | * |
||
139 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
140 | * @param int $stackPtr The position of the function token |
||
141 | * in the stack passed in $tokens. |
||
142 | * @param string $short The content of the short description. |
||
143 | * @param int $errorPos The position where an error should be thrown. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 1 | public function checkShort(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $short, $errorPos) |
|
179 | |||
180 | |||
181 | /** |
||
182 | * Process any throw tags that this function comment has. |
||
183 | * |
||
184 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
185 | * @param int $stackPtr The position of the current token |
||
186 | * in the stack passed in $tokens. |
||
187 | * @param int $commentStart The position in the stack where the comment started. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | 1 | protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
244 | |||
245 | |||
246 | /** |
||
247 | * Is the comment an inheritdoc? |
||
248 | * |
||
249 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
250 | * @param int $commentStart The position in the stack where the comment started. |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | 1 | protected function isInheritDoc(PHP_CodeSniffer_File $phpcsFile, $commentStart) |
|
267 | |||
268 | }//end class |
||
269 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.