Skip to content

Commit

Permalink
Rector and PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Nov 30, 2024
1 parent 3c0f42c commit 07d9630
Show file tree
Hide file tree
Showing 51 changed files with 519 additions and 502 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"dg/bypass-finals": "^1.5.1",
"jackalope/jackalope-doctrine-dbal": "^1.9 || ^2.0",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "1.12.11",
"phpstan/phpstan-phpunit": "1.4.0",
"phpstan/phpstan": "2.0.3",
"phpstan/phpstan-phpunit": "2.0.1",
"phpunit/phpunit": "10.5.38",
"rector/rector": "1.2.10",
"rector/rector": "2.0.0-rc1",
"ruflin/elastica": "^7.3 || ^8.0",
"solarium/solarium": "^6.2",
"symfony/cache": "^5.4 || ^6.3 || ^7.0",
Expand Down
6 changes: 3 additions & 3 deletions lib/Adapter/Doctrine/Collections/CollectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public function __construct(
) {}

/**
* @phpstan-return int<0, max>
* @return int<0, max>
*/
public function getNbResults(): int
{
return $this->collection->count();
}

/**
* @phpstan-param int<0, max> $offset
* @phpstan-param int<0, max> $length
* @param int<0, max> $offset
* @param int<0, max> $length
*
* @return iterable<TKey, T>
*/
Expand Down
10 changes: 5 additions & 5 deletions lib/Adapter/Doctrine/Collections/SelectableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public function __construct(
) {}

/**
* @phpstan-return int<0, max>
* @return int<0, max>
*/
public function getNbResults(): int
{
return $this->selectable->matching($this->createCriteria(0, null))->count();
}

/**
* @phpstan-param int<0, max> $offset
* @phpstan-param int<0, max> $length
* @param int<0, max> $offset
* @param int<0, max> $length
*
* @return iterable<array-key, T>
*/
Expand All @@ -44,8 +44,8 @@ public function getSlice(int $offset, int $length): iterable
}

/**
* @phpstan-param int<0, max> $firstResult
* @phpstan-param int<0, max>|null $maxResult
* @param int<0, max> $firstResult
* @param int<0, max>|null $maxResult
*/
private function createCriteria(int $firstResult, ?int $maxResult): Criteria
{
Expand Down
12 changes: 8 additions & 4 deletions lib/Adapter/Doctrine/Collections/Tests/CollectionAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
final class CollectionAdapterTest extends TestCase
{
/**
* @var ArrayCollection<array-key, int>
* @var ArrayCollection<int, int<1, 150>>
*/
private ArrayCollection $collection;

/**
* @var CollectionAdapter<array-key, int>
* @var CollectionAdapter<int, int<1, 150>>
*/
private CollectionAdapter $adapter;

Expand All @@ -27,11 +27,15 @@ protected function setUp(): void

public function testGetNbResultsShouldResultTheCollectionCount(): void
{
self::assertSame($this->collection->count(), $this->adapter->getNbResults());
$this->assertSame($this->collection->count(), $this->adapter->getNbResults());
}

public function testGetResultsShouldReturnTheCollectionSliceReturnValue(): void
{
self::assertSame(array_values(range(6, 17)), array_values($this->adapter->getSlice(5, 12)));
$slice = $this->adapter->getSlice(5, 12);

\assert(\is_array($slice));

$this->assertSame(array_values(range(6, 17)), array_values($slice));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public function testGetNbResults(): void
$collection->method('count')
->willReturn(10);

$this->selectable->expects(self::once())
$this->selectable->expects($this->once())
->method('matching')
->with($this->criteria)
->willReturn($collection);

self::assertSame(10, $this->adapter->getNbResults());
$this->assertSame(10, $this->adapter->getNbResults());
}

public function testGetSlice(): void
Expand All @@ -66,11 +66,11 @@ public function testGetSlice(): void

$slice = [];

$this->selectable->expects(self::once())
$this->selectable->expects($this->once())
->method('matching')
->with($this->criteria)
->willReturn($slice);

self::assertSame($slice, $this->adapter->getSlice(10, 20));
$this->assertSame($slice, $this->adapter->getSlice(10, 20));
}
}
12 changes: 5 additions & 7 deletions lib/Adapter/Doctrine/DBAL/QueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ class QueryAdapter implements AdapterInterface
private readonly QueryBuilder $queryBuilder;

/**
* @var callable
*
* @phpstan-var callable(QueryBuilder): (QueryBuilder|void)
* @var callable(QueryBuilder): (QueryBuilder|void)
*/
private $countQueryBuilderModifier;

/**
* @phpstan-param callable(QueryBuilder): (QueryBuilder|void) $countQueryBuilderModifier
* @param callable(QueryBuilder): (QueryBuilder|void) $countQueryBuilderModifier
*/
public function __construct(QueryBuilder $queryBuilder, callable $countQueryBuilderModifier)
{
Expand All @@ -33,7 +31,7 @@ public function __construct(QueryBuilder $queryBuilder, callable $countQueryBuil
}

/**
* @phpstan-return int<0, max>
* @return int<0, max>
*/
public function getNbResults(): int
{
Expand All @@ -43,8 +41,8 @@ public function getNbResults(): int
}

/**
* @phpstan-param int<0, max> $offset
* @phpstan-param int<0, max> $length
* @param int<0, max> $offset
* @param int<0, max> $length
*
* @return iterable<array-key, T>
*/
Expand Down
1 change: 1 addition & 0 deletions lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private function createCountQueryModifier(string $countField): \Closure
return static function (QueryBuilder $queryBuilder) use ($select): QueryBuilder {
$queryBuilder->select($select);

/** @phpstan-ignore-next-line function.alreadyNarrowedType */
if (method_exists($queryBuilder, 'resetOrderBy')) {
$queryBuilder->resetOrderBy();
} else {
Expand Down
10 changes: 5 additions & 5 deletions lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ static function (QueryBuilder $qb): void {
}
);

self::assertSame(50, $adapter->getNbResults());
$this->assertSame(50, $adapter->getNbResults());
}

public function testAdapterReturnsNumberOfResults(): void
{
$adapter = $this->createAdapterToTestGetNbResults();

self::assertSame(50, $adapter->getNbResults());
$this->assertSame(50, $adapter->getNbResults());
}

public function testResultCountStaysConsistentAfterSlicing(): void
Expand All @@ -45,7 +45,7 @@ public function testResultCountStaysConsistentAfterSlicing(): void

$adapter->getSlice(1, 10);

self::assertSame(50, $adapter->getNbResults());
$this->assertSame(50, $adapter->getNbResults());
}

