Conditions | 23 |
Paths | 5833 |
Total Lines | 100 |
Code Lines | 62 |
Lines | 46 |
Ratio | 46 % |
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 |
||
49 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
||
50 | if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $tokens = $phpcsFile->getTokens(); |
||
55 | |||
56 | // One space after the use keyword. |
||
57 | View Code Duplication | if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
|
|
|||
58 | $error = 'There must be a single space after the USE keyword'; |
||
59 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); |
||
60 | if ($fix === true) { |
||
61 | $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | // Namespaces in use statements must not have a leading separator |
||
66 | $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
67 | if ($tokens[$next]['code'] === T_NS_SEPARATOR) { |
||
68 | $error = 'Namespaces in use statements should not start with a namespace separator'; |
||
69 | $fix = $phpcsFile->addFixableError($error, $next, 'NamespaceStart'); |
||
70 | if ($fix) { |
||
71 | $phpcsFile->fixer->replaceToken($next, ''); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // Only one USE declaration allowed per statement. |
||
76 | $next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON], ($stackPtr + 1)); |
||
77 | if ($tokens[$next]['code'] === T_COMMA) { |
||
78 | $error = 'There must be one USE keyword per declaration'; |
||
79 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); |
||
80 | if ($fix === true) { |
||
81 | $phpcsFile->fixer->replaceToken($next, ';' . $phpcsFile->eolChar . 'use '); |
||
82 | } |
||
83 | } else { |
||
84 | $nextUse = $phpcsFile->findNext(T_USE, $next + 1); |
||
85 | if ($nextUse && !$this->shouldIgnoreUse($phpcsFile, $nextUse)) { |
||
86 | if ($tokens[$nextUse]['line'] > $tokens[$next]['line'] + 1) { |
||
87 | $error = 'There should not be newlines between use statements'; |
||
88 | $fix = $phpcsFile->addFixableError($error, $nextUse, 'NewlineBetweenUse'); |
||
89 | if ($fix) { |
||
90 | $phpcsFile->fixer->replaceToken($nextUse - 1, ''); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // Make sure this USE comes after the first namespace declaration. |
||
97 | $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
||
98 | View Code Duplication | if ($prev !== false) { |
|
99 | $first = $phpcsFile->findNext(T_NAMESPACE, 1); |
||
100 | if ($prev !== $first) { |
||
101 | $error = 'USE declarations must go after the first namespace declaration'; |
||
102 | $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // Only interested in the last USE statement from here onwards. |
||
107 | $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); |
||
108 | View Code Duplication | while ($this->shouldIgnoreUse($phpcsFile, $nextUse)) { |
|
109 | $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); |
||
110 | if ($nextUse === false) { |
||
111 | break; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if ($nextUse !== false) { |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
||
120 | $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
||
121 | $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); |
||
122 | View Code Duplication | if ($diff !== 1) { |
|
123 | if ($diff < 0) { |
||
124 | $diff = 0; |
||
125 | } |
||
126 | |||
127 | $error = 'There must be one blank line after the last USE statement; %s found;'; |
||
128 | $data = [$diff]; |
||
129 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); |
||
130 | if ($fix === true) { |
||
131 | if ($diff === 0) { |
||
132 | $phpcsFile->fixer->addNewline($end); |
||
133 | } else { |
||
134 | $phpcsFile->fixer->beginChangeset(); |
||
135 | for ($i = ($end + 1); $i < $next; $i++) { |
||
136 | if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
||
137 | break; |
||
138 | } |
||
139 | |||
140 | $phpcsFile->fixer->replaceToken($i, ''); |
||
141 | } |
||
142 | |||
143 | $phpcsFile->fixer->addNewline($end); |
||
144 | $phpcsFile->fixer->endChangeset(); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
151 |
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.