Conditions | 9 |
Paths | 44 |
Total Lines | 64 |
Code Lines | 31 |
Lines | 14 |
Ratio | 21.88 % |
Changes | 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 |
||
119 | protected function execute(InputInterface $input, OutputInterface $output) |
||
120 | { |
||
121 | // get the ticket |
||
122 | $ticket = $input->getArgument('ticket'); |
||
123 | |||
124 | // we might need framing dates |
||
125 | $toDate = null; |
||
126 | $fromDate = null; |
||
127 | |||
128 | // check if we got a keyword |
||
129 | if ($ticket === self::FILTER_KEYWORD_TODAY) { |
||
130 | // set the fromDate to today, and clear the ticket |
||
131 | $fromDate = strtotime(date('Y-m-d', time())); |
||
132 | $ticket = null; |
||
133 | |||
134 | } elseif ($ticket === self::FILTER_KEYWORD_YESTERDAY) { |
||
135 | // set the fromDate to yesterday, the toDate to today and clear the ticket |
||
136 | $fromDate = strtotime(date('Y-m-d', time() - 24 * 60 * 60)); |
||
137 | $toDate = strtotime(date('Y-m-d', time())); |
||
138 | $ticket = null; |
||
139 | |||
140 | } else { |
||
141 | // check for options first |
||
142 | View Code Duplication | if ($input->getOption(self::OPTION_TO)) { |
|
|
|||
143 | // test for valid format |
||
144 | $tmpDate = strtotime($input->getOption(self::OPTION_TO)); |
||
145 | if ($tmpDate !== false) { |
||
146 | $toDate = $tmpDate; |
||
147 | } |
||
148 | } |
||
149 | View Code Duplication | if ($input->getOption(self::OPTION_FROM)) { |
|
150 | // test for valid format |
||
151 | $tmpDate = strtotime($input->getOption(self::OPTION_FROM)); |
||
152 | if ($tmpDate !== false) { |
||
153 | $fromDate = $tmpDate; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | // check for an amount limiting keyword |
||
159 | $limit = null; |
||
160 | if ($ticket === self::FILTER_KEYWORD_CURRENT) { |
||
161 | // set the limit to 1, and clear the ticket |
||
162 | $limit = 1; |
||
163 | $ticket = null; |
||
164 | } |
||
165 | |||
166 | // filter by ticket if given |
||
167 | /** @var \Wicked\Timely\Storage\StorageFactory $storage */ |
||
168 | $storage = StorageFactory::getStorage(); |
||
169 | $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit); |
||
170 | |||
171 | // prepare with empty message |
||
172 | $text = 'Sorry, could not find any matching bookings'; |
||
173 | |||
174 | // format for output (if we got bookings) |
||
175 | if (!empty($bookings)) { |
||
176 | $formatter = FormatterFactory::getFormatter(FormatterFactory::OUTPUT_CHANNEL); |
||
177 | $text = $formatter->toString($bookings); |
||
178 | } |
||
179 | |||
180 | // write output |
||
181 | $output->write($text, true); |
||
182 | } |
||
183 | } |
||
184 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.