Skip to content

Commit

Permalink
Let PHPStan detect deprecated usages (#11639)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Oct 9, 2024
1 parent 58287bb commit 5266029
Show file tree
Hide file tree
Showing 48 changed files with 169 additions and 17 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
Expand All @@ -42,7 +43,9 @@
"doctrine/annotations": "^1.13 || ^2",
"doctrine/coding-standard": "^9.0.2 || ^12.0",
"phpbench/phpbench": "^0.16.10 || ^1.0",
"phpstan/extension-installer": "~1.1.0 || ^1.4",
"phpstan/phpstan": "~1.4.10 || 1.12.6",
"phpstan/phpstan-deprecation-rules": "^1",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
"psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.7.2",
Expand Down
5 changes: 5 additions & 0 deletions phpstan-dbal2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ parameters:
count: 2
path: src/Mapping/ClassMetadataFactory.php

- '~^Call to deprecated method getSQLResultCasing\(\) of class Doctrine\\DBAL\\Platforms\\AbstractPlatform\.$~'
-
message: '~deprecated class Doctrine\\DBAL\\Tools\\Console\\Command\\ImportCommand\:~'
path: src/Tools/Console/ConsoleRunner.php

# Symfony cache supports passing a key prefix to the clear method.
- '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/'

Expand Down
12 changes: 12 additions & 0 deletions phpstan-persistence2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ includes:
- phpstan-params.neon

parameters:
reportUnmatchedIgnoredErrors: false

ignoreErrors:
# deprecations from doctrine/dbal:3.x
- '/^Call to an undefined method Doctrine\\DBAL\\Platforms\\AbstractPlatform::getGuidExpression\(\).$/'
Expand Down Expand Up @@ -70,3 +72,13 @@ parameters:
paths:
- src/Mapping/Driver/XmlDriver.php
- src/Mapping/Driver/YamlDriver.php

# Extending a deprecated class conditionally to maintain BC
-
message: '~deprecated class Doctrine\\Persistence\\Mapping\\Driver\\AnnotationDriver\:~'
path: src/Mapping/Driver/CompatibilityAnnotationDriver.php

# We're sniffing for this deprecated class in order to detect Persistence 2
-
message: '~deprecated class Doctrine\\Common\\Persistence\\PersistentObject\:~'
path: src/EntityManager.php
7 changes: 7 additions & 0 deletions src/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,11 @@ public function setHydrationCacheProfile(?QueryCacheProfile $profile = null)

// DBAL 2
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
// @phpstan-ignore method.deprecated
if (! $profile->getResultCacheDriver()) {
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
if ($defaultHydrationCacheImpl) {
// @phpstan-ignore method.deprecated
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultHydrationCacheImpl));
}
}
Expand Down Expand Up @@ -609,9 +611,11 @@ public function setResultCacheProfile(?QueryCacheProfile $profile = null)

// DBAL 2
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
// @phpstan-ignore method.deprecated
if (! $profile->getResultCacheDriver()) {
$defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCache();
if ($defaultResultCacheDriver) {
// @phpstan-ignore method.deprecated
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultResultCacheDriver));
}
}
Expand Down Expand Up @@ -677,6 +681,7 @@ public function setResultCache(?CacheItemPoolInterface $resultCache = null)
$resultCacheDriver = DoctrineProvider::wrap($resultCache);

$this->_queryCacheProfile = $this->_queryCacheProfile
// @phpstan-ignore method.deprecated
? $this->_queryCacheProfile->setResultCacheDriver($resultCacheDriver)
: new QueryCacheProfile(0, null, $resultCacheDriver);

Expand Down Expand Up @@ -780,6 +785,7 @@ public function setResultCacheLifetime($lifetime)

