Skip to content

Commit e176918

Browse files
Merge #250
250: Start fixing PHPMD issues r=brunoocasali a=brunoocasali Fix part of the phpmd issues (most of the simpler ones) like long variable names, too many lines in the same method, and useless else conditions. Co-authored-by: Bruno Casali <[email protected]>
2 parents 386845e + 4061979 commit e176918

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed

src/Collection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
namespace Meilisearch\Bundle;
66

7+
/**
8+
* This class was extracted from illuminate.
9+
* This will suppress all the PMD warnings in this class.
10+
*
11+
* @SuppressWarnings(PHPMD)
12+
*/
713
final class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
814
{
915
/**

src/Command/MeilisearchClearCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3838
foreach ($indexToClear as $index) {
3939
$indexName = $index['name'];
4040
$className = $index['class'];
41+
$msg = "Cleared <info>$indexName</info> index of <comment>$className</comment>";
4142
$array = $this->searchService->clear($className);
43+
4244
if ('failed' === $array['status']) {
43-
$output->writeln('<error>Index <info>'.$indexName.'</info> couldn\'t be cleared</error>');
44-
} else {
45-
$output->writeln('Cleared <info>'.$indexName.'</info> index of <comment>'.$className.'</comment>');
45+
$msg = "<error>Index <info>$indexName</info> couldn\'t be cleared</error>";
4646
}
47+
48+
$output->writeln($msg);
4749
}
4850

4951
if (0 === count($indexToClear)) {

src/Command/MeilisearchCreateCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ protected function configure(): void
4242
->addOption('indices', 'i', InputOption::VALUE_OPTIONAL, 'Comma-separated list of index names');
4343
}
4444

45-
protected function execute(InputInterface $input, OutputInterface $output): int
45+
private function entitiesToIndex($indexes)
4646
{
47-
$indexes = $this->getEntitiesFromArgs($input, $output);
48-
4947
foreach ($indexes as $key => $index) {
5048
$entityClassName = $index['class'];
5149
if (is_subclass_of($entityClassName, Aggregator::class)) {
@@ -61,7 +59,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6159
}
6260
}
6361

64-
$entitiesToIndex = array_unique($indexes->all(), SORT_REGULAR);
62+
return array_unique($indexes->all(), SORT_REGULAR);
63+
}
64+
65+
protected function execute(InputInterface $input, OutputInterface $output): int
66+
{
67+
$indexes = $this->getEntitiesFromArgs($input, $output);
68+
$entitiesToIndex = $this->entitiesToIndex($indexes);
6569

6670
/** @var array $index */
6771
foreach ($entitiesToIndex as $index) {

src/Command/MeilisearchImportCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
110110
$repository = $manager->getRepository($entityClassName);
111111
$classMetadata = $manager->getClassMetadata($entityClassName);
112112
$entityIdentifiers = $classMetadata->getIdentifierFieldNames();
113-
$sortByIdentifiersParam = array_combine($entityIdentifiers, array_fill(0, count($entityIdentifiers), 'ASC'));
113+
$sortByAttrs = array_combine($entityIdentifiers, array_fill(0, count($entityIdentifiers), 'ASC'));
114114

115115
$output->writeln('<info>Importing for index '.$entityClassName.'</info>');
116116

@@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
129129
do {
130130
$entities = $repository->findBy(
131131
[],
132-
$sortByIdentifiersParam,
132+
$sortByAttrs,
133133
$batchSize,
134134
$batchSize * $page
135135
);

src/DependencyInjection/MeilisearchExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ public function load(array $configs, ContainerBuilder $container): void
3838
$container->setParameter('meili_api_key', $config['api_key'] ?? null);
3939
$container->setParameter('meili_symfony_version', MeilisearchBundle::qualifiedVersion());
4040

41-
if (\count($doctrineSubscribedEvents = $config['doctrineSubscribedEvents']) > 0) {
42-
$container->getDefinition('search.search_indexer_subscriber')->setArgument(1, $doctrineSubscribedEvents);
41+
if (\count($doctrineEvents = $config['doctrineSubscribedEvents']) > 0) {
42+
$container->getDefinition('search.search_indexer_subscriber')->setArgument(1, $doctrineEvents);
4343
} else {
4444
$container->removeDefinition('search.search_indexer_subscriber');
4545
}
4646

4747
$engineDefinition = new Definition(Engine::class, [new Reference('search.client')]);
4848

49-
$searchServiceDefinition = (new Definition(
49+
$searchDefinition = (new Definition(
5050
MeilisearchService::class,
5151
[new Reference($config['serializer']), $engineDefinition, $config]
5252
));
5353

54-
$container->setDefinition('search.service', $searchServiceDefinition->setPublic(true));
54+
$container->setDefinition('search.service', $searchDefinition->setPublic(true));
5555
}
5656
}

src/Services/MeilisearchService.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class MeilisearchService implements SearchService
3030
private array $searchableEntities;
3131
private array $entitiesAggregators;
3232
private array $aggregators;
33-
private array $classToSerializerGroupMapping;
33+
private array $classToSerializerGroup;
3434
private array $indexIfMapping;
3535

3636
public function __construct(NormalizerInterface $normalizer, Engine $engine, array $configuration)
@@ -42,7 +42,7 @@ public function __construct(NormalizerInterface $normalizer, Engine $engine, arr
4242

4343
$this->setSearchableEntities();
4444
$this->setAggregatorsAndEntitiesAggregators();
45-
$this->setClassToSerializerGroupMapping();
45+
$this->setClassToSerializerGroup();
4646
$this->setIndexIfMapping();
4747
}
4848

@@ -84,26 +84,26 @@ public function index(ObjectManager $objectManager, $searchable): array
8484
$searchable = is_array($searchable) ? $searchable : [$searchable];
8585
$searchable = array_merge($searchable, $this->getAggregatorsFromEntities($objectManager, $searchable));
8686

87-
$searchableToBeIndexed = array_filter(
87+
$dataToIndex = array_filter(
8888
$searchable,
8989
fn ($entity) => $this->isSearchable($entity)
9090
);
9191

92-
$searchableToBeRemoved = [];
93-
foreach ($searchableToBeIndexed as $key => $entity) {
92+
$dataToRemove = [];
93+
foreach ($dataToIndex as $key => $entity) {
9494
if (!$this->shouldBeIndexed($entity)) {
95-
unset($searchableToBeIndexed[$key]);
96-
$searchableToBeRemoved[] = $entity;
95+
unset($dataToIndex[$key]);
96+
$dataToRemove[] = $entity;
9797
}
9898
}
9999

100-
if (count($searchableToBeRemoved) > 0) {
101-
$this->remove($objectManager, $searchableToBeRemoved);
100+
if (count($dataToRemove) > 0) {
101+
$this->remove($objectManager, $dataToRemove);
102102
}
103103

104104
return $this->makeSearchServiceResponseFrom(
105105
$objectManager,
106-
$searchableToBeIndexed,
106+
$dataToIndex,
107107
fn ($chunk) => $this->engine->index($chunk)
108108
);
109109
}
@@ -165,17 +165,17 @@ public function search(
165165
throw new ObjectIdNotFoundException(sprintf('There is no "%s" key in the result.', self::RESULT_KEY_OBJECTID));
166166
}
167167

168+
$documentId = $hit[self::RESULT_KEY_OBJECTID];
169+
$entityClass = $className;
170+
168171
if (in_array($className, $this->aggregators, true)) {
169172
$objectId = $hit[self::RESULT_KEY_OBJECTID];
170173
$entityClass = $className::getEntityClassFromObjectId($objectId);
171-
$id = $className::getEntityIdFromObjectId($objectId);
172-
} else {
173-
$id = $hit[self::RESULT_KEY_OBJECTID];
174-
$entityClass = $className;
174+
$documentId = $className::getEntityIdFromObjectId($objectId);
175175
}
176176

177177
$repo = $objectManager->getRepository($entityClass);
178-
$entity = $repo->find($id);
178+
$entity = $repo->find($documentId);
179179

180180
if (null !== $entity) {
181181
$results[] = $entity;
@@ -251,15 +251,15 @@ private function setAggregatorsAndEntitiesAggregators(): void
251251
$this->aggregators = array_unique($this->aggregators);
252252
}
253253

254-
private function setClassToSerializerGroupMapping(): void
254+
private function setClassToSerializerGroup(): void
255255
{
256256
$mapping = [];
257257

258258
/** @var array $indexDetails */
259259
foreach ($this->configuration->get('indices') as $indexDetails) {
260260
$mapping[$indexDetails['class']] = $indexDetails['enable_serializer_groups'];
261261
}
262-
$this->classToSerializerGroupMapping = $mapping;
262+
$this->classToSerializerGroup = $mapping;
263263
}
264264

265265
private function setIndexIfMapping(): void
@@ -332,7 +332,7 @@ private function makeSearchServiceResponseFrom(
332332

333333
private function canUseSerializerGroup(string $className): bool
334334
{
335-
return $this->classToSerializerGroupMapping[$className];
335+
return $this->classToSerializerGroup[$className];
336336
}
337337

338338
private function assertIsSearchable(string $className): void

0 commit comments

Comments
 (0)