Conditions | 6 |
Paths | 3 |
Total Lines | 57 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
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 |
||
27 | protected function execute(InputInterface $input, OutputInterface $output) |
||
28 | { |
||
29 | $id = $input->getArgument('matchID'); |
||
30 | $match = Match::get($id); |
||
31 | |||
32 | if (!$match->isValid()) { |
||
33 | $output->writeln(sprintf('No match found with id: %d', $id)); |
||
34 | exit(1); |
||
|
|||
35 | } |
||
36 | |||
37 | $output->writeln(sprintf('Starting Elo recalculation with match #%d...', $id)); |
||
38 | $output->writeln(sprintf(' (this may take a while)')); |
||
39 | |||
40 | try { |
||
41 | Match::recalculateMatchesSince($match, function ($event) use ($output) { |
||
42 | switch ($event['type']) { |
||
43 | case 'recalculation.count': |
||
44 | $output->writeln(sprintf("\nDetected %d matches needing recalculations\n", $event['value'])); |
||
45 | $this->initProgress($output, $event['value']); |
||
46 | break; |
||
47 | |||
48 | case 'recalculation.progress': |
||
49 | $this->advance(); |
||
50 | break; |
||
51 | |||
52 | case 'recalculation.complete': |
||
53 | $this->finishProgress(); |
||
54 | break; |
||
55 | |||
56 | default: |
||
57 | $output->writeln(sprintf('Received unknown recalculation event: %s', $event['type'])); |
||
58 | } |
||
59 | }); |
||
60 | } |
||
61 | catch (\Exception $e) { |
||
62 | $errMsg = $e->getMessage(); |
||
63 | |||
64 | $output->writeln(<<<EXCEPTION |
||
65 | |||
66 | <bg=red;options=bold> |
||
67 | [ERROR] An exception occurred with the following message: |
||
68 | ${$errMsg} |
||
69 | </> |
||
70 | EXCEPTION |
||
71 | ); |
||
72 | exit(1); |
||
73 | } |
||
74 | |||
75 | $output->writeln(<<<SUCCESS |
||
76 | |||
77 | <bg=green;options=bold> |
||
78 | |||
79 | [OK] Match Elos have been successfully recalculated. |
||
80 | </> |
||
81 | SUCCESS |
||
82 | ); |
||
83 | } |
||
84 | } |
||
85 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.