// Compatibility for DBAL 2
if (! method_exists($this->_queryCacheProfile, 'setResultCache')) {
// @phpstan-ignore method.deprecated
$this->_queryCacheProfile = $this->_queryCacheProfile->setResultCacheDriver(DoctrineProvider::wrap($cache));

return $this;
Expand Down Expand Up @@ -1235,6 +1241,7 @@ private function getHydrationCache(): CacheItemPoolInterface

// Support for DBAL 2
if (! method_exists($this->_hydrationCacheProfile, 'getResultCache')) {
// @phpstan-ignore method.deprecated
$cacheDriver = $this->_hydrationCacheProfile->getResultCacheDriver();
assert($cacheDriver !== null);

Expand Down
2 changes: 2 additions & 0 deletions src/Cache/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* Defines a contract for accessing a particular named region.
*
* @phpstan-ignore interface.extendsDeprecatedInterface
*/
interface Region extends MultiGetRegion
{
Expand Down
2 changes: 2 additions & 0 deletions src/Cache/Region/DefaultRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function __construct(string $name, $cacheItemPool, int $lifetime = 0)
CacheItemPoolInterface::class
);

// @phpstan-ignore property.deprecated
$this->cache = $cacheItemPool;
$this->cacheItemPool = CacheAdapter::wrap($cacheItemPool);
} elseif (! $cacheItemPool instanceof CacheItemPoolInterface) {
Expand All @@ -82,6 +83,7 @@ public function __construct(string $name, $cacheItemPool, int $lifetime = 0)
get_debug_type($cacheItemPool)
));
} else {
// @phpstan-ignore property.deprecated
$this->cache = DoctrineProvider::wrap($cacheItemPool);
$this->cacheItemPool = $cacheItemPool;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public function getEntityNamespace($entityNamespaceAlias)
);

if (! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
// @phpstan-ignore staticMethod.deprecatedClass
throw UnknownEntityNamespace::fromNamespaceAlias($entityNamespaceAlias);
}

