Conditions | 19 |
Paths | 16 |
Total Lines | 197 |
Code Lines | 136 |
Lines | 26 |
Ratio | 13.2 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
55 | protected function interact(InputInterface $input, OutputInterface $output) |
||
56 | { |
||
57 | /** @var FormatterHelper $formatter */ |
||
58 | $formatter = $this->getHelperSet()->get('formatter'); |
||
59 | $this->questionHelper = new QuestionHelper(); |
||
60 | |||
61 | $output->writeln( |
||
62 | [ |
||
63 | '', |
||
64 | $formatter->formatBlock('Welcome to the ONGRElasticsearchBundle document generator', 'bg=blue', true), |
||
65 | '', |
||
66 | 'This command helps you generate ONGRElasticsearchBundle documents.', |
||
67 | '', |
||
68 | 'First, you need to give the document name you want to generate.', |
||
69 | 'You must use the shortcut notation like <comment>AcmeDemoBundle:Post</comment>.', |
||
70 | '', |
||
71 | ] |
||
72 | ); |
||
73 | |||
74 | /** @var Kernel $kernel */ |
||
75 | $kernel = $this->getContainer()->get('kernel'); |
||
76 | $bundleNames = array_keys($kernel->getBundles()); |
||
77 | |||
78 | while (true) { |
||
79 | $document = $this->questionHelper->ask( |
||
80 | $input, |
||
81 | $output, |
||
82 | $this->getQuestion('The Document shortcut name', null, [$this, 'validateDocumentName'], $bundleNames) |
||
83 | ); |
||
84 | |||
85 | list($bundle, $document) = $this->parseShortcutNotation($document); |
||
86 | |||
87 | if (in_array(strtolower($document), $this->getReservedKeywords())) { |
||
88 | $output->writeln($this->getException('"%s" is a reserved word.', [$document])->getMessage()); |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | try { |
||
93 | if (!file_exists( |
||
94 | $kernel->getBundle($bundle)->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php' |
||
95 | )) { |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | $output->writeln( |
||
100 | $this->getException('Document "%s:%s" already exists.', [$bundle, $document])->getMessage() |
||
101 | ); |
||
102 | } catch (\Exception $e) { |
||
103 | $output->writeln($this->getException('Bundle "%s" does not exist.', [$bundle])->getMessage()); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $output->writeln($this->getOptionsLabel($this->getDocumentAnnotations(), 'Available types')); |
||
108 | $annotation = $this->questionHelper->ask( |
||
109 | $input, |
||
110 | $output, |
||
111 | $this->getQuestion( |
||
112 | 'Document type', |
||
113 | 'Document', |
||
114 | [$this, 'validateDocumentAnnotation'], |
||
115 | $this->getDocumentAnnotations() |
||
116 | ) |
||
117 | ); |
||
118 | |||
119 | $documentType = lcfirst($document); |
||
|
|||
120 | if ($annotation == 'Document') { |
||
121 | $documentType = $this->questionHelper->ask( |
||
122 | $input, |
||
123 | $output, |
||
124 | $this->getQuestion( |
||
125 | "\n" . 'Elasticsearch Document name', |
||
126 | lcfirst($document), |
||
127 | [$this, 'validateFieldName'] |
||
128 | ) |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | $properties = []; |
||
133 | $output->writeln(['', $formatter->formatBlock('New Document Property', 'bg=blue;fg=white', true)]); |
||
134 | |||
135 | while (true) { |
||
136 | $property = []; |
||
137 | $question = $this->getQuestion( |
||
138 | 'Property name [<comment>press <info><return></info> to stop</comment>]', |
||
139 | false |
||
140 | ); |
||
141 | |||
142 | if (!$field = $this->questionHelper->ask($input, $output, $question)) { |
||
143 | break; |
||
144 | } |
||
145 | |||
146 | foreach ($properties as $previousProperty) { |
||
147 | if ($previousProperty['field_name'] == $field) { |
||
148 | $output->writeln($this->getException('Duplicate field name "%s"', [$field])->getMessage()); |
||
149 | continue(2); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | try { |
||
154 | $this->validateFieldName($field); |
||
155 | } catch (\InvalidArgumentException $e) { |
||
156 | $output->writeln($e->getMessage()); |
||
157 | continue; |
||
158 | } |
||
159 | |||
160 | $output->writeln($this->getOptionsLabel($this->getPropertyAnnotations(), 'Available annotations')); |
||
161 | $property['annotation'] = $this->questionHelper->ask( |
||
162 | $input, |
||
163 | $output, |
||
164 | $this->getQuestion( |
||
165 | 'Property annotation', |
||
166 | 'Property', |
||
167 | [$this, 'validatePropertyAnnotation'], |
||
168 | $this->getPropertyAnnotations() |
||
169 | ) |
||
170 | ); |
||
171 | |||
172 | $property['field_name'] = $property['property_name'] = $field; |
||
173 | |||
174 | switch ($property['annotation']) { |
||
175 | case 'Embedded': |
||
176 | $property['property_name'] = $this->askForPropertyName($input, $output, $property['field_name']); |
||
177 | $property['property_class'] = $this->askForPropertyClass($input, $output); |
||
178 | |||
179 | $question = new ConfirmationQuestion("\n<info>Multiple</info> [<comment>no</comment>]: ", false); |
||
180 | $question->setAutocompleterValues(['yes', 'no']); |
||
181 | $property['property_multiple'] = $this->questionHelper->ask($input, $output, $question); |
||
182 | |||
183 | $property['property_options'] = $this->askForPropertyOptions($input, $output); |
||
184 | break; |
||
185 | View Code Duplication | case 'ParentDocument': |
|
186 | if (!$this->isUniqueAnnotation($properties, $property['annotation'])) { |
||
187 | $output->writeln( |
||
188 | $this |
||
189 | ->getException('Only one "%s" field can be added', [$property['annotation']]) |
||
190 | ->getMessage() |
||
191 | ); |
||
192 | continue(2); |
||
193 | } |
||
194 | $property['property_class'] = $this->askForPropertyClass($input, $output); |
||
195 | break; |
||
196 | case 'Property': |
||
197 | $property['property_name'] = $this->askForPropertyName($input, $output, $property['field_name']); |
||
198 | |||
199 | $output->writeln($this->getOptionsLabel($this->getPropertyTypes(), 'Available types')); |
||
200 | $property['property_type'] = $this->questionHelper->ask( |
||
201 | $input, |
||
202 | $output, |
||
203 | $this->getQuestion( |
||
204 | 'Property type', |
||
205 | 'string', |
||
206 | [$this, 'validatePropertyType'], |
||
207 | $this->getPropertyTypes() |
||
208 | ) |
||
209 | ); |
||
210 | |||
211 | $property['property_options'] = $this->askForPropertyOptions($input, $output); |
||
212 | break; |
||
213 | View Code Duplication | case 'Ttl': |
|
214 | if (!$this->isUniqueAnnotation($properties, $property['annotation'])) { |
||
215 | $output->writeln( |
||
216 | $this |
||
217 | ->getException('Only one "%s" field can be added', [$property['annotation']]) |
||
218 | ->getMessage() |
||
219 | ); |
||
220 | continue(2); |
||
221 | } |
||
222 | $property['property_default'] = $this->questionHelper->ask( |
||
223 | $input, |
||
224 | $output, |
||
225 | $this->getQuestion("\n" . 'Default time to live') |
||
226 | ); |
||
227 | break; |
||
228 | case 'Id': |
||
229 | if (!$this->isUniqueAnnotation($properties, $property['annotation'])) { |
||
230 | $output->writeln( |
||
231 | $this |
||
232 | ->getException('Only one "%s" field can be added', [$property['annotation']]) |
||
233 | ->getMessage() |
||
234 | ); |
||
235 | continue(2); |
||
236 | } |
||
237 | break; |
||
238 | } |
||
239 | |||
240 | $properties[] = $property; |
||
241 | $output->writeln(['', $formatter->formatBlock('New Document Property', 'bg=blue;fg=white', true)]); |
||
242 | } |
||
243 | |||
244 | $this->getContainer()->get('es.generate')->generate( |
||
245 | $this->getContainer()->get('kernel')->getBundle($bundle), |
||
246 | $document, |
||
247 | $annotation, |
||
248 | $documentType, |
||
249 | $properties |
||
250 | ); |
||
251 | } |
||
252 | |||
679 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: