| Conditions | 26 |
| Paths | 1009 |
| Total Lines | 165 |
| Code Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 138 | private function describeRule(OutputInterface $output, string $name): void |
||
| 139 | { |
||
| 140 | $fixers = $this->getFixers(); |
||
| 141 | |||
| 142 | if (!isset($fixers[$name])) { |
||
| 143 | throw new DescribeNameNotFoundException($name, 'rule'); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** @var FixerInterface $fixer */ |
||
| 147 | $fixer = $fixers[$name]; |
||
| 148 | |||
| 149 | $definition = $fixer->getDefinition(); |
||
| 150 | |||
| 151 | $description = $definition->getSummary(); |
||
| 152 | |||
| 153 | if ($fixer instanceof DeprecatedFixerInterface) { |
||
| 154 | $successors = $fixer->getSuccessorsNames(); |
||
| 155 | $message = [] === $successors |
||
| 156 | ? 'will be removed on next major version' |
||
| 157 | : sprintf('use %s instead', Utils::naturalLanguageJoinWithBackticks($successors)); |
||
| 158 | $message = Preg::replace('/(`.+?`)/', '<info>$1</info>', $message); |
||
| 159 | $description .= sprintf(' <error>DEPRECATED</error>: %s.', $message); |
||
| 160 | } |
||
| 161 | |||
| 162 | $output->writeln(sprintf('<info>Description of</info> %s <info>rule</info>.', $name)); |
||
| 163 | |||
| 164 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
||
| 165 | $output->writeln(sprintf('Fixer class: <comment>%s</comment>.', \get_class($fixer))); |
||
| 166 | } |
||
| 167 | |||
| 168 | $output->writeln($description); |
||
| 169 | |||
| 170 | if ($definition->getDescription()) { |
||
| 171 | $output->writeln($definition->getDescription()); |
||
| 172 | } |
||
| 173 | |||
| 174 | $output->writeln(''); |
||
| 175 | |||
| 176 | if ($fixer->isRisky()) { |
||
| 177 | $output->writeln('<error>Fixer applying this rule is risky.</error>'); |
||
| 178 | |||
| 179 | if ($definition->getRiskyDescription()) { |
||
| 180 | $output->writeln($definition->getRiskyDescription()); |
||
| 181 | } |
||
| 182 | |||
| 183 | $output->writeln(''); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($fixer instanceof ConfigurableFixerInterface) { |
||
| 187 | $configurationDefinition = $fixer->getConfigurationDefinition(); |
||
| 188 | $options = $configurationDefinition->getOptions(); |
||
| 189 | |||
| 190 | $output->writeln(sprintf('Fixer is configurable using following option%s:', 1 === \count($options) ? '' : 's')); |
||
| 191 | |||
| 192 | foreach ($options as $option) { |
||
| 193 | $line = '* <info>'.OutputFormatter::escape($option->getName()).'</info>'; |
||
| 194 | $allowed = HelpCommand::getDisplayableAllowedValues($option); |
||
| 195 | |||
| 196 | if (null !== $allowed) { |
||
| 197 | foreach ($allowed as &$value) { |
||
| 198 | if ($value instanceof AllowedValueSubset) { |
||
| 199 | $value = 'a subset of <comment>'.HelpCommand::toString($value->getAllowedValues()).'</comment>'; |
||
| 200 | } else { |
||
| 201 | $value = '<comment>'.HelpCommand::toString($value).'</comment>'; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | $allowed = array_map( |
||
| 206 | static function (string $type) { |
||
| 207 | return '<comment>'.$type.'</comment>'; |
||
| 208 | }, |
||
| 209 | $option->getAllowedTypes() |
||
|
|
|||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | if (null !== $allowed) { |
||
| 214 | $line .= ' ('.implode(', ', $allowed).')'; |
||
| 215 | } |
||
| 216 | |||
| 217 | $description = Preg::replace('/(`.+?`)/', '<info>$1</info>', OutputFormatter::escape($option->getDescription())); |
||
| 218 | $line .= ': '.lcfirst(Preg::replace('/\.$/', '', $description)).'; '; |
||
| 219 | |||
| 220 | if ($option->hasDefault()) { |
||
| 221 | $line .= sprintf( |
||
| 222 | 'defaults to <comment>%s</comment>', |
||
| 223 | HelpCommand::toString($option->getDefault()) |
||
| 224 | ); |
||
| 225 | } else { |
||
| 226 | $line .= '<comment>required</comment>'; |
||
| 227 | } |
||
| 228 | |||
| 229 | if ($option instanceof DeprecatedFixerOption) { |
||
| 230 | $line .= '. <error>DEPRECATED</error>: '.Preg::replace( |
||
| 231 | '/(`.+?`)/', |
||
| 232 | '<info>$1</info>', |
||
| 233 | OutputFormatter::escape(lcfirst($option->getDeprecationMessage())) |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($option instanceof AliasedFixerOption) { |
||
| 238 | $line .= '; <error>DEPRECATED</error> alias: <comment>'.$option->getAlias().'</comment>'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $output->writeln($line); |
||
| 242 | } |
||
| 243 | |||
| 244 | $output->writeln(''); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** @var CodeSampleInterface[] $codeSamples */ |
||
| 248 | $codeSamples = array_filter($definition->getCodeSamples(), static function (CodeSampleInterface $codeSample) { |
||
| 249 | if ($codeSample instanceof VersionSpecificCodeSampleInterface) { |
||
| 250 | return $codeSample->isSuitableFor(\PHP_VERSION_ID); |
||
| 251 | } |
||
| 252 | |||
| 253 | return true; |
||
| 254 | }); |
||
| 255 | |||
| 256 | if (!\count($codeSamples)) { |
||
| 257 | $output->writeln([ |
||
| 258 | 'Fixing examples can not be demonstrated on the current PHP version.', |
||
| 259 | '', |
||
| 260 | ]); |
||
| 261 | } else { |
||
| 262 | $output->writeln('Fixing examples:'); |
||
| 263 | |||
| 264 | $differ = new FullDiffer(); |
||
| 265 | $diffFormatter = new DiffConsoleFormatter( |
||
| 266 | $output->isDecorated(), |
||
| 267 | sprintf( |
||
| 268 | '<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>', |
||
| 269 | PHP_EOL, |
||
| 270 | PHP_EOL |
||
| 271 | ) |
||
| 272 | ); |
||
| 273 | |||
| 274 | foreach ($codeSamples as $index => $codeSample) { |
||
| 275 | $old = $codeSample->getCode(); |
||
| 276 | $tokens = Tokens::fromCode($old); |
||
| 277 | |||
| 278 | $configuration = $codeSample->getConfiguration(); |
||
| 279 | |||
| 280 | if ($fixer instanceof ConfigurableFixerInterface) { |
||
| 281 | $fixer->configure(null === $configuration ? [] : $configuration); |
||
| 282 | } |
||
| 283 | |||
| 284 | $file = $codeSample instanceof FileSpecificCodeSampleInterface |
||
| 285 | ? $codeSample->getSplFileInfo() |
||
| 286 | : new StdinFileInfo(); |
||
| 287 | |||
| 288 | $fixer->fix($file, $tokens); |
||
| 289 | |||
| 290 | $diff = $differ->diff($old, $tokens->generateCode()); |
||
| 291 | |||
| 292 | if ($fixer instanceof ConfigurableFixerInterface) { |
||
| 293 | if (null === $configuration) { |
||
| 294 | $output->writeln(sprintf(' * Example #%d. Fixing with the <comment>default</comment> configuration.', $index + 1)); |
||
| 295 | } else { |
||
| 296 | $output->writeln(sprintf(' * Example #%d. Fixing with configuration: <comment>%s</comment>.', $index + 1, HelpCommand::toString($codeSample->getConfiguration()))); |
||
| 297 | } |
||
| 298 | } else { |
||
| 299 | $output->writeln(sprintf(' * Example #%d.', $index + 1)); |
||
| 300 | } |
||
| 301 | |||
| 302 | $output->writeln([$diffFormatter->format($diff, ' %s'), '']); |
||
| 303 | } |
||
| 434 |