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