Skip to content

Commit

Permalink
SonarCloud fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-moser committed May 22, 2024
1 parent cb1d08f commit b4dd729
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 88 deletions.
62 changes: 37 additions & 25 deletions src/DependencyInjection/Compiler/SearchModifierHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion src/DependencyInjection/Compiler/ServiceLocatorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' =>
Expand Down Expand Up @@ -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) {
Expand All @@ -80,6 +91,5 @@ public function process(ContainerBuilder $container): void
}
}
}

}
}
4 changes: 0 additions & 4 deletions src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class AssetSearchResultItem

private AssetPermissions $permissions;

public function __construct(
) {
}

public function getId(): int
{
return $this->id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class DataObjectSearchResultItem

private DataObjectPermissions $permissions;

public function __construct(
) {
}

public function getId(): int
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ class DocumentSearchResultItem

private DocumentPermissions $permissions;

public function __construct(
) {
}

public function getId(): int
{
return $this->id;
Expand Down
68 changes: 17 additions & 51 deletions src/QueryLanguage/Pql/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\(\"(?:.*?)\"\)';
Expand Down Expand Up @@ -90,84 +89,51 @@ 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
{
$tokens = [];
$this->moveNext();
while ($this->lookahead !== null) {
//p_r("Token: " . (string)$this->lookahead['type']->value . " - Value: " . $this->lookahead['value']);
$tokens[] = $this->lookahead;
$this->moveNext();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Service/SearchIndex/IndexQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit b4dd729

Please sign in to comment.