| Total Complexity | 51 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TextDescriptor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TextDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class TextDescriptor extends Descriptor |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The output interface implementation. |
||
| 38 | * |
||
| 39 | * @var \Syscodes\Components\Contracts\Console\Output $output |
||
| 40 | */ |
||
| 41 | protected $output; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Describes an InputArgument instance. |
||
| 45 | * |
||
| 46 | * @param \Syscodes\Components\Console\Input\InputArgument $argument The argument implemented |
||
| 47 | * @param array $options The options of the console |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | protected function describeArgument(InputArgument $argument, array $options = []) |
||
| 52 | { |
||
| 53 | if (null !== $argument->getDefault() && ( ! is_array($argument->getDefault()) || count($argument->getDefault()))) { |
||
| 54 | $default = sprintf(' [<note>default: %s</>] ', $argument->getDefault()); |
||
| 55 | } else { |
||
| 56 | $default = ''; |
||
| 57 | } |
||
| 58 | |||
| 59 | $totalWidth = strlen($argument->getName()); |
||
| 60 | $spacingWidth = $totalWidth - strlen($argument->getName()); |
||
| 61 | |||
| 62 | $this->writeText(sprintf(' <info>%s</> %s%s%s', |
||
| 63 | $argument->getName(), |
||
| 64 | str_repeat(' ', $spacingWidth), |
||
| 65 | preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()), |
||
| 66 | $default |
||
| 67 | ), $options); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Describes an InputOption instance. |
||
| 72 | * |
||
| 73 | * @param \Syscodes\Components\Console\Input\InputOption $option The option implemented |
||
| 74 | * @param array $options The options of the console |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | protected function describeOption(InputOption $option, array $options = []) |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Describes an InputDefinition instance. |
||
| 114 | * |
||
| 115 | * @param \Syscodes\Components\Console\Input\InputDefinition $definition The definition implemented |
||
| 116 | * @param array $options The options of the console |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | protected function describeDefinition(InputDefinition $definition, array $options = []) |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Describes an Command instance. |
||
| 159 | * |
||
| 160 | * @param \Syscodes\Components\Console\Command\Command $command The command implemented |
||
| 161 | * @param array $options The options of the console |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | protected function describeCommand(Command $command, array $options = []) |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Describes an Application instance. |
||
| 208 | * |
||
| 209 | * @param \Syscodes\Components\Console\Application $application The application implemented |
||
| 210 | * @param array $options The options of the console |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | protected function describeApplication(Application $application, array $options = []) |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Writes a message to the output. |
||
| 287 | * |
||
| 288 | * @param string $content The message to output |
||
| 289 | * @param array $options The option of bitmask |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | private function writeText(string $content, array $options = []) |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Formats command aliases to show them in the command description. |
||
| 303 | * |
||
| 304 | * @param \Syscodes\Components\Console\Command\Command $command |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | private function getCommandAliases(Command $command): string |
||
| 318 | } |
||
| 319 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.