Conditions | 25 |
Paths | 37 |
Total Lines | 96 |
Code Lines | 58 |
Lines | 10 |
Ratio | 10.42 % |
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 |
||
86 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
87 | { |
||
88 | $tokens = $phpcsFile->getTokens(); |
||
89 | $tokenCode = $tokens[$stackPtr]['code']; |
||
90 | $tokenContentLc = strtolower($tokens[$stackPtr]['content']); |
||
91 | |||
92 | // For string tokens we only care about 'trait' and 'namespace' as those are |
||
93 | // the only ones which may not be correctly recognized as it's own token. |
||
94 | // This only happens in older versions of PHP where the token doesn't exist yet as a keyword. |
||
95 | if ($tokenCode === T_STRING && ($tokenContentLc !== 'trait' && $tokenContentLc !== 'namespace')) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | if (in_array($tokenCode, array(T_CLASS, T_INTERFACE, T_TRAIT), true)) { |
||
100 | // Check for the declared name being a name which is not tokenized as T_STRING. |
||
101 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
102 | if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
||
103 | $name = $tokens[$nextNonEmpty]['content']; |
||
104 | } else { |
||
105 | // Get the declared name if it's a T_STRING. |
||
106 | $name = $phpcsFile->getDeclarationName($stackPtr); |
||
107 | } |
||
108 | unset($nextNonEmpty); |
||
109 | |||
110 | if (isset($name) === false || is_string($name) === false || $name === '') { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | $nameLc = strtolower($name); |
||
115 | if (isset($this->forbiddenNames[$nameLc]) === false) { |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | } else if ($tokenCode === T_NAMESPACE) { |
||
120 | $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr); |
||
121 | |||
122 | if ($namespaceName === false || $namespaceName === '') { |
||
123 | return; |
||
124 | } |
||
125 | |||
126 | $namespaceParts = explode('\\', $namespaceName); |
||
127 | foreach ($namespaceParts as $namespacePart) { |
||
128 | $partLc = strtolower($namespacePart); |
||
129 | if (isset($this->forbiddenNames[$partLc]) === true) { |
||
130 | $name = $namespacePart; |
||
131 | $nameLc = $partLc; |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | } else if ($tokenCode === T_STRING) { |
||
136 | // Traits and namespaces which are not yet tokenized as T_TRAIT/T_NAMESPACE. |
||
137 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
138 | if ($nextNonEmpty === false) { |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | $nextNonEmptyCode = $tokens[$nextNonEmpty]['code']; |
||
143 | |||
144 | if ($nextNonEmptyCode !== T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) { |
||
145 | $name = $tokens[$nextNonEmpty]['content']; |
||
146 | $nameLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
147 | } else if ($nextNonEmptyCode === T_STRING) { |
||
148 | $endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1)); |
||
149 | |||
150 | do { |
||
151 | $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
152 | |||
153 | if (isset($this->forbiddenNames[$nextNonEmptyLc]) === true) { |
||
154 | $name = $tokens[$nextNonEmpty]['content']; |
||
155 | $nameLc = $nextNonEmptyLc; |
||
156 | break; |
||
157 | } |
||
158 | |||
159 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true); |
||
160 | } while ($nextNonEmpty !== false); |
||
161 | } |
||
162 | unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement); |
||
163 | } |
||
164 | |||
165 | if (isset($name, $nameLc) === false) { |
||
166 | return; |
||
167 | } |
||
168 | |||
169 | // Still here, so this is one of the reserved words. |
||
170 | $version = $this->forbiddenNames[$nameLc]; |
||
171 | View Code Duplication | if ($this->supportsAbove($version) === true) { |
|
1 ignored issue
–
show
|
|||
172 | $error = "'%s' is a reserved keyword as of PHP version %s and cannot be used to name a class, interface or trait or as part of a namespace (%s)"; |
||
173 | $data = array( |
||
174 | $nameLc, |
||
175 | $version, |
||
176 | $tokens[$stackPtr]['type'], |
||
177 | ); |
||
178 | |||
179 | $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
||
180 | } |
||
181 | }//end process() |
||
182 | |||
185 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.