Conditions | 9 |
Paths | 7 |
Total Lines | 98 |
Code Lines | 52 |
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 |
||
111 | protected function interact(InputInterface $input, OutputInterface $output) |
||
112 | { |
||
113 | $questionHelper = $this->getQuestionHelper(); |
||
114 | $questionHelper->writeSection($output, 'Welcome to the Image entity generator'); |
||
115 | |||
116 | $message = [ |
||
117 | 'This Currently only supports one Image entity', |
||
118 | '', |
||
119 | ]; |
||
120 | |||
121 | // If entity already exists, |
||
122 | // get overwrite permission |
||
123 | if ($this->needsOverWritePermission) { |
||
124 | $message[] = sprintf('It looks like the image entity exists as %s', $this->responsiveImageEntity); |
||
125 | $message[] = ''; |
||
126 | $message[] = sprintf( |
||
127 | 'Therefore this generator will overwrite that class, %s', |
||
128 | $this->responsiveImageEntity |
||
129 | ); |
||
130 | |||
131 | $output->writeln($message); |
||
132 | |||
133 | // Ask whether overwrite is allowed |
||
134 | $question = $this->createYesNoQuestion($questionHelper, $this->responsiveImageEntity); |
||
135 | $overwrite = $questionHelper->ask($input, $output, $question); |
||
136 | |||
137 | if ($overwrite !== 'y') { |
||
138 | throw new \RuntimeException( |
||
139 | 'Aborting, overwrite permission is needed.' |
||
140 | ); |
||
141 | } |
||
142 | else { |
||
143 | $this->overwrite = true; |
||
144 | } |
||
145 | } |
||
146 | // If class name is not set, |
||
147 | // Ask the bundle and the entity name questions |
||
148 | elseif (empty($this->responsiveImageEntity)) { |
||
149 | $question = new Question( |
||
150 | $questionHelper->getQuestion('The bundle name', $input->getOption('bundle')), |
||
151 | $input->getOption('bundle') |
||
152 | ); |
||
153 | |||
154 | $question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName']); |
||
155 | $question->setNormalizer( |
||
156 | function ($value) { |
||
157 | return $value ? trim($value) : ''; |
||
158 | } |
||
159 | ); |
||
160 | $question->setMaxAttempts(2); |
||
161 | |||
162 | $bundle = $questionHelper->ask($input, $output, $question); |
||
163 | $input->setOption('bundle', $bundle); |
||
164 | |||
165 | // Get the Bundle to generate it in |
||
166 | $output->writeln( |
||
167 | [ |
||
168 | '', |
||
169 | 'Now, give the name of the new entity class (eg <comment>Image</comment>)', |
||
170 | ] |
||
171 | ); |
||
172 | |||
173 | // Get the new class name and validate it. |
||
174 | $question = new Question( |
||
175 | $questionHelper->getQuestion('The entity name', $input->getOption('entity_name')), |
||
176 | $input->getOption('entity_name') |
||
177 | ); |
||
178 | $question->setValidator( |
||
179 | function ($answer) { |
||
180 | // Should only contain letters. |
||
181 | $valid = preg_match('/^[a-zA-Z]+$/', $answer); |
||
182 | if (!$valid) { |
||
183 | throw new \RuntimeException( |
||
184 | 'The class name should only contain letters' |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | return $answer; |
||
189 | } |
||
190 | ); |
||
191 | $question->setNormalizer( |
||
192 | function ($value) { |
||
193 | return $value ? trim($value) : ''; |
||
194 | } |
||
195 | ); |
||
196 | |||
197 | $notificationName = $questionHelper->ask($input, $output, $question); |
||
198 | $input->setOption('entity_name', $notificationName); |
||
199 | } |
||
200 | // Should be all ready to generate |
||
201 | |||
202 | // At the end we need the bundle and the entity |
||
203 | if (empty($this->entityName) || empty($this->bundle)) { |
||
204 | throw new \RuntimeException( |
||
205 | 'Required options have not been set' |
||
206 | ); |
||
207 | } |
||
208 | } |
||
209 | |||
296 | } |
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.