| Conditions | 10 |
| Paths | 16 |
| Total Lines | 54 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 41 | public function execute(Arguments $args, ConsoleIo $io): ?int |
||
| 42 | { |
||
| 43 | $registry = new SearchRegistry(); |
||
| 44 | $adapters = array_keys((array)Configure::read('Search.adapters')); |
||
| 45 | if ($args->hasOption('adapters')) { |
||
| 46 | $adapters = explode(',', (string)$args->getOption('adapters')); |
||
| 47 | } |
||
| 48 | |||
| 49 | foreach ($adapters as $name) { |
||
| 50 | /** @var array<string, mixed> $config */ |
||
| 51 | $config = (array)Configure::read(sprintf('Search.adapters.%s', $name)); |
||
| 52 | if (empty($config)) { |
||
| 53 | $io->warning(sprintf('Missing configuration for adapter "%s", skipping index update', $name)); |
||
| 54 | |||
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | $adapter = $registry->load($name, $config); |
||
| 59 | if (!$adapter instanceof ElasticSearchAdapter) { |
||
| 60 | $io->warning(sprintf( |
||
| 61 | 'Adapter "%s" is not an instance of %s, skipping index update', |
||
| 62 | $name, |
||
| 63 | ElasticSearchAdapter::class, |
||
| 64 | )); |
||
| 65 | |||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | $index = $adapter->getIndex(); |
||
| 70 | if (!$index->indexExists()) { |
||
| 71 | if (!$args->getOption('create')) { |
||
| 72 | $io->warning(sprintf('Index "%s" for adapter "%s" does not exist', $index->getName(), $name)); |
||
| 73 | |||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | if (!$index->create()) { |
||
| 78 | $io->error(sprintf('Error creating missing index "%s" for adapter "%s"', $index->getName(), $name)); |
||
| 79 | } else { |
||
| 80 | $io->success(sprintf('Created missing index "%s" for adapter "%s"', $index->getName(), $name)); |
||
| 81 | } |
||
| 82 | |||
| 83 | continue; |
||
| 84 | } |
||
| 85 | if (!$index->updateProperties() || !$index->updateAnalysis()) { |
||
| 86 | $io->error(sprintf('Error updating index "%s" for adapter "%s"', $index->getName(), $name)); |
||
| 87 | |||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $io->success(sprintf('Updated index "%s" for adapter "%s"', $index->getName(), $name)); |
||
| 92 | } |
||
| 93 | |||
| 94 | return static::CODE_SUCCESS; |
||
| 95 | } |
||
| 97 |