Conditions | 7 |
Paths | 10 |
Total Lines | 80 |
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 |
||
61 | protected function execute(InputInterface $input, OutputInterface $output) |
||
62 | { |
||
63 | /** @var KernelInterface $kernel */ |
||
64 | $kernel = $this->getApplication()->getKernel(); |
||
|
|||
65 | $environment = $kernel->getEnvironment(); |
||
66 | /** @var FormatterHelper $formatter */ |
||
67 | $formatter = $this->getHelper('formatter'); |
||
68 | |||
69 | // Be careful, when using the no-interaction option you will not get the confirmation question |
||
70 | $noInteraction = $input->getOption('no-interaction'); |
||
71 | |||
72 | if (!in_array($environment, ['dev_event_replay', 'prod_event_replay', 'smoketest_event_replay'])) { |
||
73 | $output->writeln($formatter->formatBlock( |
||
74 | [ |
||
75 | '', |
||
76 | 'This command may only be executed using env "dev_event_replay", "prod_event_replay", or |
||
77 | "smoketest_event_replay"', |
||
78 | '' |
||
79 | ], |
||
80 | 'error' |
||
81 | )); |
||
82 | |||
83 | return; |
||
84 | } |
||
85 | |||
86 | /** @var QuestionHelper $interrogator */ |
||
87 | $interrogator = $this->getHelper('question'); |
||
88 | if ($environment === 'prod_event_replay') { |
||
89 | $wantToRunOnProd = new ConfirmationQuestion( |
||
90 | '<question>You have selected to run this on production. Have you disabled all access to the production ' |
||
91 | .'environment? (y/N)</question>', |
||
92 | false |
||
93 | ); |
||
94 | |||
95 | if (!$interrogator->ask($input, $output, $wantToRunOnProd)) { |
||
96 | $output->writeln('<comment>Not starting the replay</comment>'); |
||
97 | |||
98 | return; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | $increments = (int)$input->getOption('increments'); |
||
103 | if ($increments < 1) { |
||
104 | $output->writeln( |
||
105 | $formatter->formatBlock( |
||
106 | sprintf('Increments must be a positive integer, "%s" given', $input->getOption('increments')), |
||
107 | 'error' |
||
108 | ) |
||
109 | ); |
||
110 | |||
111 | return; |
||
112 | } |
||
113 | |||
114 | if (!$noInteraction) { |
||
115 | $output->writeln(['', $formatter->formatBlock(['', 'WARNING!!!!', ''], 'error'), '']); |
||
116 | |||
117 | $question = <<<QUESTION |
||
118 | You are about to WIPE all read data and recreate all data based on the events present, in steps of %d. |
||
119 | |||
120 | <%s>>> This can take a while and should not be interrupted <<</%s> |
||
121 | |||
122 | Are you sure you wish to continue? (y/N) |
||
123 | QUESTION; |
||
124 | |||
125 | $question = sprintf($question, $increments, 'error', 'error'); |
||
126 | $areYouSure = new ConfirmationQuestion(sprintf("<question>%s</question>\n", $question), false); |
||
127 | if (!$interrogator->ask($input, $output, $areYouSure)) { |
||
128 | $output->writeln('<comment>Replay cancelled!</comment>'); |
||
129 | |||
130 | return; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $output->writeln(['', $formatter->formatBlock('Starting Event Replay', 'info')]); |
||
135 | $output->writeln( |
||
136 | $formatter->formatBlock(' >> If it is interrupted it must be rerun till completed', 'comment') |
||
137 | ); |
||
138 | |||
139 | $this->replayer->replayEvents($output, $increments); |
||
140 | } |
||
141 | } |
||
142 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: