| Total Complexity | 51 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 = []) |
||
| 215 | { |
||
| 216 | $describedNamespace = $options['namespace'] ?? null; |
||
| 217 | $description = new ApplicationDescription($application, $describedNamespace); |
||
| 218 | |||
| 219 | if (isset($options['raw_text']) && $options['raw_text']) { |
||
| 220 | $width = ($description->getCommands()); |
||
| 221 | |||
| 222 | foreach ($description->getCommands() as $command) { |
||
| 223 | $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options); |
||
| 224 | $this->writeText("\n"); |
||
| 225 | } |
||
| 226 | } else { |
||
| 227 | if ('' != $help = $application->getHelp()) { |
||
| 228 | $this->writeText("$help\n\n", $options); |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->writeText("<comment>Usage:</>\n", $options); |
||
| 232 | $this->writeText(" command [options] [arguments]\n\n", $options); |
||
| 233 | |||
| 234 | $this->describeDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options); |
||
| 235 | |||
| 236 | $this->writeText("\n"); |
||
| 237 | $this->writeText("\n"); |
||
| 238 | |||
| 239 | $commands = $description->getCommands(); |
||
| 240 | $namespaces = $description->getNamespaces(); |
||
| 241 | |||
| 242 | if ($describedNamespace && $namespaces) { |
||
|
|
|||
| 243 | // make sure all alias commands are included when describing a specific namespace |
||
| 244 | $describedNamespaceInfo = reset($namespaces); |
||
| 245 | |||
| 246 | foreach ($describedNamespaceInfo['commands'] as $name) { |
||
| 247 | $commands[$name] = $description->getCommand($name); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // calculate max. width based on available commands per namespace |
||
| 252 | $width = (array_merge(...array_values(array_map(fn ($namespace) => array_intersect($namespace['commands'], array_keys($commands)), array_values($namespaces))))); |
||
| 253 | |||
| 254 | if ($describedNamespace) { |
||
| 255 | $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options); |
||
| 256 | } else { |
||
| 257 | $this->writeText('<comment>Available commands:</comment>', $options); |
||
| 258 | } |
||
| 259 | |||
| 260 | foreach ($namespaces as $namespace) { |
||
| 261 | $namespace['commands'] = array_filter($namespace['commands'], fn ($name) => isset($commands[$name])); |
||
| 262 | |||
| 263 | if ( ! $namespace['commands']) { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | if ( ! $describedNamespace && ApplicationDescription::G_NAMESPACE !== $namespace['id']) { |
||
| 268 | $this->writeText("\n"); |
||
| 269 | $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options); |
||
| 270 | } |
||
| 271 | |||
| 272 | foreach ($namespace['commands'] as $name) { |
||
| 273 | $this->writeText("\n"); |
||
| 274 | $spacingWidth = 18; |
||
| 275 | $command = $commands[$name]; |
||
| 276 | $commandAliases = $name === $command->getName() ? $this->getCommandAliases($command) : ''; |
||
| 277 | $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | $this->writeText("\n"); |
||
| 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 = []) |
||
| 294 | { |
||
| 295 | $this->write( |
||
| 296 | isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content, |
||
| 297 | isset($options['raw_output']) ? ! $options['raw_output'] : true |
||
| 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.