Conditions | 8 |
Paths | 10 |
Total Lines | 69 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 2 |
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 |
||
54 | protected function execute(InputInterface $input, OutputInterface $output) |
||
55 | { |
||
56 | $io = new SymfonyStyle($input, $output); |
||
57 | $manager = $this->getManager($input->getOption('manager')); |
||
58 | $originalIndexName = $manager->getIndexName(); |
||
59 | |||
60 | if ($input->getOption('time')) { |
||
61 | /** @var IndexSuffixFinder $finder */ |
||
62 | $finder = $this->getContainer()->get('es.client.index_suffix_finder'); |
||
63 | $finder->setNextFreeIndex($manager); |
||
64 | } |
||
65 | |||
66 | if ($input->getOption('if-not-exists') && $manager->indexExists()) { |
||
67 | $io->note( |
||
68 | sprintf( |
||
69 | 'Index `%s` already exists in `%s` manager.', |
||
70 | $manager->getIndexName(), |
||
71 | $input->getOption('manager') |
||
72 | ) |
||
73 | ); |
||
74 | |||
75 | return 0; |
||
76 | } |
||
77 | |||
78 | $manager->createIndex($input->getOption('no-mapping')); |
||
79 | |||
80 | $io->text( |
||
81 | sprintf( |
||
82 | 'Created `<comment>%s</comment>` index for the `<comment>%s</comment>` manager. ', |
||
83 | $manager->getIndexName(), |
||
84 | $input->getOption('manager') |
||
85 | ) |
||
86 | ); |
||
87 | |||
88 | if ($input->getOption('alias') && $originalIndexName != $manager->getIndexName()) { |
||
89 | $params['body'] = [ |
||
|
|||
90 | 'actions' => [ |
||
91 | [ |
||
92 | 'add' => [ |
||
93 | 'index' => $manager->getIndexName(), |
||
94 | 'alias' => $originalIndexName, |
||
95 | ] |
||
96 | ] |
||
97 | ] |
||
98 | ]; |
||
99 | $message = 'Created an alias `<comment>'.$originalIndexName.'</comment>` for the `<comment>'. |
||
100 | $manager->getIndexName().'</comment>` index. '; |
||
101 | |||
102 | if ($manager->getClient()->indices()->existsAlias(['name' => $originalIndexName])) { |
||
103 | $currentAlias = $manager->getClient()->indices()->getAlias( |
||
104 | [ |
||
105 | 'name' => $originalIndexName, |
||
106 | ] |
||
107 | ); |
||
108 | |||
109 | $indexesToRemoveAliases = implode(',', array_keys($currentAlias)); |
||
110 | if (!empty($indexesToRemoveAliases)) { |
||
111 | $params['body']['actions'][]['remove'] = [ |
||
112 | 'index' => $indexesToRemoveAliases, |
||
113 | 'alias' => $originalIndexName, |
||
114 | ]; |
||
115 | $message .= 'Removed `<comment>'.$originalIndexName.'</comment>` alias from `<comment>'. |
||
116 | $indexesToRemoveAliases.'</comment>` index(es).'; |
||
117 | } |
||
118 | } |
||
119 | $manager->getClient()->indices()->updateAliases($params); |
||
120 | $io->text($message); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 |
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.