| Conditions | 8 |
| Paths | 8 |
| Total Lines | 103 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 47 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 48 | { |
||
| 49 | $io = new SymfonyStyle($input, $output); |
||
| 50 | $io->section('Welcome to the ONGRElasticsearchBundle document generator'); |
||
| 51 | $io->writeln( |
||
| 52 | [ |
||
| 53 | '', |
||
| 54 | 'This command helps you generate ONGRElasticsearchBundle documents.', |
||
| 55 | '', |
||
| 56 | 'First, you need to give the document name you want to generate.', |
||
| 57 | 'You must use the shortcut notation like <comment>AcmeDemoBundle:Post</comment>.', |
||
| 58 | '', |
||
| 59 | ] |
||
| 60 | ); |
||
| 61 | |||
| 62 | /** @var Kernel $kernel */ |
||
| 63 | $kernel = $this->getContainer()->get('kernel'); |
||
| 64 | $bundleNames = array_keys($kernel->getBundles()); |
||
| 65 | |||
| 66 | while (true) { |
||
| 67 | $question = new Question('The Document shortcut name'); |
||
| 68 | $question |
||
| 69 | ->setValidator([$this, 'validateDocumentName']) |
||
| 70 | ->setAutocompleterValues($bundleNames); |
||
| 71 | |||
| 72 | $document = $io->askQuestion($question); |
||
| 73 | |||
| 74 | list($bundle, $document) = $this->parseShortcutNotation($document); |
||
| 75 | |||
| 76 | if (in_array(strtolower($document), $this->getReservedKeywords())) { |
||
| 77 | $io->error(sprintf('"%s" is a reserved word.', $document)); |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | try { |
||
| 82 | if (!file_exists( |
||
| 83 | $kernel->getBundle($bundle)->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php' |
||
| 84 | )) { |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | |||
| 88 | $io->error(sprintf('Document "%s:%s" already exists.', $bundle, $document)); |
||
| 89 | } catch (\Exception $e) { |
||
| 90 | $io->error(sprintf('Bundle "%s" does not exist.', $bundle)); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $question = new Question('Document type in Elasticsearch', lcfirst($document)); |
||
|
|
|||
| 95 | $question->setValidator([$this, 'validateFieldName']); |
||
| 96 | $documentType = $io->askQuestion($question); |
||
| 97 | |||
| 98 | $properties = []; |
||
| 99 | |||
| 100 | while (true) { |
||
| 101 | $question = new Question( |
||
| 102 | 'Enter property name [<comment>No property will be added if empty!</comment>]', |
||
| 103 | false |
||
| 104 | ); |
||
| 105 | |||
| 106 | if (!$field = $io->askQuestion($question)) { |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | |||
| 110 | try { |
||
| 111 | $this->validateFieldName($field); |
||
| 112 | } catch (\InvalidArgumentException $e) { |
||
| 113 | $io->error($e->getMessage()); |
||
| 114 | continue; |
||
| 115 | } |
||
| 116 | |||
| 117 | $question = new Question('Enter property name in Elasticsearch', $field); |
||
| 118 | $question->setValidator([$this, 'validateFieldName']); |
||
| 119 | $name = $io->askQuestion($question); |
||
| 120 | |||
| 121 | $question = new Question('Enter property type', 'string'); |
||
| 122 | $question |
||
| 123 | ->setAutocompleterValues($this->getPropertyTypes()) |
||
| 124 | ->setValidator([$this, 'validatePropertyType']); |
||
| 125 | $type = $io->askQuestion($question); |
||
| 126 | |||
| 127 | $question = new Question( |
||
| 128 | 'Enter property options [<comment>No options will be added if empty!</comment>]', |
||
| 129 | false |
||
| 130 | ); |
||
| 131 | |||
| 132 | $options = $io->askQuestion($question); |
||
| 133 | |||
| 134 | $properties[] = [ |
||
| 135 | 'annotation' => 'Property', |
||
| 136 | 'field_name' => $field, |
||
| 137 | 'property_name' => $name, |
||
| 138 | 'property_type' => $type, |
||
| 139 | 'property_options' => $options, |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->getContainer()->get('es.generate')->generate( |
||
| 144 | $this->getContainer()->get('kernel')->getBundle($bundle), |
||
| 145 | $document, |
||
| 146 | $documentType, |
||
| 147 | $properties |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 342 |
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: