| Conditions | 10 |
| Paths | 4 |
| Total Lines | 123 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 78 | public function process(File $phpcsFile, $stackPtr): void |
||
| 79 | { |
||
| 80 | $docCommentTags = [ |
||
| 81 | '@param' => 1, |
||
| 82 | '@return' => 1, |
||
| 83 | '@throws' => 1, |
||
| 84 | '@var' => 2, |
||
| 85 | ]; |
||
| 86 | $scanTokens = [ |
||
| 87 | T_NS_SEPARATOR, |
||
| 88 | T_DOC_COMMENT_OPEN_TAG, |
||
| 89 | ]; |
||
| 90 | |||
| 91 | $tokens = $phpcsFile->getTokens(); |
||
| 92 | $useStatements = $this->getUseStatements($phpcsFile, 0, ($stackPtr - 1)); |
||
| 93 | $namespace = $this->getNamespace($phpcsFile, 0, ($stackPtr - 1)); |
||
| 94 | |||
| 95 | $nsSep = $phpcsFile->findNext($scanTokens, ($stackPtr + 1)); |
||
| 96 | |||
| 97 | while (false !== $nsSep) { |
||
| 98 | $classNameEnd = (int) $phpcsFile->findNext( |
||
| 99 | $this->classNameTokens, |
||
| 100 | $nsSep, |
||
| 101 | null, |
||
| 102 | true |
||
| 103 | ); |
||
| 104 | |||
| 105 | if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) { |
||
| 106 | if (T_STRING === $tokens[($nsSep - 1)]['code']) { |
||
| 107 | --$nsSep; |
||
| 108 | } |
||
| 109 | |||
| 110 | $className = $phpcsFile->getTokensAsString( |
||
| 111 | $nsSep, |
||
| 112 | ($classNameEnd - $nsSep) |
||
| 113 | ); |
||
| 114 | |||
| 115 | $this->checkShorthandPossible( |
||
| 116 | $phpcsFile, |
||
| 117 | $useStatements, |
||
| 118 | $className, |
||
| 119 | $namespace, |
||
| 120 | $nsSep, |
||
| 121 | ($classNameEnd - 1) |
||
| 122 | ); |
||
| 123 | } else { |
||
| 124 | // Doc comment block. |
||
| 125 | foreach ($tokens[$nsSep]['comment_tags'] as $tag) { |
||
| 126 | $content = $tokens[$tag]['content']; |
||
| 127 | |||
| 128 | if (false === \array_key_exists($content, $docCommentTags)) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $next = ($tag + 1); |
||
| 133 | // PHP Code Sniffer will magically add T_DOC_COMMENT_CLOSE_TAG with empty string content. |
||
| 134 | $lineEnd = $phpcsFile->findNext( |
||
| 135 | [ |
||
| 136 | T_DOC_COMMENT_CLOSE_TAG, |
||
| 137 | T_DOC_COMMENT_STAR, |
||
| 138 | ], |
||
| 139 | $next |
||
| 140 | ); |
||
| 141 | |||
| 142 | $docCommentStringPtr = $phpcsFile->findNext( |
||
| 143 | [T_DOC_COMMENT_STRING], |
||
| 144 | $next, |
||
| 145 | (int) $lineEnd |
||
| 146 | ); |
||
| 147 | |||
| 148 | if (false === $docCommentStringPtr) { |
||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | $docLine = $tokens[$docCommentStringPtr]['content']; |
||
| 153 | |||
| 154 | $docLineTokens = PregLibrary::MO4PregSplit( |
||
| 155 | '/\s+/', |
||
| 156 | $docLine, |
||
| 157 | -1, |
||
| 158 | PREG_SPLIT_NO_EMPTY |
||
| 159 | ); |
||
| 160 | |||
| 161 | // phpcs:disable |
||
| 162 | /** @var array<string> $docLineTokens */ |
||
| 163 | $docLineTokens = \array_slice( |
||
| 164 | $docLineTokens, |
||
| 165 | 0, |
||
| 166 | $docCommentTags[$content] |
||
| 167 | ); |
||
| 168 | // phpcs:enable |
||
| 169 | |||
| 170 | foreach ($docLineTokens as $docLineToken) { |
||
| 171 | // phpcs:disable |
||
| 172 | /** @var array<string> $typeTokens */ |
||
| 173 | $typeTokens = PregLibrary::MO4PregSplit( |
||
| 174 | '/\|/', |
||
| 175 | $docLineToken, |
||
| 176 | -1, |
||
| 177 | PREG_SPLIT_NO_EMPTY |
||
| 178 | ); |
||
| 179 | // phpcs:enable |
||
| 180 | |||
| 181 | foreach ($typeTokens as $typeToken) { |
||
| 182 | if (true === \in_array($typeToken, $useStatements, true)) { |
||
| 183 | continue; |
||
| 184 | } |
||
| 185 | |||
| 186 | $this->checkShorthandPossible( |
||
| 187 | $phpcsFile, |
||
| 188 | $useStatements, |
||
| 189 | $typeToken, |
||
| 190 | $namespace, |
||
| 191 | $docCommentStringPtr, |
||
| 192 | $docCommentStringPtr, |
||
| 193 | true |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | $nsSep = $phpcsFile->findNext($scanTokens, ($classNameEnd + 1)); |
||
| 201 | } |
||
| 394 |