Conditions | 7 |
Paths | 5 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
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 | private function searchEmptyLines(File $phpcsFile, int $searchPosition): void |
||
74 | { |
||
75 | $endOfDoc = $phpcsFile->findEndOfStatement($searchPosition); |
||
76 | |||
77 | do { |
||
78 | $currentToken = $phpcsFile->getTokens()[$searchPosition]; |
||
79 | $nextTokenPosition = (int) $phpcsFile->findNext( |
||
80 | [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], |
||
81 | $searchPosition + 1, |
||
82 | $endOfDoc, |
||
83 | true |
||
84 | ); |
||
85 | |||
86 | if ($hasToken = ($nextTokenPosition > 0)) { |
||
87 | $nextToken = $phpcsFile->getTokens()[$nextTokenPosition]; |
||
88 | $hasTooManyLines = ($nextToken['line'] - $currentToken['line']) > 2; |
||
89 | |||
90 | if ($hasTooManyLines) { |
||
91 | $isFixing = $phpcsFile->addFixableError( |
||
92 | self::ERROR_EMPTY_LINES_FOUND, |
||
93 | $nextTokenPosition, |
||
94 | self::CODE_EMPTY_LINES_FOUND |
||
95 | ); |
||
96 | |||
97 | if ($isFixing) { |
||
98 | $movement = 2; |
||
99 | |||
100 | if ($nextToken['type'] === 'T_DOC_COMMENT_CLOSE_TAG') { |
||
101 | $movement = 1; |
||
102 | } |
||
103 | |||
104 | $phpcsFile->fixer->beginChangeset(); |
||
105 | |||
106 | (new LineHelper($this->file))->removeLines( |
||
107 | $currentToken['line'] + $movement, |
||
108 | $nextToken['line'] - 1 |
||
109 | ); |
||
110 | |||
111 | $phpcsFile->fixer->endChangeset(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $phpcsFile->recordMetric( |
||
116 | $searchPosition, |
||
117 | 'DocBlock has too many lines', |
||
118 | $hasTooManyLines ? 'yes' : 'no' |
||
119 | ); |
||
120 | |||
121 | $searchPosition = $nextTokenPosition; |
||
122 | |||
123 | } |
||
124 | } while ($hasToken); |
||
125 | } |
||
126 | } |
||
127 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: