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