Conditions | 9 |
Paths | 5 |
Total Lines | 69 |
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 |
||
33 | public function handle(): int |
||
34 | { |
||
35 | $indexName = $this->argument('index-name'); |
||
36 | |||
37 | if ($indexName === null || |
||
38 | !is_string($indexName) || |
||
39 | mb_strlen($indexName) === 0 |
||
40 | ) { |
||
41 | $this->output->writeln( |
||
42 | '<error>Argument index-name must be a non empty string.</error>' |
||
43 | ); |
||
44 | |||
45 | return self::FAILURE; |
||
46 | } |
||
47 | |||
48 | $aliasName = $this->argument('alias-name'); |
||
49 | |||
50 | if ($aliasName === null || |
||
51 | !is_string($aliasName) || |
||
52 | mb_strlen($aliasName) === 0 |
||
53 | ) { |
||
54 | $this->output->writeln( |
||
55 | '<error>Argument alias-name must be a non empty string.</error>' |
||
56 | ); |
||
57 | |||
58 | return self::FAILURE; |
||
59 | } |
||
60 | |||
61 | if (!$this->client->indices()->exists([ |
||
62 | 'index' => $indexName, |
||
63 | ])) { |
||
64 | $this->output->writeln( |
||
65 | sprintf( |
||
66 | '<error>Index %s doesn\'t exists and alias cannot be created.</error>', |
||
67 | $indexName |
||
68 | ) |
||
69 | ); |
||
70 | |||
71 | return self::FAILURE; |
||
72 | } |
||
73 | |||
74 | try { |
||
75 | $this->client->indices()->putAlias([ |
||
76 | 'index' => $indexName, |
||
77 | 'name' => $aliasName, |
||
78 | ]); |
||
79 | } catch (Throwable $exception) { |
||
80 | $this->output->writeln( |
||
81 | sprintf( |
||
82 | '<error>Error creating alias %s for index %s, exception message: %s.</error>', |
||
83 | $aliasName, |
||
84 | $indexName, |
||
85 | $exception->getMessage() |
||
86 | ) |
||
87 | ); |
||
88 | |||
89 | return self::FAILURE; |
||
90 | } |
||
91 | |||
92 | $this->output->writeln( |
||
93 | sprintf( |
||
94 | '<info>Alias %s created for index %s.</info>', |
||
95 | $aliasName, |
||
96 | $indexName |
||
97 | ) |
||
98 | ); |
||
99 | |||
100 | return self::SUCCESS; |
||
101 | } |
||
102 | } |
||
103 |