Skip to content

Commit

Permalink
Merge pull request #6 from lmc-eu/feature/use-php81
Browse files Browse the repository at this point in the history
Require php 8.1
  • Loading branch information
MortalFlesh authored Apr 27, 2022
2 parents 60df089 + 48abadb commit 94cb01f
Show file tree
Hide file tree
Showing 23 changed files with 138 additions and 102 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

strategy:
matrix:
php-version: ['7.4']
php-version: ['8.1']
dependencies: ['']
include:
- { php-version: '7.4', dependencies: '--prefer-lowest --prefer-stable' }
- { php-version: '8.1', dependencies: '--prefer-lowest --prefer-stable' }

name: Unit tests - PHP ${{ matrix.dependencies }}

Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: json, mbstring

- name: Install dependencies
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<!-- There should always be "Unreleased" section at the beginning. -->

## Unreleased
- Require php 8.1
- [**BC**] Use new language features and change method signatures

## 2.0.0 - 2022-03-30
- Allow setting an edismax as local parameters in query
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
"license": "MIT",
"type": "library",
"require": {
"php": "^7.4",
"php": "^8.1",
"ext-json": "*",
"ext-mbstring": "*",
"lmc/cqrs-types": "^2.0",
"nelmio/solarium-bundle": "^4.0",
"solarium/solarium": "^5.2"
"lmc/cqrs-types": "^3.0",
"nelmio/solarium-bundle": "^5.0",
"solarium/solarium": "^6.2.3"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.5",
"lmc/coding-standard": "^3.0",
"lmc/coding-standard": "^3.3",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^0.12.83",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpunit/phpunit": "^9.5"
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5.20"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Lmc\CodingStandard\Sniffs\Naming\ClassNameSuffixByParentSniff;
use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;
Expand All @@ -13,7 +12,9 @@

$parameters->set(
Option::SKIP,
['SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff.ReferencedGeneralException' => ['tests/Exception/*.php']]
[
'SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff.ReferencedGeneralException' => ['tests/Exception/*.php'],
]
);

$containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php');
Expand All @@ -26,6 +27,5 @@
$services->set(ClassNameSuffixByParentSniff::class)
->property('extraParentTypesToSuffixes', ['*ApplicatorInterface' => 'Applicator']);

$services->set(NoSuperfluousPhpdocTagsFixer::class)
->call('configure', [['allow_mixed' => true]]);
$containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs-7.4.php');
};
5 changes: 1 addition & 4 deletions src/Handler/SolrQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
class SolrQueryHandler extends AbstractQueryHandler
{
private Client $client;

public function __construct(Client $client)
public function __construct(private Client $client)
{
$this->client = $client;
}

/** @phpstan-param QueryInterface<mixed> $query */
Expand Down
10 changes: 8 additions & 2 deletions src/QueryBuilder/Applicator/FacetsApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ private function setExcludes(AbstractFacet $field, string $facetField, array $fa
}
}

if (!empty($facetExcludes) && method_exists($field, 'setExcludes')) {
$field->setExcludes($facetExcludes);
if (empty($facetExcludes)) {
return;
}

$facetExcludes = array_unique($facetExcludes);

$field
->getLocalParameters()
->setExcludes($facetExcludes);
}
}
7 changes: 4 additions & 3 deletions src/QueryBuilder/Applicator/FulltextApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\EntityInterface;
use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\FulltextInterface;
use Lmc\Cqrs\Solr\ValueObject\LocalParameter;
use Solarium\QueryType\Select\Query\Query;

class FulltextApplicator implements ApplicatorInterface
Expand Down Expand Up @@ -99,12 +100,12 @@ private function setLocalEdisMax(): self
}

if (!empty($phraseFields = $this->entity->getPhraseFields())) {
$localParameters['pf'] = 'pf=$phraseFields';
$localParameters['pf'] = LocalParameter::withPlaceholder('pf', '$phraseFields');
$this->query->addParam('phraseFields', implode(' ', $phraseFields));
}

$localParameters['tie'] = 'tie=' . $this->entity->getTie();
$localParameters['mm'] = 'mm=' . $this->entity->getMinimumMatch();
$localParameters['tie'] = LocalParameter::withValue('tie', (string) $this->entity->getTie());
$localParameters['mm'] = LocalParameter::withValue('mm', $this->entity->getMinimumMatch());

