Conditions | 12 |
Paths | 1 |
Total Lines | 102 |
Code Lines | 55 |
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 declare(strict_types = 1); |
||
31 | protected function execute(InputInterface $input, OutputInterface $output) |
||
32 | { |
||
33 | if ($this->yaml === null) { |
||
34 | throw new InvalidArgumentException('Missing configuration'); |
||
35 | } |
||
36 | |||
37 | $style = new SymfonyStyle($input, $output); |
||
38 | |||
39 | $style->newLine(2); |
||
40 | $this->asciiArt($style); |
||
41 | $style->newLine(2); |
||
42 | |||
43 | $style->title($this->yaml['text']['welcome']); |
||
44 | |||
45 | /** |
||
46 | * Launchpad, the retry goto point. |
||
47 | */ |
||
48 | retry: |
||
49 | $style->section('Please answer the following questions.'); |
||
50 | |||
51 | $replacements = []; |
||
52 | $table = []; |
||
53 | |||
54 | foreach ($this->yaml['questions'] as $identifier => $question) { |
||
55 | $replacements[$identifier] = $style->ask( |
||
56 | $question['question'], |
||
57 | get_in($question, ['default'], '') |
||
58 | ); |
||
59 | |||
60 | if (empty($replacements[$identifier]) || |
||
61 | (isset($question['validate']) && is_callable($question['validate'])) |
||
62 | ) { |
||
63 | while (!$this->validate($question['validate'], $replacements[$identifier]) || |
||
64 | empty($replacements[$identifier]) |
||
65 | ) { |
||
66 | $replacements[$identifier] = $style->ask( |
||
67 | 'Invalid response please try again, ' . $question['question'], |
||
68 | get_in($question, ['default'], '') |
||
69 | ); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | $table[] = [ |
||
74 | $question['description'], |
||
75 | $replacements[$identifier], |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | $style->section('Summary:'); |
||
80 | $style->table( |
||
81 | [ |
||
82 | 'What', |
||
83 | 'Value', |
||
84 | ], |
||
85 | $table |
||
86 | ); |
||
87 | |||
88 | $installNow = $style->choice( |
||
89 | 'All settings correct?', |
||
90 | [ |
||
91 | 'y' => 'Yes', |
||
92 | 'n' => 'Change settings', |
||
93 | 'q' => 'Cancel installation', |
||
94 | ], |
||
95 | 'Yes' |
||
96 | ); |
||
97 | |||
98 | switch (strtolower($installNow)) { |
||
99 | case 'y': |
||
100 | { |
||
101 | $style->text('Creating your middleware package now.'); |
||
102 | /** @var callable $operation */ |
||
103 | foreach ($this->yaml['operations'] as $operation) { |
||
104 | $operation()->operate($replacements, $this->yaml['env'] ?? [], $style); |
||
105 | } |
||
106 | $style->section('Package creation has been successfully.'); |
||
107 | $style->text('Next up we\'re running composer update twice to ensure all traces of this installer are gone.'); |
||
108 | $style->text('(The first time to update composer.lock, and the second time to update the autoloader.)'); |
||
109 | $style->text('After which your new package is ready to be developed.'); |
||
110 | $style->success('Installer done.'); |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | case 'n': |
||
115 | { |
||
116 | /** |
||
117 | * Retry, goto launchpad. |
||
118 | */ |
||
119 | goto retry; |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | case 'q': |
||
124 | { |
||
125 | $style->error('Installation canceled.'); |
||
126 | |||
127 | return 9; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return 0; |
||
132 | } |
||
133 | |||
185 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.