Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecations of index management in Search #2150

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed `$origin` and `$scale` parameter types of `Elastica\Query\FunctionScore::addDecayFunction()` to allow `float|int|string` [#2144](https://github.com/ruflin/Elastica/pull/2144)
* Changed `$key` parameter type of `Elastica\Multi\Search::addSearch()` to allow `int|string|null` [#2144](https://github.com/ruflin/Elastica/pull/2144)
* Changed `$options` parameter type of `Elastica\Index::create()` to only allow `array<string, mixed>` [#2147](https://github.com/ruflin/Elastica/pull/2147)
* Changed `Elastica\Search::addIndex()`, `Elasica\Search::addIndices()` and `Elastica\Search::hasIndex()` parameter type to only allow `Elastica\Index` [#2150](https://github.com/ruflin/Elastica/pull/2150)
* Changed `$options` argument to not accept `int` in the following methods [#2148](https://github.com/ruflin/Elastica/pull/2148)
* `Elastica\SearchableInterface::search()`
* `Elastica\SearchableInterface::createSearch()`
Expand All @@ -63,6 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `Elastica\Search::setOptionsAndQuery()`
* `Elastica\Index::search()`
* `Elastica\Index::createSearch()`

### Added
* Added support for PHP 8.2 [#2136](https://github.com/ruflin/Elastica/pull/2136)
### Changed
Expand Down
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,6 @@ parameters:
count: 1
path: tests/ResultSetTest.php

-
message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, int given\\.$#"
count: 1
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, stdClass given\\.$#"
count: 1
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, string given\\.$#"
count: 1
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:hasIndex\\(\\) expects Elastica\\\\Index, string given\\.$#"
count: 2
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndices\\(\\) expects array\\<Elastica\\\\Index\\>, array\\<int, stdClass\\> given\\.$#"
count: 1
Expand Down
56 changes: 4 additions & 52 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,10 @@ public function __construct(Client $client, ?BuilderInterface $builder = null)

/**
* Adds a index to the list.
*
* @param Index $index Index object or string
*
* @throws InvalidException
*/
public function addIndex($index): self
public function addIndex(Index $index): self
{
if ($index instanceof Index) {
$index = $index->getName();
} else {
\trigger_deprecation(
'ruflin/elastica',
'7.2.0',
'Passing a string as 1st argument to "%s()" is deprecated, pass an Index instance or use "addIndexByName" instead. It will throw a %s in 8.0.',
__METHOD__,
\TypeError::class
);
}

if (!\is_scalar($index)) {
throw new InvalidException('Invalid param type');
}

return $this->addIndexByName((string) $index);
return $this->addIndexByName($index->getName());
}

/**
Expand All @@ -126,19 +106,6 @@ public function addIndexByName(string $index): self
public function addIndices(array $indices = []): self
{
foreach ($indices as $index) {
if (\is_string($index)) {
\trigger_deprecation(
'ruflin/elastica',
'7.2.0',
'Passing a array of strings as 1st argument to "%s()" is deprecated, pass an array of Indexes or use "addIndicesByName" instead. It will throw a %s in 8.0.',
__METHOD__,
\TypeError::class
);
$this->addIndexByName($index);

continue;
}

if (!$index instanceof Index) {
throw new InvalidException('Invalid param type for addIndices(), expected Index[]');
}
Expand Down Expand Up @@ -265,24 +232,9 @@ public function hasIndices(): bool
return \count($this->_indices) > 0;
}

/**
* @param Index $index
*/
public function hasIndex($index): bool
public function hasIndex(Index $index): bool
{
if ($index instanceof Index) {
$index = $index->getName();
} else {
\trigger_deprecation(
'ruflin/elastica',
'7.2.0',
'Passing a string as 1st argument to "%s()" is deprecated, pass an Index instance or use "hasIndexByName" instead. It will throw a %s in 8.0.',
__METHOD__,
\TypeError::class
);
}

return $this->hasIndexByName($index);
return $this->hasIndexByName($index->getName());
}

public function hasIndexByName(string $index): bool
Expand Down
99 changes: 0 additions & 99 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
use Elastica\Search;
use Elastica\Suggest;
use Elastica\Test\Base as BaseTest;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
* @internal
*/
class SearchTest extends BaseTest
{
use ExpectDeprecationTrait;

/**
* @group unit
*/
Expand Down Expand Up @@ -78,33 +75,6 @@ public function testAddIndexByName(): void
$this->assertContains('index1', $indices);
}

/**
* @group functional
* @group legacy
*/
public function testAddIndexTriggersDeprecationWithString(): void
{
$client = $this->_getClient();
$search = new Search($client);

$index1 = $this->_createIndex();

$search->addIndex($index1);
$indices = $search->getIndices();

$this->assertCount(1, $indices);

$this->assertContains($index1->getName(), $indices);

$this->expectDeprecation('Since ruflin/elastica 7.2.0: Passing a string as 1st argument to "Elastica\Search::addIndex()" is deprecated, pass an Index instance or use "addIndexByName" instead. It will throw a TypeError in 8.0.');

$search->addIndex('test2');
$indices = $search->getIndices();

$this->assertCount(2, $indices);
$this->assertContains('test2', $indices);
}

/**
* @group functional
*/
Expand Down Expand Up @@ -147,25 +117,6 @@ public function testHasIndexByName(): void
$this->assertTrue($search->hasIndexByName($indexName2));
}

/**
* @group functional
* @group legacy
*/
public function testHasIndexTriggersDeprecationWithString(): void
{
$client = $this->_getClient();
$search = new Search($client);

$indexName = 'index1';

$this->expectDeprecation('Since ruflin/elastica 7.2.0: Passing a string as 1st argument to "Elastica\Search::hasIndex()" is deprecated, pass an Index instance or use "hasIndexByName" instead. It will throw a TypeError in 8.0.');

$this->assertFalse($search->hasIndex($indexName));

$search->addIndexByName($indexName);
$this->assertTrue($search->hasIndex($indexName));
}

/**
* @group unit
*/
Expand Down Expand Up @@ -207,26 +158,6 @@ public function testAddIndicesByName(): void
$this->assertCount(2, $search->getIndices());
}

/**
* @group unit
* @group legacy
*/
public function testAddIndicesTriggersDeprecationWithIndexAsString(): void
{
$client = $this->_getClient();
$search = new Search($client);

$indices = [];
$indices[] = $client->getIndex('elastica_test1');
$indices[] = 'elastica_test2';

$this->expectDeprecation('Since ruflin/elastica 7.2.0: Passing a array of strings as 1st argument to "Elastica\Search::addIndices()" is deprecated, pass an array of Indexes or use "addIndicesByName" instead. It will throw a TypeError in 8.0.');

$search->addIndices($indices);

$this->assertCount(2, $search->getIndices());
}

/**
* @group unit
*/
Expand All @@ -239,36 +170,6 @@ public function testAddIndicesByNameWithInvalidParametersThrowsException(): void
$search->addIndicesByName([new \stdClass()]);
}

/**
* @group unit
* @group legacy
*/
public function testAddIndexInvalid(): void
{
$this->expectException(InvalidException::class);

$client = $this->_getClient();
$search = new Search($client);

$search->addIndex(new \stdClass());
}

/**
* @group unit
* @group legacy
*/
public function testAddNumericIndex(): void
{
$client = $this->_getClient();
$search = new Search($client);

$this->expectDeprecation('Since ruflin/elastica 7.2.0: Passing a string as 1st argument to "Elastica\Search::addIndex()" is deprecated, pass an Index instance or use "addIndexByName" instead. It will throw a TypeError in 8.0.');

$search->addIndex(1);

$this->assertContains('1', $search->getIndices(), 'Make sure it has been added and converted to string');
}

/**
* @group functional
*/
Expand Down