$this->query->addParam('q.alt', $this->entity->getQueryAlternative());

Expand Down
3 changes: 2 additions & 1 deletion src/QueryBuilder/Applicator/FulltextBigramApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\EntityInterface;
use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\FulltextBigramInterface;
use Lmc\Cqrs\Solr\ValueObject\LocalParameter;
use Solarium\QueryType\Select\Query\Query;

class FulltextBigramApplicator implements ApplicatorInterface
Expand Down Expand Up @@ -31,7 +32,7 @@ public function applyOnQuery(Query $query): void
if ($this->entity->useEDisMaxGlobally()) {
$query->getEDisMax()->setPhraseBigramFields(implode(' ', $phraseBigramFields));
} else {
$query->getLocalParameters()->offsetSet('pf2', 'pf2=$phraseBigramFields');
$query->getLocalParameters()->offsetSet('pf2', LocalParameter::withPlaceholder('pf2', '$phraseBigramFields'));
$query->addParam('phraseBigramFields', implode(' ', $phraseBigramFields));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/QueryBuilder/Applicator/FulltextBoostApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\EntityInterface;
use Lmc\Cqrs\Solr\QueryBuilder\EntityInterface\FulltextBoostInterface;
use Lmc\Cqrs\Solr\ValueObject\LocalParameter;
use Solarium\QueryType\Select\Query\Query;

class FulltextBoostApplicator implements ApplicatorInterface
Expand Down Expand Up @@ -44,12 +45,12 @@ public function applyOnQuery(Query $query): void
}
} else {
if (!empty($boostQuery)) {
$query->getLocalParameters()->offsetSet('bq', 'bq=$boostQuery');
$query->getLocalParameters()->offsetSet('bq', LocalParameter::withPlaceholder('bq', '$boostQuery'));
$query->addParam('boostQuery', $this->entity->getBoostQuery());
}

if ($phraseSlop !== null) {
$query->getLocalParameters()->offsetSet('ps', 'ps=$phraseSlop');
$query->getLocalParameters()->offsetSet('ps', LocalParameter::withPlaceholder('ps', '$phraseSlop'));
$query->addParam('phraseSlop', $this->entity->getPhraseSlop());
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/QueryBuilder/Query/BuilderPrototypeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@

final class BuilderPrototypeQuery extends AbstractSolrSelectQuery
{
/** @var ApplicatorInterface[] */
private array $applicators;

/**
* @param ApplicatorInterface[] $applicators
*/
public function __construct(array $applicators)
/** @param ApplicatorInterface[] $applicators */
public function __construct(private array $applicators)
{
$this->applicators = $applicators;
}

public function prepareSelect(Query $select): Query
Expand Down
5 changes: 1 addition & 4 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

class QueryBuilder
{
private ApplicatorFactory $applicatorFactory;

public function __construct(ApplicatorFactory $applicatorFactory)
public function __construct(private ApplicatorFactory $applicatorFactory)
{
$this->applicatorFactory = $applicatorFactory;
}

public function buildQuery(EntityInterface $entity): BuilderPrototypeQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
namespace Lmc\Cqrs\Solr\Solarium\QueryType\Select\ResponseParser;

use Lmc\Cqrs\Solr\Solarium\QueryType\Select\Result\CountDistinctStatsResult;
use Solarium\Component\AbstractComponent;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Component\ResponseParser\ComponentParserInterface;
use Solarium\Component\Result\Stats\FacetValue;
use Solarium\Component\Result\Stats\Stats;
use Solarium\QueryType\Select\Query\Query;

/**
* This class is a copy of the \Solarium\QueryType\Select\ResponseParser\Component\Stats class.
* Only difference is the use of CountDistinctStatsResult class as our implementation of result class.
*/
class CountDistinctStatsResponseParser implements ComponentParserInterface
{
/**
* @param Query $query
* @param object $stats
* @param array $data
* @return Stats
*/
public function parse($query, $stats, $data)
public function parse(?ComponentAwareQueryInterface $query, ?AbstractComponent $component, array $data): Stats
{
$results = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class CountDistinctStatsResult extends Result
{
/**
* @return int|null
*/
public function getCountDistinct()
public function getCountDistinct(): ?int
{
return isset($this->stats['countDistinct']) ? (int) $this->stats['countDistinct'] : null;
}
Expand Down
33 changes: 33 additions & 0 deletions src/ValueObject/LocalParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Lmc\Cqrs\Solr\ValueObject;

use Solarium\Core\Query\LocalParameters\LocalParameter as SolariumLocalParameter;
use Solarium\Core\Query\LocalParameters\LocalParameterInterface;

/** @internal */
class LocalParameter
{
public static function withValue(string $type, string $value): LocalParameterInterface
{
if (array_key_exists($type, SolariumLocalParameter::PARAMETER_MAP)) {
throw new \InvalidArgumentException(
sprintf(
'Local Parameter has defined type "%s" and you should use it directly by LocalParameters::set... method.',
$type
)
);
}

return (new SolariumLocalParameter($type))->addValue(sprintf('%s=%s', $type, $value));
}

public static function withPlaceholder(string $type, string $placeholder): LocalParameterInterface
{
if (!str_starts_with($placeholder, '$')) {
throw new \InvalidArgumentException('Local Parameter placeholder must start with $ sign.');
}

return self::withValue($type, $placeholder);
}
}
18 changes: 7 additions & 11 deletions src/ValueObject/SolrField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
/**
* SolrField is a representation of any data passed by user to Solr (eg. in query or a list of fields returned by Solr)
*/
class SolrField
class SolrField implements \Stringable
{
private string $value;
private string $localParameter;
private int $proximity;
private int $boost;

public function __construct(string $value, string $localParameter = '', int $proximity = 0, int $boost = 0)
{
public function __construct(
private string $value,
private string $localParameter = '',
private int $proximity = 0,
private int $boost = 0
) {
$this->value = $this->escapePhrase($value);
$this->localParameter = $localParameter;
$this->proximity = $proximity;
$this->boost = $boost;
}

public function __toString(): string
Expand Down
7 changes: 1 addition & 6 deletions src/ValueObject/SolrRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

class SolrRequest
{
private AbstractQuery $query;
private ?string $endpoint;

public function __construct(AbstractQuery $query, ?string $endpoint = null)
public function __construct(private AbstractQuery $query, private ?string $endpoint = null)
{
$this->query = $query;
$this->endpoint = $endpoint;
}

public function getQuery(): AbstractQuery
Expand Down
31 changes: 31 additions & 0 deletions tests/AbstractSolrTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Solarium\Client;
use Solarium\Core\Client\Adapter\AdapterInterface;
use Solarium\Core\Query\Result\ResultInterface;
use Solarium\QueryType\Select\Query\Query;

Expand All @@ -19,6 +21,14 @@ protected function setUpClient(): void
$this->client = $this->createMock(Client::class);
}

protected function createSolrClient(): Client
{
return new Client(
$this->createMock(AdapterInterface::class),
$this->createMock(EventDispatcherInterface::class)
);
}

/** @return ResultInterface|MockObject */
protected function prepareResult(array $data): ResultInterface
{
Expand Down Expand Up @@ -63,4 +73,25 @@ protected function expectClientToExecuteSelectQueryOnce(
->with($query, $endpoint)
->willReturn($result);
}

protected function assertQueryStringContainsPart(string $expected, string $queryString): void
{
$message = function () use ($expected, $queryString) {
[$_, $queryString] = explode('?', $queryString);
$parts = explode('&', $queryString);

return sprintf(
"Expected \"%s\" was not found in query string: \"%s\"\nwith parts:\n - %s",
urldecode($expected),
urldecode($queryString),
implode("\n - ", array_map(urldecode(...), $parts))
);
};

$this->assertStringContainsString(
$expected,
$queryString,
$message()
);
}
}
5 changes: 1 addition & 4 deletions tests/Fixture/DummySolrQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

class DummySolrQuery extends AbstractSolrSelectQuery
{
private array $fields;

public function __construct(array $fields)
public function __construct(private array $fields)
{
$this->fields = $fields;
}

public function prepareSelect(Query $select): Query
Expand Down
Loading

0 comments on commit 94cb01f

Please sign in to comment.