Conditions | 9 |
Paths | 11 |
Total Lines | 81 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
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 execute(InputInterface $input, OutputInterface $output) |
||
67 | { |
||
68 | $io = new SymfonyStyle($input, $output); |
||
69 | $manager = $this->getManager($input->getOption('manager')); |
||
70 | $originalIndexName = $manager->getIndexName(); |
||
71 | |||
72 | if ($input->getOption('dump')) { |
||
73 | $io->note("Index mappings:"); |
||
74 | $io->text( |
||
75 | json_encode( |
||
76 | $manager->getIndexMappings(), |
||
77 | JSON_PRETTY_PRINT |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | return 0; |
||
82 | } |
||
83 | |||
84 | if ($input->getOption('time')) { |
||
85 | /** @var IndexSuffixFinder $finder */ |
||
86 | $finder = $this->getContainer()->get('es.client.index_suffix_finder'); |
||
87 | $finder->setNextFreeIndex($manager); |
||
88 | } |
||
89 | |||
90 | if ($input->getOption('if-not-exists') && $manager->indexExists()) { |
||
91 | $io->note( |
||
92 | sprintf( |
||
93 | 'Index `%s` already exists in `%s` manager.', |
||
94 | $manager->getIndexName(), |
||
95 | $input->getOption('manager') |
||
96 | ) |
||
97 | ); |
||
98 | |||
99 | return 0; |
||
100 | } |
||
101 | |||
102 | $manager->createIndex($input->getOption('no-mapping'), $input->getOption('no-array-mappings')); |
||
103 | |||
104 | $io->text( |
||
105 | sprintf( |
||
106 | 'Created `<comment>%s</comment>` index for the `<comment>%s</comment>` manager. ', |
||
107 | $manager->getIndexName(), |
||
108 | $input->getOption('manager') |
||
109 | ) |
||
110 | ); |
||
111 | |||
112 | if ($input->getOption('alias') && $originalIndexName != $manager->getIndexName()) { |
||
113 | $params['body'] = [ |
||
|
|||
114 | 'actions' => [ |
||
115 | [ |
||
116 | 'add' => [ |
||
117 | 'index' => $manager->getIndexName(), |
||
118 | 'alias' => $originalIndexName, |
||
119 | ] |
||
120 | ] |
||
121 | ] |
||
122 | ]; |
||
123 | $message = 'Created an alias `<comment>'.$originalIndexName.'</comment>` for the `<comment>'. |
||
124 | $manager->getIndexName().'</comment>` index. '; |
||
125 | |||
126 | if ($manager->getClient()->indices()->existsAlias(['name' => $originalIndexName])) { |
||
127 | $currentAlias = $manager->getClient()->indices()->getAlias( |
||
128 | [ |
||
129 | 'name' => $originalIndexName, |
||
130 | ] |
||
131 | ); |
||
132 | |||
133 | $indexesToRemoveAliases = implode(',', array_keys($currentAlias)); |
||
134 | if (!empty($indexesToRemoveAliases)) { |
||
135 | $params['body']['actions'][]['remove'] = [ |
||
136 | 'index' => $indexesToRemoveAliases, |
||
137 | 'alias' => $originalIndexName, |
||
138 | ]; |
||
139 | $message .= 'Removed `<comment>'.$originalIndexName.'</comment>` alias from `<comment>'. |
||
140 | $indexesToRemoveAliases.'</comment>` index(es).'; |
||
141 | } |
||
142 | } |
||
143 | $manager->getClient()->indices()->updateAliases($params); |
||
144 | $io->text($message); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.