| Conditions | 19 |
| Paths | 16 |
| Total Lines | 205 |
| Code Lines | 141 |
| Lines | 26 |
| Ratio | 12.68 % |
| Changes | 3 | ||
| 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( |
||
| 89 | $formatter->formatBlock(sprintf('"%s" is a reserved word.', $document), 'bg=red', true) |
||
| 90 | ); |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | try { |
||
| 95 | if (!file_exists( |
||
| 96 | $kernel->getBundle($bundle)->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php' |
||
| 97 | )) { |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | |||
| 101 | $output->writeln( |
||
| 102 | $formatter->formatBlock( |
||
| 103 | sprintf('Document "%s:%s" already exists.', $bundle, $document), |
||
| 104 | 'bg=red', |
||
| 105 | true |
||
| 106 | ) |
||
| 107 | ); |
||
| 108 | } catch (\Exception $e) { |
||
| 109 | $output->writeln( |
||
| 110 | $formatter->formatBlock(sprintf('Bundle "%s" does not exist.', $bundle), 'bg=red', true) |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $output->writeln($this->getOptionsLabel($this->getDocumentAnnotations(), 'Available types')); |
||
| 116 | $annotation = $this->questionHelper->ask( |
||
| 117 | $input, |
||
| 118 | $output, |
||
| 119 | $this->getQuestion( |
||
| 120 | 'Document type', |
||
| 121 | 'Document', |
||
| 122 | [$this, 'validateDocumentAnnotation'], |
||
| 123 | $this->getDocumentAnnotations() |
||
| 124 | ) |
||
| 125 | ); |
||
| 126 | |||
| 127 | $documentType = lcfirst($document); |
||
|
|
|||
| 128 | if ($annotation == 'Document') { |
||
| 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->getPropertyAnnotations(), 'Available annotations')); |
||
| 169 | $property['annotation'] = $this->questionHelper->ask( |
||
| 170 | $input, |
||
| 171 | $output, |
||
| 172 | $this->getQuestion( |
||
| 173 | 'Property annotation', |
||
| 174 | 'Property', |
||
| 175 | [$this, 'validatePropertyAnnotation'], |
||
| 176 | $this->getPropertyAnnotations() |
||
| 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 | |||
| 678 |
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: