Complex classes like Application 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Application |
||
| 61 | { |
||
| 62 | private $commands = array(); |
||
| 63 | private $wantHelps = false; |
||
| 64 | private $runningCommand; |
||
| 65 | private $name; |
||
| 66 | private $version; |
||
| 67 | private $catchExceptions = true; |
||
| 68 | private $autoExit = true; |
||
| 69 | private $definition; |
||
| 70 | private $helperSet; |
||
| 71 | private $dispatcher; |
||
| 72 | private $terminal; |
||
| 73 | private $defaultCommand; |
||
| 74 | private $singleCommand; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $name The name of the application |
||
| 78 | * @param string $version The version of the application |
||
| 79 | */ |
||
| 80 | public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') |
||
| 93 | |||
| 94 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 95 | { |
||
| 96 | $this->dispatcher = $dispatcher; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Runs the current application. |
||
| 101 | * |
||
| 102 | * @param InputInterface $input An Input instance |
||
| 103 | * @param OutputInterface $output An Output instance |
||
| 104 | * |
||
| 105 | * @return int 0 if everything went fine, or an error code |
||
| 106 | * |
||
| 107 | * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}. |
||
| 108 | */ |
||
| 109 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Runs the current application. |
||
| 172 | * |
||
| 173 | * @param InputInterface $input An Input instance |
||
| 174 | * @param OutputInterface $output An Output instance |
||
| 175 | * |
||
| 176 | * @return int 0 if everything went fine, or an error code |
||
| 177 | */ |
||
| 178 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set a helper set to be used with the command. |
||
| 231 | * |
||
| 232 | * @param HelperSet $helperSet The helper set |
||
| 233 | */ |
||
| 234 | public function setHelperSet(HelperSet $helperSet) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the helper set associated with the command. |
||
| 241 | * |
||
| 242 | * @return HelperSet The HelperSet instance associated with this command |
||
| 243 | */ |
||
| 244 | public function getHelperSet() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set an input definition to be used with this application. |
||
| 251 | * |
||
| 252 | * @param InputDefinition $definition The input definition |
||
| 253 | */ |
||
| 254 | public function setDefinition(InputDefinition $definition) |
||
| 255 | { |
||
| 256 | $this->definition = $definition; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the InputDefinition related to this Application. |
||
| 261 | * |
||
| 262 | * @return InputDefinition The InputDefinition instance |
||
| 263 | */ |
||
| 264 | public function getDefinition() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Gets the help message. |
||
| 278 | * |
||
| 279 | * @return string A help message |
||
| 280 | */ |
||
| 281 | public function getHelp() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Gets whether to catch exceptions or not during commands execution. |
||
| 288 | * |
||
| 289 | * @return bool Whether to catch exceptions or not during commands execution |
||
| 290 | */ |
||
| 291 | public function areExceptionsCaught() |
||
| 292 | { |
||
| 293 | return $this->catchExceptions; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Sets whether to catch exceptions or not during commands execution. |
||
| 298 | * |
||
| 299 | * @param bool $boolean Whether to catch exceptions or not during commands execution |
||
| 300 | */ |
||
| 301 | public function setCatchExceptions($boolean) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Gets whether to automatically exit after a command execution or not. |
||
| 308 | * |
||
| 309 | * @return bool Whether to automatically exit after a command execution or not |
||
| 310 | */ |
||
| 311 | public function isAutoExitEnabled() |
||
| 312 | { |
||
| 313 | return $this->autoExit; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Sets whether to automatically exit after a command execution or not. |
||
| 318 | * |
||
| 319 | * @param bool $boolean Whether to automatically exit after a command execution or not |
||
| 320 | */ |
||
| 321 | public function setAutoExit($boolean) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Gets the name of the application. |
||
| 328 | * |
||
| 329 | * @return string The application name |
||
| 330 | */ |
||
| 331 | public function getName() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sets the application name. |
||
| 338 | * |
||
| 339 | * @param string $name The application name |
||
| 340 | */ |
||
| 341 | public function setName($name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Gets the application version. |
||
| 348 | * |
||
| 349 | * @return string The application version |
||
| 350 | */ |
||
| 351 | public function getVersion() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Sets the application version. |
||
| 358 | * |
||
| 359 | * @param string $version The application version |
||
| 360 | */ |
||
| 361 | public function setVersion($version) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns the long version of the application. |
||
| 368 | * |
||
| 369 | * @return string The long application version |
||
| 370 | */ |
||
| 371 | public function getLongVersion() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Registers a new command. |
||
| 386 | * |
||
| 387 | * @param string $name The command name |
||
| 388 | * |
||
| 389 | * @return Command The newly created command |
||
| 390 | */ |
||
| 391 | public function register($name) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Adds an array of command objects. |
||
| 398 | * |
||
| 399 | * If a Command is not enabled it will not be added. |
||
| 400 | * |
||
| 401 | * @param Command[] $commands An array of commands |
||
| 402 | */ |
||
| 403 | public function addCommands(array $commands) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Adds a command object. |
||
| 412 | * |
||
| 413 | * If a command with the same name already exists, it will be overridden. |
||
| 414 | * If the command is not enabled it will not be added. |
||
| 415 | * |
||
| 416 | * @param Command $command A Command object |
||
| 417 | * |
||
| 418 | * @return Command|null The registered command if enabled or null |
||
| 419 | */ |
||
| 420 | public function add(Command $command) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns a registered command by name or alias. |
||
| 445 | * |
||
| 446 | * @param string $name The command name or alias |
||
| 447 | * |
||
| 448 | * @return Command A Command object |
||
| 449 | * |
||
| 450 | * @throws CommandNotFoundException When given command name does not exist |
||
| 451 | */ |
||
| 452 | public function get($name) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Returns true if the command exists, false otherwise. |
||
| 474 | * |
||
| 475 | * @param string $name The command name or alias |
||
| 476 | * |
||
| 477 | * @return bool true if the command exists, false otherwise |
||
| 478 | */ |
||
| 479 | public function has($name) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns an array of all unique namespaces used by currently registered commands. |
||
| 486 | * |
||
| 487 | * It does not return the global namespace which always exists. |
||
| 488 | * |
||
| 489 | * @return string[] An array of namespaces |
||
| 490 | */ |
||
| 491 | public function getNamespaces() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Finds a registered namespace by a name or an abbreviation. |
||
| 507 | * |
||
| 508 | * @param string $namespace A namespace or abbreviation to search for |
||
| 509 | * |
||
| 510 | * @return string A registered namespace |
||
| 511 | * |
||
| 512 | * @throws CommandNotFoundException When namespace is incorrect or ambiguous |
||
| 513 | */ |
||
| 514 | public function findNamespace($namespace) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Finds a command by name or alias. |
||
| 546 | * |
||
| 547 | * Contrary to get, this command tries to find the best |
||
| 548 | * match if you give it an abbreviation of a name or alias. |
||
| 549 | * |
||
| 550 | * @param string $name A command name or a command alias |
||
| 551 | * |
||
| 552 | * @return Command A Command instance |
||
| 553 | * |
||
| 554 | * @throws CommandNotFoundException When command name is incorrect or ambiguous |
||
| 555 | */ |
||
| 556 | public function find($name) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Gets the commands (registered in the given namespace if provided). |
||
| 615 | * |
||
| 616 | * The array keys are the full names and the values the command instances. |
||
| 617 | * |
||
| 618 | * @param string $namespace A namespace name |
||
| 619 | * |
||
| 620 | * @return Command[] An array of Command instances |
||
| 621 | */ |
||
| 622 | public function all($namespace = null) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Returns an array of possible abbreviations given a set of names. |
||
| 640 | * |
||
| 641 | * @param array $names An array of names |
||
| 642 | * |
||
| 643 | * @return array An array of abbreviations |
||
| 644 | */ |
||
| 645 | public static function getAbbreviations($names) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Renders a caught exception. |
||
| 660 | * |
||
| 661 | * @param \Exception $e An exception instance |
||
| 662 | * @param OutputInterface $output An OutputInterface instance |
||
| 663 | */ |
||
| 664 | public function renderException(\Exception $e, OutputInterface $output) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Tries to figure out the terminal width in which this application runs. |
||
| 738 | * |
||
| 739 | * @return int|null |
||
| 740 | * |
||
| 741 | * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. |
||
| 742 | */ |
||
| 743 | protected function getTerminalWidth() |
||
| 744 | { |
||
| 745 | @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 746 | |||
| 747 | return $this->terminal->getWidth(); |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Tries to figure out the terminal height in which this application runs. |
||
| 752 | * |
||
| 753 | * @return int|null |
||
| 754 | * |
||
| 755 | * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. |
||
| 756 | */ |
||
| 757 | protected function getTerminalHeight() |
||
| 758 | { |
||
| 759 | @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 760 | |||
| 761 | return $this->terminal->getHeight(); |
||
| 762 | } |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Tries to figure out the terminal dimensions based on the current environment. |
||
| 766 | * |
||
| 767 | * @return array Array containing width and height |
||
| 768 | * |
||
| 769 | * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. |
||
| 770 | */ |
||
| 771 | public function getTerminalDimensions() |
||
| 772 | { |
||
| 773 | @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 774 | |||
| 775 | return array($this->terminal->getWidth(), $this->terminal->getHeight()); |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Sets terminal dimensions. |
||
| 780 | * |
||
| 781 | * Can be useful to force terminal dimensions for functional tests. |
||
| 782 | * |
||
| 783 | * @param int $width The width |
||
| 784 | * @param int $height The height |
||
| 785 | * |
||
| 786 | * @return $this |
||
| 787 | * |
||
| 788 | * @deprecated since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead. |
||
| 789 | */ |
||
| 790 | public function setTerminalDimensions($width, $height) |
||
| 791 | { |
||
| 792 | @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Set the COLUMNS and LINES env vars instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 793 | |||
| 794 | putenv('COLUMNS='.$width); |
||
| 795 | putenv('LINES='.$height); |
||
| 796 | |||
| 797 | return $this; |
||
| 798 | } |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Configures the input and output instances based on the user arguments and options. |
||
| 802 | * |
||
| 803 | * @param InputInterface $input An InputInterface instance |
||
| 804 | * @param OutputInterface $output An OutputInterface instance |
||
| 805 | */ |
||
| 806 | protected function configureIO(InputInterface $input, OutputInterface $output) |
||
| 807 | { |
||
| 808 | if (true === $input->hasParameterOption(array('--ansi'), true)) { |
||
| 809 | $output->setDecorated(true); |
||
| 810 | } elseif (true === $input->hasParameterOption(array('--no-ansi'), true)) { |
||
| 811 | $output->setDecorated(false); |
||
| 812 | } |
||
| 813 | |||
| 814 | if (true === $input->hasParameterOption(array('--no-interaction', '-n'), true)) { |
||
| 815 | $input->setInteractive(false); |
||
| 816 | } elseif (function_exists('posix_isatty')) { |
||
| 817 | $inputStream = null; |
||
| 818 | |||
| 819 | if ($input instanceof StreamableInputInterface) { |
||
| 820 | $inputStream = $input->getStream(); |
||
| 821 | } |
||
| 822 | |||
| 823 | // This check ensures that calling QuestionHelper::setInputStream() works |
||
| 824 | // To be removed in 4.0 (in the same time as QuestionHelper::setInputStream) |
||
| 825 | if (!$inputStream && $this->getHelperSet()->has('question')) { |
||
| 826 | $inputStream = $this->getHelperSet()->get('question')->getInputStream(false); |
||
| 827 | } |
||
| 828 | |||
| 829 | if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) { |
||
| 830 | $input->setInteractive(false); |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) { |
||
| 835 | $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
||
| 836 | $input->setInteractive(false); |
||
| 837 | } else { |
||
| 838 | if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || $input->getParameterOption('--verbose', false, true) === 3) { |
||
| 839 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
| 840 | } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || $input->getParameterOption('--verbose', false, true) === 2) { |
||
| 841 | $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 842 | } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) { |
||
| 843 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Runs the current command. |
||
| 850 | * |
||
| 851 | * If an event dispatcher has been attached to the application, |
||
| 852 | * events are also dispatched during the life-cycle of the command. |
||
| 853 | * |
||
| 854 | * @param Command $command A Command instance |
||
| 855 | * @param InputInterface $input An Input instance |
||
| 856 | * @param OutputInterface $output An Output instance |
||
| 857 | * |
||
| 858 | * @return int 0 if everything went fine, or an error code |
||
| 859 | */ |
||
| 860 | protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
||
| 861 | { |
||
| 862 | foreach ($command->getHelperSet() as $helper) { |
||
| 863 | if ($helper instanceof InputAwareInterface) { |
||
| 864 | $helper->setInput($input); |
||
| 865 | } |
||
| 866 | } |
||
| 867 | |||
| 868 | if (null === $this->dispatcher) { |
||
| 869 | return $command->run($input, $output); |
||
| 870 | } |
||
| 871 | |||
| 872 | // bind before the console.command event, so the listeners have access to input options/arguments |
||
| 873 | try { |
||
| 874 | $command->mergeApplicationDefinition(); |
||
| 875 | $input->bind($command->getDefinition()); |
||
| 876 | } catch (ExceptionInterface $e) { |
||
| 877 | // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition |
||
| 878 | } |
||
| 879 | |||
| 880 | $event = new ConsoleCommandEvent($command, $input, $output); |
||
| 881 | $e = null; |
||
| 882 | |||
| 883 | try { |
||
| 884 | $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); |
||
| 885 | |||
| 886 | if ($event->commandShouldRun()) { |
||
| 887 | $exitCode = $command->run($input, $output); |
||
| 888 | } else { |
||
| 889 | $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; |
||
| 890 | } |
||
| 891 | } catch (\Exception $e) { |
||
| 892 | } catch (\Throwable $e) { |
||
| 893 | } |
||
| 894 | if (null !== $e) { |
||
| 895 | if ($this->dispatcher->hasListeners(ConsoleEvents::EXCEPTION)) { |
||
| 896 | $x = $e instanceof \Exception ? $e : new FatalThrowableError($e); |
||
| 897 | $event = new ConsoleExceptionEvent($command, $input, $output, $x, $x->getCode()); |
||
| 898 | $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); |
||
| 899 | |||
| 900 | if ($x !== $event->getException()) { |
||
| 901 | $e = $event->getException(); |
||
| 902 | } |
||
| 903 | } |
||
| 904 | $event = new ConsoleErrorEvent($input, $output, $e, $command); |
||
| 905 | $this->dispatcher->dispatch(ConsoleEvents::ERROR, $event); |
||
| 906 | $e = $event->getError(); |
||
| 907 | |||
| 908 | if (0 === $exitCode = $event->getExitCode()) { |
||
| 909 | $e = null; |
||
| 910 | } |
||
| 911 | } |
||
| 912 | |||
| 913 | $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); |
||
| 914 | $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); |
||
| 915 | |||
| 916 | if (null !== $e) { |
||
| 917 | throw $e; |
||
| 918 | } |
||
| 919 | |||
| 920 | return $event->getExitCode(); |
||
| 921 | } |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Gets the name of the command based on input. |
||
| 925 | * |
||
| 926 | * @param InputInterface $input The input interface |
||
| 927 | * |
||
| 928 | * @return string The command name |
||
| 929 | */ |
||
| 930 | protected function getCommandName(InputInterface $input) |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Gets the default input definition. |
||
| 937 | * |
||
| 938 | * @return InputDefinition An InputDefinition instance |
||
| 939 | */ |
||
| 940 | protected function getDefaultInputDefinition() |
||
| 941 | { |
||
| 942 | return new InputDefinition(array( |
||
| 943 | new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'), |
||
| 944 | |||
| 945 | new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'), |
||
| 946 | new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'), |
||
| 947 | new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'), |
||
| 948 | new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'), |
||
| 949 | new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'), |
||
| 950 | new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'), |
||
| 951 | new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'), |
||
| 952 | )); |
||
| 953 | } |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Gets the default commands that should always be available. |
||
| 957 | * |
||
| 958 | * @return Command[] An array of default Command instances |
||
| 959 | */ |
||
| 960 | protected function getDefaultCommands() |
||
| 961 | { |
||
| 962 | return array(new HelpCommand(), new ListCommand()); |
||
| 963 | } |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Gets the default helper set with the helpers that should always be available. |
||
| 967 | * |
||
| 968 | * @return HelperSet A HelperSet instance |
||
| 969 | */ |
||
| 970 | protected function getDefaultHelperSet() |
||
| 971 | { |
||
| 972 | return new HelperSet(array( |
||
| 973 | new FormatterHelper(), |
||
| 974 | new DebugFormatterHelper(), |
||
| 975 | new ProcessHelper(), |
||
| 976 | new QuestionHelper(), |
||
| 977 | )); |
||
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Returns abbreviated suggestions in string format. |
||
| 982 | * |
||
| 983 | * @param array $abbrevs Abbreviated suggestions to convert |
||
| 984 | * |
||
| 985 | * @return string A formatted string of abbreviated suggestions |
||
| 986 | */ |
||
| 987 | private function getAbbreviationSuggestions($abbrevs) |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Returns the namespace part of the command name. |
||
| 994 | * |
||
| 995 | * This method is not part of public API and should not be used directly. |
||
| 996 | * |
||
| 997 | * @param string $name The full name of the command |
||
| 998 | * @param string $limit The maximum number of parts of the namespace |
||
| 999 | * |
||
| 1000 | * @return string The namespace of the command |
||
| 1001 | */ |
||
| 1002 | public function extractNamespace($name, $limit = null) |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Finds alternative of $name among $collection, |
||
| 1012 | * if nothing is found in $collection, try in $abbrevs. |
||
| 1013 | * |
||
| 1014 | * @param string $name The string |
||
| 1015 | * @param array|\Traversable $collection The collection |
||
| 1016 | * |
||
| 1017 | * @return string[] A sorted array of similar string |
||
| 1018 | */ |
||
| 1019 | private function findAlternatives($name, $collection) |
||
| 1020 | { |
||
| 1021 | $threshold = 1e3; |
||
| 1022 | $alternatives = array(); |
||
| 1023 | |||
| 1024 | $collectionParts = array(); |
||
| 1025 | foreach ($collection as $item) { |
||
| 1026 | $collectionParts[$item] = explode(':', $item); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | foreach (explode(':', $name) as $i => $subname) { |
||
| 1030 | foreach ($collectionParts as $collectionName => $parts) { |
||
| 1031 | $exists = isset($alternatives[$collectionName]); |
||
| 1032 | if (!isset($parts[$i]) && $exists) { |
||
| 1033 | $alternatives[$collectionName] += $threshold; |
||
| 1034 | continue; |
||
| 1035 | } elseif (!isset($parts[$i])) { |
||
| 1036 | continue; |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | $lev = levenshtein($subname, $parts[$i]); |
||
| 1040 | if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) { |
||
| 1041 | $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev; |
||
| 1042 | } elseif ($exists) { |
||
| 1043 | $alternatives[$collectionName] += $threshold; |
||
| 1044 | } |
||
| 1045 | } |
||
| 1046 | } |
||
| 1047 | |||
| 1048 | foreach ($collection as $item) { |
||
| 1049 | $lev = levenshtein($name, $item); |
||
| 1050 | if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) { |
||
| 1051 | $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev; |
||
| 1052 | } |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | $alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; }); |
||
| 1056 | ksort($alternatives, SORT_NATURAL | SORT_FLAG_CASE); |
||
| 1057 | |||
| 1058 | return array_keys($alternatives); |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Sets the default Command name. |
||
| 1063 | * |
||
| 1064 | * @param string $commandName The Command name |
||
| 1065 | * @param bool $isSingleCommand Set to true if there is only one command in this application |
||
| 1066 | * |
||
| 1067 | * @return self |
||
| 1068 | */ |
||
| 1069 | public function setDefaultCommand($commandName, $isSingleCommand = false) |
||
| 1070 | { |
||
| 1071 | $this->defaultCommand = $commandName; |
||
| 1072 | |||
| 1073 | if ($isSingleCommand) { |
||
| 1074 | // Ensure the command exist |
||
| 1075 | $this->find($commandName); |
||
| 1076 | |||
| 1077 | $this->singleCommand = true; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | return $this; |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | private function splitStringByWidth($string, $width) |
||
| 1084 | { |
||
| 1085 | // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly. |
||
| 1086 | // additionally, array_slice() is not enough as some character has doubled width. |
||
| 1087 | // we need a function to split string not by character count but by string width |
||
| 1088 | if (false === $encoding = mb_detect_encoding($string, null, true)) { |
||
| 1089 | return str_split($string, $width); |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | $utf8String = mb_convert_encoding($string, 'utf8', $encoding); |
||
| 1093 | $lines = array(); |
||
| 1094 | $line = ''; |
||
| 1095 | foreach (preg_split('//u', $utf8String) as $char) { |
||
| 1096 | // test if $char could be appended to current line |
||
| 1097 | if (mb_strwidth($line.$char, 'utf8') <= $width) { |
||
| 1098 | $line .= $char; |
||
| 1099 | continue; |
||
| 1100 | } |
||
| 1101 | // if not, push current line to array and make new line |
||
| 1102 | $lines[] = str_pad($line, $width); |
||
| 1103 | $line = $char; |
||
| 1104 | } |
||
| 1105 | if ('' !== $line) { |
||
| 1106 | $lines[] = count($lines) ? str_pad($line, $width) : $line; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | mb_convert_variables($encoding, 'utf8', $lines); |
||
| 1110 | |||
| 1111 | return $lines; |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Returns all namespaces of the command name. |
||
| 1116 | * |
||
| 1117 | * @param string $name The full name of the command |
||
| 1118 | * |
||
| 1119 | * @return string[] The namespaces of the command |
||
| 1120 | */ |
||
| 1121 | private function extractAllNamespaces($name) |
||
| 1122 | { |
||
| 1123 | // -1 as third argument is needed to skip the command short name when exploding |
||
| 1124 | $parts = explode(':', $name, -1); |
||
| 1125 | $namespaces = array(); |
||
| 1126 | |||
| 1127 | foreach ($parts as $part) { |
||
| 1128 | if (count($namespaces)) { |
||
| 1129 | $namespaces[] = end($namespaces).':'.$part; |
||
| 1130 | } else { |
||
| 1131 | $namespaces[] = $part; |
||
| 1132 | } |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | return $namespaces; |
||
| 1136 | } |
||
| 1137 | } |
||
| 1138 |
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.