From b4dd7298747696a642e7ac03563bea6a6945107d Mon Sep 17 00:00:00 2001 From: markus-moser Date: Wed, 22 May 2024 16:44:51 +0200 Subject: [PATCH] SonarCloud fixes --- .../Compiler/SearchModifierHandlerPass.php | 62 ++++++++++------- .../Compiler/ServiceLocatorPass.php | 12 +++- .../SearchResult/AssetSearchResultItem.php | 4 -- .../DataObjectSearchResultItem.php | 3 - .../SearchResult/DocumentSearchResultItem.php | 4 -- src/QueryLanguage/Pql/Lexer.php | 68 +++++-------------- src/Service/SearchIndex/IndexQueueService.php | 3 + 7 files changed, 68 insertions(+), 88 deletions(-) diff --git a/src/DependencyInjection/Compiler/SearchModifierHandlerPass.php b/src/DependencyInjection/Compiler/SearchModifierHandlerPass.php index f5f2cd86..d00e0ce2 100644 --- a/src/DependencyInjection/Compiler/SearchModifierHandlerPass.php +++ b/src/DependencyInjection/Compiler/SearchModifierHandlerPass.php @@ -24,6 +24,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\Search\Modifier\SearchModifierServiceInterface; use ReflectionClass; use ReflectionException; +use ReflectionMethod; use ReflectionNamedType; use ReflectionUnionType; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -73,31 +74,7 @@ private function guessHandledClasses( string $serviceId, string $methodName ): iterable { - try { - $method = $handlerClass->getMethod($methodName); - } catch (ReflectionException) { - throw new RuntimeException( - sprintf( - 'Invalid handler service "%s": class "%s" must have an "%s()" method.', - $serviceId, - $handlerClass->getName(), - $methodName - ) - ); - } - - if ($method->getNumberOfRequiredParameters() !== 2) { - throw new RuntimeException( - sprintf( - 'Invalid handler service "%s": method "%s::%s()" requires exactly two arguments, ' . - 'first one being the search modifier model it handles and ' . - 'second one the SearchModifierContext object.', - $serviceId, - $handlerClass->getName(), - $methodName - ) - ); - } + $method = $this->getMethodFromHandlerClass($handlerClass, $methodName, $serviceId); $parameters = $method->getParameters(); @@ -174,6 +151,41 @@ private function guessHandledClasses( return [$searchModifierType->getName()]; } + private function getMethodFromHandlerClass( + ReflectionClass $handlerClass, + string $methodName, + string $serviceId + ): ReflectionMethod + { + try { + $method = $handlerClass->getMethod($methodName); + } catch (ReflectionException) { + throw new RuntimeException( + sprintf( + 'Invalid handler service "%s": class "%s" must have an "%s()" method.', + $serviceId, + $handlerClass->getName(), + $methodName + ) + ); + } + + if ($method->getNumberOfRequiredParameters() !== 2) { + throw new RuntimeException( + sprintf( + 'Invalid handler service "%s": method "%s::%s()" requires exactly two arguments, ' . + 'first one being the search modifier model it handles and ' . + 'second one the SearchModifierContext object.', + $serviceId, + $handlerClass->getName(), + $methodName + ) + ); + } + + return $method; + } + private function checkArgumentInstanceOf( ReflectionNamedType|ReflectionUnionType|null $type, string $classOrInterface, diff --git a/src/DependencyInjection/Compiler/ServiceLocatorPass.php b/src/DependencyInjection/Compiler/ServiceLocatorPass.php index 057b7ea5..52f6fa59 100644 --- a/src/DependencyInjection/Compiler/ServiceLocatorPass.php +++ b/src/DependencyInjection/Compiler/ServiceLocatorPass.php @@ -18,6 +18,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Enum\DependencyInjection\ServiceTag; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\QueryLanguage\FieldNameTransformerInterface; +use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\QueryLanguage\FieldNameValidatorInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException; @@ -32,6 +33,12 @@ final class ServiceLocatorPass implements CompilerPassInterface * @param ContainerBuilder $container */ public function process(ContainerBuilder $container): void + { + $this->handleServiceLocatorDefinitions($container); + $this->handleTaggedIteratorDefinitions($container); + } + + private function handleServiceLocatorDefinitions(ContainerBuilder $container): void { $definitionList = [ 'pimcore.generic_data_index.object.search_index_field_definition_locator' => @@ -64,9 +71,13 @@ public function process(ContainerBuilder $container): void $serviceLocator = $container->getDefinition($definitionId); $serviceLocator->setArgument(0, $arguments); } + } + private function handleTaggedIteratorDefinitions(ContainerBuilder $container): void + { $definitionList = [ ServiceTag::PQL_FIELD_NAME_TRANSFORMER->value => FieldNameTransformerInterface::class, + ServiceTag::PQL_FIELD_NAME_VALIDATOR->value => FieldNameValidatorInterface::class, ]; foreach($definitionList as $serviceTagName => $interfaceName) { @@ -80,6 +91,5 @@ public function process(ContainerBuilder $container): void } } } - } } diff --git a/src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php b/src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php index ab5ec318..c9a1a728 100644 --- a/src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php +++ b/src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php @@ -59,10 +59,6 @@ class AssetSearchResultItem private AssetPermissions $permissions; - public function __construct( - ) { - } - public function getId(): int { return $this->id; diff --git a/src/Model/Search/DataObject/SearchResult/DataObjectSearchResultItem.php b/src/Model/Search/DataObject/SearchResult/DataObjectSearchResultItem.php index 86b6ddf2..ec59e8d7 100644 --- a/src/Model/Search/DataObject/SearchResult/DataObjectSearchResultItem.php +++ b/src/Model/Search/DataObject/SearchResult/DataObjectSearchResultItem.php @@ -56,9 +56,6 @@ class DataObjectSearchResultItem private DataObjectPermissions $permissions; - public function __construct( - ) { - } public function getId(): int { diff --git a/src/Model/Search/Document/SearchResult/DocumentSearchResultItem.php b/src/Model/Search/Document/SearchResult/DocumentSearchResultItem.php index 45b9923f..62898108 100644 --- a/src/Model/Search/Document/SearchResult/DocumentSearchResultItem.php +++ b/src/Model/Search/Document/SearchResult/DocumentSearchResultItem.php @@ -56,10 +56,6 @@ class DocumentSearchResultItem private DocumentPermissions $permissions; - public function __construct( - ) { - } - public function getId(): int { return $this->id; diff --git a/src/QueryLanguage/Pql/Lexer.php b/src/QueryLanguage/Pql/Lexer.php index 37795891..b7619e61 100644 --- a/src/QueryLanguage/Pql/Lexer.php +++ b/src/QueryLanguage/Pql/Lexer.php @@ -45,7 +45,6 @@ class Lexer extends AbstractLexer implements LexerInterface { private const REGEX_FIELD_NAME = '[a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*'; - //const REGEX_RELATION_FIELD = '[a-zA-Z_]\w*(?:\:[a-zA-Z_]\w*)(?:\.[a-zA-Z_]\w*)*'; private const REGEX_RELATION_FIELD = self::REGEX_FIELD_NAME . '(?:\:[a-zA-Z_]\w*)(?:\.[a-zA-Z_]\w*)+'; private const REGEX_QUERY_STRING = 'query\(\"(?:.*?)\"\)'; @@ -90,76 +89,44 @@ protected function getNonCatchablePatterns(): array */ protected function getType(&$value): QueryTokenType { - $tokenType = QueryTokenType::T_NONE; - // Check for specific words or characters to assign token types switch (true) { case is_numeric($value): - $tokenType = $this->isIntegerString($value) ? QueryTokenType::T_INTEGER : QueryTokenType::T_FLOAT; - - break; + return $this->isIntegerString($value) ? QueryTokenType::T_INTEGER : QueryTokenType::T_FLOAT; case strlen($value)>1 && in_array($value[0], ["'", '"']) && $value[strlen($value)-1] === $value[0]: $value = substr($value, 1, -1); $value = str_replace(["''", '""'], ["'", '"'], $value); - $tokenType = QueryTokenType::T_STRING; - - break; + return QueryTokenType::T_STRING; case str_starts_with(strtolower($value), 'query("'): $value = substr($value, 7, -2); - $tokenType = QueryTokenType::T_QUERY_STRING; - - break; + return QueryTokenType::T_QUERY_STRING; case $value === '(': - $tokenType = QueryTokenType::T_LPAREN; - - break; + return QueryTokenType::T_LPAREN; case $value === ')': - $tokenType = QueryTokenType::T_RPAREN; - - break; + return QueryTokenType::T_RPAREN; case strtolower($value) === 'and': - $tokenType = QueryTokenType::T_AND; - - break; + return QueryTokenType::T_AND; case strtolower($value) === 'or': - $tokenType = QueryTokenType::T_OR; - - break; + return QueryTokenType::T_OR; case $value === '=': - $tokenType = QueryTokenType::T_EQ; - - break; + return QueryTokenType::T_EQ; case $value === '>': - $tokenType = QueryTokenType::T_GT; - - break; + return QueryTokenType::T_GT; case $value === '<': - $tokenType = QueryTokenType::T_LT; - - break; + return QueryTokenType::T_LT; case $value === '>=': - $tokenType = QueryTokenType::T_GTE; - - break; + return QueryTokenType::T_GTE; case $value === '<=': - $tokenType = QueryTokenType::T_LTE; - - break; + return QueryTokenType::T_LTE; case strtolower($value) === 'like': - $tokenType = QueryTokenType::T_LIKE; - - break; + return QueryTokenType::T_LIKE; case preg_match('#' . self::REGEX_RELATION_FIELD . '#', $value): - $tokenType = QueryTokenType::T_RELATION_FIELD; - - break; + return QueryTokenType::T_RELATION_FIELD; case preg_match('#' . self::REGEX_FIELD_NAME . '#', $value): - $tokenType = QueryTokenType::T_FIELDNAME; - - break; + return QueryTokenType::T_FIELDNAME; + default: + return QueryTokenType::T_NONE; } - - return $tokenType; } public function getTokens(): array @@ -167,7 +134,6 @@ public function getTokens(): array $tokens = []; $this->moveNext(); while ($this->lookahead !== null) { - //p_r("Token: " . (string)$this->lookahead['type']->value . " - Value: " . $this->lookahead['value']); $tokens[] = $this->lookahead; $this->moveNext(); } diff --git a/src/Service/SearchIndex/IndexQueueService.php b/src/Service/SearchIndex/IndexQueueService.php index 3435ece6..88a943e0 100644 --- a/src/Service/SearchIndex/IndexQueueService.php +++ b/src/Service/SearchIndex/IndexQueueService.php @@ -20,6 +20,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue; use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation; use Pimcore\Bundle\GenericDataIndexBundle\Exception\IndexDataException; +use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidArgumentException; use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\BulkOperationServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\PathServiceInterface; @@ -134,6 +135,8 @@ private function doHandleIndexData(ElementInterface $element, string $operation) $this->indexService->deleteFromIndex($element); break; + default: + throw new InvalidArgumentException(sprintf('Operation %s not valid', $operation)); } }