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