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

Upgrade to PHPStan 2 #11756

Merged
merged 1 commit into from
Dec 7, 2024
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"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",
"phpstan/phpstan": "~1.4.10 || 2.0.3",
"phpstan/phpstan-deprecation-rules": "^1 || ^2",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
"psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.7.2",
Expand Down
3,077 changes: 2,232 additions & 845 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions phpstan-dbal2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:

ignoreErrors:
# PHPStan doesn't understand our method_exists() safeguards.
- '/Call to function method_exists.*/'
- '/Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\./'
# Class name will change in DBAL 3.
- '/^Class Doctrine\\DBAL\\Platforms\\PostgreSQLPlatform not found\.$/'
Expand Down Expand Up @@ -46,6 +47,16 @@ parameters:
path: src/Query/AST/Functions/LocateFunction.php

# Won't get fixed in DBAL 2
-
message: '#.*deleteItem.*expects string.*#'
count: 1
path: src/Query.php

-
message: '#.*get(Drop|Create)SchemaS(ql|QL).*should return list.*but returns array.*#'
count: 2
path: src/Tools/SchemaTool.php

-
message: "#^Parameter \\#2 \\$start of method Doctrine\\\\DBAL\\\\Platforms\\\\AbstractPlatform\\:\\:getSubstringExpression\\(\\) expects int, string given\\.$#"
count: 1
Expand Down
4 changes: 0 additions & 4 deletions phpstan-params.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ parameters:
Doctrine\ORM\Query\Parser:
- syntaxError
phpVersion: 80400

ignoreErrors:
# Remove on 3.0.x
- '~^Default value of the parameter #2 \$value \(array\{\}\) of method Doctrine\\ORM\\Query\\AST\\InstanceOfExpression\:\:__construct\(\) is incompatible with type non\-empty\-array<int, Doctrine\\ORM\\Query\\AST\\InputParameter\|string>\.$~'
2 changes: 1 addition & 1 deletion phpstan-persistence2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ parameters:
path: src/Mapping/ClassMetadataFactory.php

-
message: '#^Parameter \#1 \$classNames of method Doctrine\\ORM\\Mapping\\ClassMetadataInfo\<object\>\:\:setParentClasses\(\) expects array\<int, class\-string\>, array\<string\> given\.$#'
message: '#^Parameter \#1 \$classNames of method Doctrine\\ORM\\Mapping\\ClassMetadataInfo\<object\>\:\:setParentClasses\(\) expects list\<class\-string\>, array\<string\> given\.$#'
path: src/Mapping/ClassMetadataFactory.php
1 change: 0 additions & 1 deletion src/Cache/DefaultQueryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h
$region = $persister->getCacheRegion();

$cm = $this->em->getClassMetadata($entityName);
assert($cm instanceof ClassMetadata);

foreach ($result as $index => $entity) {
$identifier = $this->uow->getEntityIdentifier($entity);
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/Persister/Entity/AbstractEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Doctrine\ORM\UnitOfWork;

use function array_merge;
use function assert;
use function serialize;
use function sha1;

Expand Down Expand Up @@ -614,9 +613,10 @@ public function refresh(array $id, $entity, $lockMode = null)
*/
protected function buildCollectionCacheKey(array $association, $ownerId)
{
$metadata = $this->metadataFactory->getMetadataFor($association['sourceEntity']);
assert($metadata instanceof ClassMetadata);

return new CollectionCacheKey($metadata->rootEntityName, $association['fieldName'], $ownerId);
return new CollectionCacheKey(
$this->metadataFactory->getMetadataFor($association['sourceEntity'])->rootEntityName,
$association['fieldName'],
$ownerId
);
}
}
2 changes: 0 additions & 2 deletions src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,6 @@ private function inheritIdGeneratorMapping(ClassMetadataInfo $class, ClassMetada
*/
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService)
{
assert($class instanceof ClassMetadata);
$class->wakeupReflection($reflService);
}

Expand All @@ -839,7 +838,6 @@ protected function wakeupReflection(ClassMetadataInterface $class, ReflectionSer
*/
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService)
{
assert($class instanceof ClassMetadata);
$class->initializeReflection($reflService);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Mapping/DefaultTypedFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public function validateAndComplete(array $mapping, ReflectionProperty $field):
assert(is_a($type->getName(), BackedEnum::class, true));
$mapping['enumType'] = $type->getName();
$type = $reflection->getBackingType();

assert($type instanceof ReflectionNamedType);
}

if (isset($this->typedFieldMappings[$type->getName()])) {
Expand Down
8 changes: 0 additions & 8 deletions src/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
use LogicException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;

use function assert;
use function class_exists;
use function constant;
use function defined;
Expand Down Expand Up @@ -310,8 +308,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
}

foreach ($reflectionClass->getProperties() as $property) {
assert($property instanceof ReflectionProperty);

if ($this->isRepeatedPropertyDeclaration($property, $metadata)) {
continue;
}
Expand All @@ -322,8 +318,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
// Evaluate #[Cache] attribute
$cacheAttribute = $this->reader->getPropertyAttribute($property, Mapping\Cache::class);
if ($cacheAttribute !== null) {
assert($cacheAttribute instanceof Mapping\Cache);

$mapping['cache'] = $metadata->getAssociationCacheDefaults(
$mapping['fieldName'],
[
Expand Down Expand Up @@ -569,7 +563,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
$listenerClass = new ReflectionClass($listenerClassName);

foreach ($listenerClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
assert($method instanceof ReflectionMethod);
// find method callbacks.
$callbacks = $this->getMethodCallbacks($method);
$hasMapping = $hasMapping ?: ! empty($callbacks);
Expand All @@ -589,7 +582,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
// Evaluate #[HasLifecycleCallbacks] attribute
if (isset($classAttributes[Mapping\HasLifecycleCallbacks::class])) {
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
assert($method instanceof ReflectionMethod);
foreach ($this->getMethodCallbacks($method) as $value) {
$metadata->addLifecycleCallback($value[0], $value[1]);
}
Expand Down
9 changes: 1 addition & 8 deletions src/Tools/Console/Command/ClearCache/QueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function assert;
use function get_debug_type;
use function sprintf;

Expand Down Expand Up @@ -96,13 +95,7 @@

$ui->comment('Clearing <info>all</info> Query cache entries');

if ($cache) {
$result = $cache->clear();
} else {
assert($cacheDriver !== null);
$result = $cacheDriver->deleteAll();
}

$result = $cache ? $cache->clear() : $cacheDriver->deleteAll();

Check warning on line 98 in src/Tools/Console/Command/ClearCache/QueryCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Tools/Console/Command/ClearCache/QueryCommand.php#L98

Added line #L98 was not covered by tests
$message = $result ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';

if ($input->getOption('flush') === true && ! $cache) {
Expand Down
3 changes: 0 additions & 3 deletions src/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
use function array_filter;
use function array_flip;
use function array_intersect_key;
use function assert;
use function count;
use function current;
use function func_num_args;
use function implode;
use function in_array;
use function is_array;
use function is_numeric;
use function method_exists;
use function strtolower;
Expand Down Expand Up @@ -327,7 +325,6 @@ static function (ClassMetadata $class) use ($idMapping): bool {
$pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
} elseif (isset($class->associationMappings[$identifierField])) {
$assoc = $class->associationMappings[$identifierField];
assert(is_array($assoc));

foreach ($assoc['joinColumns'] as $joinColumn) {
$pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
Expand Down