public function testGetSlice(): void
Expand All @@ -58,7 +58,7 @@ public function testGetSlice(): void
$this->qb->setFirstResult($offset)
->setMaxResults($length);

self::assertSame($this->qb->executeQuery()->fetchAllAssociative(), $adapter->getSlice($offset, $length));
$this->assertSame($this->qb->executeQuery()->fetchAllAssociative(), $adapter->getSlice($offset, $length));
}

public function testTheAdapterUsesAClonedQuery(): void
Expand All @@ -68,7 +68,7 @@ public function testTheAdapterUsesAClonedQuery(): void
$this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id')
->groupBy('c.post_id');

self::assertSame(50, $adapter->getNbResults());
$this->assertSame(50, $adapter->getNbResults());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function testACountFieldWithoutAnAliasIsRejected(): void

public function testAdapterReturnsNumberOfResults(): void
{
self::assertSame(50, $this->adapter->getNbResults());
$this->assertSame(50, $this->adapter->getNbResults());
}

public function testResultCountStaysConsistentAfterSlicing(): void
{
$this->adapter->getSlice(1, 10);

self::assertSame(50, $this->adapter->getNbResults());
$this->assertSame(50, $this->adapter->getNbResults());
}

public function testGetSlice(): void
Expand All @@ -54,6 +54,6 @@ public function testGetSlice(): void
$q->setFirstResult($offset)
->setMaxResults($length);

self::assertSame($q->executeQuery()->fetchAllAssociative(), $this->adapter->getSlice($offset, $length));
$this->assertSame($q->executeQuery()->fetchAllAssociative(), $this->adapter->getSlice($offset, $length));
}
}
6 changes: 3 additions & 3 deletions lib/Adapter/Doctrine/MongoDBODM/AggregationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
) {}

/**
* @phpstan-return int<0, max>
* @return int<0, max>
*/
public function getNbResults(): int
{
Expand All @@ -34,8 +34,8 @@ public function getNbResults(): int
}

/**
* @phpstan-param int<0, max> $offset
* @phpstan-param int<0, max> $length
* @param int<0, max> $offset
* @param int<0, max> $length
*
* @return iterable<array-key, T>
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/Adapter/Doctrine/MongoDBODM/QueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
) {}

/**
* @phpstan-return int<0, max>
* @return int<0, max>
*/
public function getNbResults(): int
{
Expand All @@ -33,8 +33,8 @@ public function getNbResults(): int
}

/**
* @phpstan-param int<0, max> $offset
* @phpstan-param int<0, max> $length
* @param int<0, max> $offset
* @param int<0, max> $length
*
* @return iterable<array-key, T>
*/
Expand Down
22 changes: 11 additions & 11 deletions lib/Adapter/Doctrine/MongoDBODM/Tests/AggregationAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ public function testGetNbResultsShouldResetHydrationAndAddCountStage(): void
/** @var MockObject&Count $countStage */
$countStage = $this->createMock(Count::class);

$countStage->expects(self::once())
$countStage->expects($this->once())
->method('getAggregation')
->willReturn($aggregation);

$resultIterator->expects(self::once())
$resultIterator->expects($this->once())
->method('toArray')
->willReturn([['numResults' => 110]]);

$aggregation->expects(self::once())
$aggregation->expects($this->once())
->method('getIterator')
->willReturn($resultIterator);

$this->aggregationBuilder->expects(self::once())
$this->aggregationBuilder->expects($this->once())
->method('hydrate')
->with(null)
->willReturnSelf();

$this->aggregationBuilder->expects(self::once())
$this->aggregationBuilder->expects($this->once())
->method('count')
->with('numResults')
->willReturn($countStage);

self::assertSame(110, $this->adapter->getNbResults());
$this->assertSame(110, $this->adapter->getNbResults());
}

public function testGetSlice(): void
Expand All @@ -89,24 +89,24 @@ public function testGetSlice(): void
/** @var MockObject&Limit $limitStage */
$limitStage = $this->createMock(Limit::class);

$skipStage->expects(self::once())
$skipStage->expects($this->once())
->method('limit')
->with($length)
->willReturn($limitStage);

$limitStage->expects(self::once())
$limitStage->expects($this->once())
->method('getAggregation')
->willReturn($aggregation);

$aggregation->expects(self::once())
$aggregation->expects($this->once())
->method('getIterator')
->willReturn($slice);

$this->aggregationBuilder->expects(self::once())
$this->aggregationBuilder->expects($this->once())
->method('skip')
->with($offset)
->willReturn($skipStage);

self::assertSame($slice, $this->adapter->getSlice($offset, $length));
$this->assertSame($slice, $this->adapter->getSlice($offset, $length));
}
}
Loading

0 comments on commit 07d9630

Please sign in to comment.