Conditions | 11 |
Paths | 23 |
Total Lines | 50 |
Code Lines | 31 |
Lines | 24 |
Ratio | 48 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
73 | public function process(File $phpcsFile, $stackPtr) |
||
74 | { |
||
75 | // We are only interested if this is the first open tag. |
||
76 | View Code Duplication | if ($stackPtr !== 0) { |
|
|
|||
77 | if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
||
78 | return; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $filePath = $phpcsFile->getFilename(); |
||
83 | $tokens = $phpcsFile->getTokens(); |
||
84 | // removes the application root from the beginning of the file path |
||
85 | $locationPath = self::_getLocationPath($filePath, $this->_getAppRoot()); |
||
86 | // add an error, if application root doesn't exist in current file path |
||
87 | if (false === $locationPath) { |
||
88 | $error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.'; |
||
89 | $phpcsFile->addError($error, count($tokens) - 1); |
||
90 | return; |
||
91 | } |
||
92 | // generates the expected comment |
||
93 | $commentTemplate = "Location: $locationPath"; |
||
94 | |||
95 | $currentToken = count($tokens) - 1; |
||
96 | $hasClosingLocationComment = false; |
||
97 | $isNotAWhitespaceOrAComment = false; |
||
98 | View Code Duplication | while ($currentToken >= 0 |
|
99 | && ! $isNotAWhitespaceOrAComment |
||
100 | && ! $hasClosingLocationComment |
||
101 | ) { |
||
102 | $token = $tokens[$currentToken]; |
||
103 | $tokenCode = $token['code']; |
||
104 | if (T_COMMENT === $tokenCode) { |
||
105 | $commentString = self::_getCommentContent($token['content']); |
||
106 | if (0 === strcmp($commentString, $commentTemplate)) { |
||
107 | $hasClosingLocationComment = true; |
||
108 | } |
||
109 | } else if (T_WHITESPACE === $tokenCode) { |
||
110 | // Whitespaces are allowed between the closing file comment, |
||
111 | //other comments and end of file |
||
112 | } else { |
||
113 | $isNotAWhitespaceOrAComment = true; |
||
114 | } |
||
115 | $currentToken--; |
||
116 | } |
||
117 | |||
118 | if ( ! $hasClosingLocationComment) { |
||
119 | $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
||
120 | $phpcsFile->addError($error, $currentToken); |
||
121 | } |
||
122 | }//end process() |
||
123 | |||
183 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.