Expand Down Expand Up @@ -314,6 +315,7 @@ public function getResultCache(): ?CacheItemPoolInterface
{
// Compatibility with DBAL 2
if (! method_exists(parent::class, 'getResultCache')) {
// @phpstan-ignore method.deprecated
$cacheImpl = $this->getResultCacheImpl();

return $cacheImpl ? CacheAdapter::wrap($cacheImpl) : null;
Expand All @@ -329,6 +331,7 @@ public function setResultCache(CacheItemPoolInterface $cache): void
{
// Compatibility with DBAL 2
if (! method_exists(parent::class, 'setResultCache')) {
// @phpstan-ignore method.deprecated
$this->setResultCacheImpl(DoctrineProvider::wrap($cache));

return;
Expand Down
1 change: 1 addition & 0 deletions src/Decorator/EntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function wrapInTransaction(callable $func)
E_USER_NOTICE
);

// @phpstan-ignore method.deprecated
return $this->wrapped->transactional($func);
}

Expand Down
7 changes: 5 additions & 2 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ public function __construct(Connection $conn, Configuration $config, ?EventManag
throw MissingMappingDriverImplementation::create();
}

$this->conn = $conn;
$this->config = $config;
$this->conn = $conn;
$this->config = $config;
// @phpstan-ignore method.deprecated
$this->eventManager = $eventManager ?? $conn->getEventManager();

$metadataFactoryClassName = $config->getClassMetadataFactoryName();
Expand Down Expand Up @@ -615,6 +616,7 @@ public function getPartialReference($entityName, $identifier)
public function clear($entityName = null)
{
if ($entityName !== null && ! is_string($entityName)) {
// @phpstan-ignore staticMethod.deprecated
throw ORMInvalidArgumentException::invalidEntityName($entityName);
}

Expand Down Expand Up @@ -1109,6 +1111,7 @@ private function configureMetadataCache(): void

private function configureLegacyMetadataCache(): void
{
// @phpstan-ignore method.deprecated
$metadataCache = $this->config->getMetadataCacheImpl();
if (! $metadataCache) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/Event/PostLoadEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PostLoadEventArgs extends LifecycleEventArgs
{
}
1 change: 1 addition & 0 deletions src/Event/PostPersistEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PostPersistEventArgs extends LifecycleEventArgs
{
}
1 change: 1 addition & 0 deletions src/Event/PostRemoveEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PostRemoveEventArgs extends LifecycleEventArgs
{
}
1 change: 1 addition & 0 deletions src/Event/PostUpdateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PostUpdateEventArgs extends LifecycleEventArgs
{
}
1 change: 1 addition & 0 deletions src/Event/PrePersistEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PrePersistEventArgs extends LifecycleEventArgs
{
}
1 change: 1 addition & 0 deletions src/Event/PreRemoveEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Event;

/** @phpstan-ignore class.extendsDeprecatedClass */
final class PreRemoveEventArgs extends LifecycleEventArgs
{
}
3 changes: 3 additions & 0 deletions src/Event/PreUpdateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Class that holds event arguments for a preUpdate event.
*
* @phpstan-ignore class.extendsDeprecatedClass
*/
class PreUpdateEventArgs extends LifecycleEventArgs
{
Expand All @@ -26,6 +28,7 @@ class PreUpdateEventArgs extends LifecycleEventArgs
*/
public function __construct($entity, EntityManagerInterface $em, array &$changeSet)
{
// @phpstan-ignore staticMethod.deprecatedClass
parent::__construct($entity, $em);

$this->entityChangeSet = &$changeSet;
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/ORMException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* Should become an interface in 3.0
*
* @phpstan-ignore class.extendsDeprecatedClass
*/
class ORMException extends BaseORMException
{
Expand Down
1 change: 1 addition & 0 deletions src/Id/AbstractIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function generateId(EntityManagerInterface $em, $entity)
throw new InvalidArgumentException('Unsupported entity manager implementation.');
}

// @phpstan-ignore method.deprecated
return $this->generate($em, $entity);
}

Expand Down
1 change: 1 addition & 0 deletions src/Internal/CriteriaOrderings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ trait CriteriaOrderings
private static function getCriteriaOrderings(Criteria $criteria): array
{
if (! method_exists(Criteria::class, 'orderings')) {
// @phpstan-ignore method.deprecated
return $criteria->getOrderings();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mapping/Builder/ClassMetadataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public function addUniqueConstraint(array $columns, $name)
/**
* Adds named query.
*
* @deprecated
*
* @param string $name
* @param string $dqlQuery
*
Expand Down
11 changes: 8 additions & 3 deletions src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata
{
foreach ($parentClass->namedQueries as $name => $query) {
if (! isset($subClass->namedQueries[$name])) {
// @phpstan-ignore method.deprecated
$subClass->addNamedQuery(
[
'name' => $query['name'],
Expand All @@ -575,6 +576,7 @@ private function addInheritedNamedNativeQueries(ClassMetadata $subClass, ClassMe
{
foreach ($parentClass->namedNativeQueries as $name => $query) {
if (! isset($subClass->namedNativeQueries[$name])) {
// @phpstan-ignore method.deprecated
$subClass->addNamedNativeQuery(
[
'name' => $query['name'],
Expand Down Expand Up @@ -637,7 +639,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
$platform = $this->getTargetPlatform();

// Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
/** @psalm-suppress UndefinedClass, InvalidClass */
/** @psalm-suppress UndefinedClass, InvalidClass */ // @phpstan-ignore method.deprecated
if (! $platform instanceof MySQLPlatform && ! $platform instanceof SqlitePlatform && ! $platform instanceof SQLServerPlatform && $platform->usesSequenceEmulatedIdentityColumns()) {
Deprecation::trigger(
'doctrine/orm',
Expand All @@ -654,8 +656,9 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
$columnName = $class->getSingleIdentifierColumnName();
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequencePrefix = $class->getSequencePrefix($this->getTargetPlatform());
$sequenceName = $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix, $columnName);
$definition = [
// @phpstan-ignore method.deprecated
$sequenceName = $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix, $columnName);
$definition = [
'sequenceName' => $this->truncateSequenceName($sequenceName),
];

Expand Down Expand Up @@ -711,13 +714,15 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
$class->setIdGenerator(new AssignedGenerator());
break;

// @phpstan-ignore classConstant.deprecated
case ClassMetadata::GENERATOR_TYPE_UUID:
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/7312',
'Mapping for %s: the "UUID" id generator strategy is deprecated with no replacement',
$class->name
);
// @phpstan-ignore new.deprecated
$class->setIdGenerator(new UuidGenerator());
break;

Expand Down
Loading

0 comments on commit 5266029

Please sign in to comment.