Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.12.x' into 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Feb 22, 2022
2 parents 87b894e + a52d988 commit 1712e3c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- "default"
include:
- php-version: "8.1"
dbal-version: "3.4@dev"
dbal-version: "3@dev"

steps:
- name: "Checkout"
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\Mapping\EntityListenerResolver;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
Expand Down Expand Up @@ -467,6 +468,7 @@ public function getClassMetadataFactoryName()
*
* @param string $name The name of the filter.
* @param string $className The class name of the filter.
* @psalm-param class-string<SQLFilter> $className
*
* @return void
*/
Expand All @@ -482,7 +484,7 @@ public function addFilter($name, $className)
*
* @return string|null The class name of the filter, or null if it is not
* defined.
* @psalm-return ?class-string
* @psalm-return class-string<SQLFilter>|null
*/
public function getFilterClassName($name)
{
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ protected function getEntityManager(): EntityManagerInterface
return $this->em;
}

/**
* @psalm-return ClassMetadata<T>
*/
protected function getClassMetadata(): ClassMetadata
{
return $this->class;
Expand Down
30 changes: 22 additions & 8 deletions lib/Doctrine/ORM/Query/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ class FilterCollection
* Instances of enabled filters.
*
* @var SQLFilter[]
* @psalm-var array<string, SQLFilter>
*/
private $enabledFilters = [];

/** @var string The filter hash from the last time the query was parsed. */
private $filterHash;
/**
* The filter hash from the last time the query was parsed.
*
* @var string
*/
private $filterHash = '';

/** @var int The current state of this filter. */
/**
* The current state of this filter.
*
* @var int
* @psalm-var self::FILTERS_STATE_*
*/
private $filtersState = self::FILTERS_STATE_CLEAN;

public function __construct(EntityManagerInterface $em)
Expand All @@ -66,6 +76,7 @@ public function __construct(EntityManagerInterface $em)
* Gets all the enabled filters.
*
* @return SQLFilter[] The enabled filters.
* @psalm-return array<string, SQLFilter>
*/
public function getEnabledFilters()
{
Expand Down Expand Up @@ -97,8 +108,7 @@ public function enable($name)
// Keep the enabled filters sorted for the hash
ksort($this->enabledFilters);

// Now the filter collection is dirty
$this->filtersState = self::FILTERS_STATE_DIRTY;
$this->setFiltersStateDirty();
}

return $this->enabledFilters[$name];
Expand All @@ -120,8 +130,7 @@ public function disable($name)

unset($this->enabledFilters[$name]);

// Now the filter collection is dirty
$this->filtersState = self::FILTERS_STATE_DIRTY;
$this->setFiltersStateDirty();

return $filter;
}
Expand Down Expand Up @@ -169,7 +178,9 @@ public function isEnabled($name)
}

/**
* @return bool True, if the filter collection is clean.
* Checks if the filter collection is clean.
*
* @return bool
*/
public function isClean()
{
Expand All @@ -194,6 +205,9 @@ public function getHash()
$filterHash .= $name . $filter;
}

$this->filterHash = $filterHash;
$this->filtersState = self::FILTERS_STATE_CLEAN;

return $filterHash;
}

Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Query/Expr/Select.php

-
message: "#^Property Doctrine\\\\ORM\\\\Query\\\\FilterCollection\\:\\:\\$filterHash is never written, only read\\.$#"
count: 1
path: lib/Doctrine/ORM/Query/FilterCollection.php

-
message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#"
count: 3
Expand Down
14 changes: 0 additions & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1847,20 +1847,6 @@
<code>$this-&gt;parameters</code>
</PropertyTypeCoercion>
</file>
<file src="lib/Doctrine/ORM/Query/FilterCollection.php">
<LessSpecificReturnStatement occurrences="1">
<code>$this-&gt;enabledFilters[$name]</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType occurrences="1">
<code>SQLFilter</code>
</MoreSpecificReturnType>
<PropertyNotSetInConstructor occurrences="1">
<code>$filterHash</code>
</PropertyNotSetInConstructor>
<PropertyTypeCoercion occurrences="1">
<code>$this-&gt;enabledFilters</code>
</PropertyTypeCoercion>
</file>
<file src="lib/Doctrine/ORM/Query/Parser.php">
<ArgumentTypeCoercion occurrences="1">
<code>$stringPattern</code>
Expand Down
37 changes: 34 additions & 3 deletions tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace Doctrine\Tests\ORM\Query;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\OrmTestCase;
use InvalidArgumentException;

/**
* Test case for FilterCollection
*/
class FilterCollectionTest extends OrmTestCase
{
/** @var EntityManagerInterface */
/** @var EntityManagerMock */
private $em;

protected function setUp(): void
Expand Down Expand Up @@ -64,7 +65,7 @@ public function testIsEnabled(): void

public function testGetFilterInvalidArgument(): void
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$filterCollection = $this->em->getFilters();
$filterCollection->getFilter('testFilter');
}
Expand All @@ -76,6 +77,36 @@ public function testGetFilter(): void

self::assertInstanceOf(MyFilter::class, $filterCollection->getFilter('testFilter'));
}

public function testHashing(): void
{
$filterCollection = $this->em->getFilters();

self::assertTrue($filterCollection->isClean());

$oldHash = $filterCollection->getHash();
$filterCollection->setFiltersStateDirty();

self::assertFalse($filterCollection->isClean());
self::assertSame($oldHash, $filterCollection->getHash());
self::assertTrue($filterCollection->isClean());

$filterCollection->enable('testFilter');

self::assertFalse($filterCollection->isClean());

$hash = $filterCollection->getHash();

self::assertNotSame($oldHash, $hash);
self::assertTrue($filterCollection->isClean());
self::assertSame($hash, $filterCollection->getHash());

$filterCollection->disable('testFilter');

self::assertFalse($filterCollection->isClean());
self::assertSame($oldHash, $filterCollection->getHash());
self::assertTrue($filterCollection->isClean());
}
}

class MyFilter extends SQLFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testCachesDistinctRepositoriesPerDistinctEntityManager(): void
private function buildClassMetadata(string $className): ClassMetadata
{
$metadata = $this->createMock(ClassMetadata::class);
$metadata->expects(self::any())->method('getName')->will(self::returnValue($className));
$metadata->method('getName')->will(self::returnValue($className));
$metadata->name = $className;

$metadata->customRepositoryClassName = null;
Expand All @@ -131,10 +131,7 @@ private function buildClassMetadata(string $className): ClassMetadata
private function createEntityManager(): EntityManagerInterface
{
$entityManager = $this->createMock(EntityManagerInterface::class);

$entityManager->expects(self::any())
->method('getConfiguration')
->will(self::returnValue($this->configuration));
$entityManager->method('getConfiguration')->willReturn($this->configuration);

return $entityManager;
}
Expand Down

0 comments on commit 1712e3c

Please sign in to comment.