From 8261581b3b7584b6f8f7ae68e931a11da901265d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 10 Oct 2010 14:41:08 -0400 Subject: [PATCH] Pulling out some duplicated code into methods. --- cake/console/console_input_option.php | 9 +++++++++ cake/console/console_option_parser.php | 25 +++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cake/console/console_input_option.php b/cake/console/console_input_option.php index 1827bf32d..a5e8fa4b7 100644 --- a/cake/console/console_input_option.php +++ b/cake/console/console_input_option.php @@ -53,6 +53,15 @@ class ConsoleInputOption { } } +/** + * Get the name of the argument + * + * @return string + */ + public function name() { + return $this->name; + } + /** * Generate the help for this this option. * diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index aab7429ad..0e95e2349 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -390,11 +390,7 @@ class ConsoleOptionParser { } if (!empty($this->_options)) { - $max = 0; - foreach ($this->_options as $description) { - $max = (strlen($description->name) > $max) ? strlen($description->name) : $max; - } - $max += 8; + $max = $this->_getMaxLength($this->_options) + 8; $out[] = 'Options:'; $out[] = ''; foreach ($this->_options as $option) { @@ -403,11 +399,7 @@ class ConsoleOptionParser { $out[] = ''; } if (!empty($this->_args)) { - $max = 0; - foreach ($this->_args as $argument) { - $max = (strlen($argument->name) > $max) ? strlen($argument->name) : $max; - } - $max += 2; + $max = $this->_getMaxLength($this->_args) + 2; $out[] = 'Arguments:'; $out[] = ''; foreach ($this->_args as $argument) { @@ -547,4 +539,17 @@ class ConsoleOptionParser { protected function _nextToken() { return isset($this->_tokens[0]) ? $this->_tokens[0] : ''; } + +/** + * Iterate over a collection and find the longest named thing. + * + * @return integer + */ + protected function _getMaxLength($collection) { + $max = 0; + foreach ($collection as $item) { + $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max; + } + return $max; + } }