Skip to content

Commit 8ef9253

Browse files
authored
Upgrade to PHPStan 2 (doctrine#11756)
Some calls to assert() are no longer necessary.
1 parent 0ed0be0 commit 8ef9253

12 files changed

+2252
-881
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"doctrine/coding-standard": "^9.0.2 || ^12.0",
4545
"phpbench/phpbench": "^0.16.10 || ^1.0",
4646
"phpstan/extension-installer": "~1.1.0 || ^1.4",
47-
"phpstan/phpstan": "~1.4.10 || 1.12.6",
48-
"phpstan/phpstan-deprecation-rules": "^1",
47+
"phpstan/phpstan": "~1.4.10 || 2.0.3",
48+
"phpstan/phpstan-deprecation-rules": "^1 || ^2",
4949
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
5050
"psr/log": "^1 || ^2 || ^3",
5151
"squizlabs/php_codesniffer": "3.7.2",

phpstan-baseline.neon

+2,232-845
Large diffs are not rendered by default.

phpstan-dbal2.neon

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77

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

4849
# Won't get fixed in DBAL 2
50+
-
51+
message: '#.*deleteItem.*expects string.*#'
52+
count: 1
53+
path: src/Query.php
54+
55+
-
56+
message: '#.*get(Drop|Create)SchemaS(ql|QL).*should return list.*but returns array.*#'
57+
count: 2
58+
path: src/Tools/SchemaTool.php
59+
4960
-
5061
message: "#^Parameter \\#2 \\$start of method Doctrine\\\\DBAL\\\\Platforms\\\\AbstractPlatform\\:\\:getSubstringExpression\\(\\) expects int, string given\\.$#"
5162
count: 1

phpstan-params.neon

-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@ parameters:
99
Doctrine\ORM\Query\Parser:
1010
- syntaxError
1111
phpVersion: 80400
12-
13-
ignoreErrors:
14-
# Remove on 3.0.x
15-
- '~^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>\.$~'

phpstan-persistence2.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ parameters:
9898
path: src/Mapping/ClassMetadataFactory.php
9999

100100
-
101-
message: '#^Parameter \#1 \$classNames of method Doctrine\\ORM\\Mapping\\ClassMetadataInfo\<object\>\:\:setParentClasses\(\) expects array\<int, class\-string\>, array\<string\> given\.$#'
101+
message: '#^Parameter \#1 \$classNames of method Doctrine\\ORM\\Mapping\\ClassMetadataInfo\<object\>\:\:setParentClasses\(\) expects list\<class\-string\>, array\<string\> given\.$#'
102102
path: src/Mapping/ClassMetadataFactory.php

src/Cache/DefaultQueryCache.php

-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h
262262
$region = $persister->getCacheRegion();
263263

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

267266
foreach ($result as $index => $entity) {
268267
$identifier = $this->uow->getEntityIdentifier($entity);

src/Cache/Persister/Entity/AbstractEntityPersister.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Doctrine\ORM\UnitOfWork;
2626

2727
use function array_merge;
28-
use function assert;
2928
use function serialize;
3029
use function sha1;
3130

@@ -614,9 +613,10 @@ public function refresh(array $id, $entity, $lockMode = null)
614613
*/
615614
protected function buildCollectionCacheKey(array $association, $ownerId)
616615
{
617-
$metadata = $this->metadataFactory->getMetadataFor($association['sourceEntity']);
618-
assert($metadata instanceof ClassMetadata);
619-
620-
return new CollectionCacheKey($metadata->rootEntityName, $association['fieldName'], $ownerId);
616+
return new CollectionCacheKey(
617+
$this->metadataFactory->getMetadataFor($association['sourceEntity'])->rootEntityName,
618+
$association['fieldName'],
619+
$ownerId
620+
);
621621
}
622622
}

src/Mapping/ClassMetadataFactory.php

-2
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@ private function inheritIdGeneratorMapping(ClassMetadataInfo $class, ClassMetada
830830
*/
831831
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService)
832832
{
833-
assert($class instanceof ClassMetadata);
834833
$class->wakeupReflection($reflService);
835834
}
836835

@@ -839,7 +838,6 @@ protected function wakeupReflection(ClassMetadataInterface $class, ReflectionSer
839838
*/
840839
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService)
841840
{
842-
assert($class instanceof ClassMetadata);
843841
$class->initializeReflection($reflService);
844842
}
845843

src/Mapping/DefaultTypedFieldMapper.php

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public function validateAndComplete(array $mapping, ReflectionProperty $field):
6868
assert(is_a($type->getName(), BackedEnum::class, true));
6969
$mapping['enumType'] = $type->getName();
7070
$type = $reflection->getBackingType();
71-
72-
assert($type instanceof ReflectionNamedType);
7371
}
7472

7573
if (isset($this->typedFieldMappings[$type->getName()])) {

src/Mapping/Driver/AttributeDriver.php

-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
use LogicException;
1717
use ReflectionClass;
1818
use ReflectionMethod;
19-
use ReflectionProperty;
2019

21-
use function assert;
2220
use function class_exists;
2321
use function constant;
2422
use function defined;
@@ -310,8 +308,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
310308
}
311309

312310
foreach ($reflectionClass->getProperties() as $property) {
313-
assert($property instanceof ReflectionProperty);
314-
315311
if ($this->isRepeatedPropertyDeclaration($property, $metadata)) {
316312
continue;
317313
}
@@ -322,8 +318,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
322318
// Evaluate #[Cache] attribute
323319
$cacheAttribute = $this->reader->getPropertyAttribute($property, Mapping\Cache::class);
324320
if ($cacheAttribute !== null) {
325-
assert($cacheAttribute instanceof Mapping\Cache);
326-
327321
$mapping['cache'] = $metadata->getAssociationCacheDefaults(
328322
$mapping['fieldName'],
329323
[
@@ -569,7 +563,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
569563
$listenerClass = new ReflectionClass($listenerClassName);
570564

571565
foreach ($listenerClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
572-
assert($method instanceof ReflectionMethod);
573566
// find method callbacks.
574567
$callbacks = $this->getMethodCallbacks($method);
575568
$hasMapping = $hasMapping ?: ! empty($callbacks);
@@ -589,7 +582,6 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
589582
// Evaluate #[HasLifecycleCallbacks] attribute
590583
if (isset($classAttributes[Mapping\HasLifecycleCallbacks::class])) {
591584
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
592-
assert($method instanceof ReflectionMethod);
593585
foreach ($this->getMethodCallbacks($method) as $value) {
594586
$metadata->addLifecycleCallback($value[0], $value[1]);
595587
}

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
2020

21-
use function assert;
2221
use function get_debug_type;
2322
use function sprintf;
2423

@@ -96,13 +95,7 @@ private function doExecute(InputInterface $input, OutputInterface $output): int
9695

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

99-
if ($cache) {
100-
$result = $cache->clear();
101-
} else {
102-
assert($cacheDriver !== null);
103-
$result = $cacheDriver->deleteAll();
104-
}
105-
98+
$result = $cache ? $cache->clear() : $cacheDriver->deleteAll();
10699
$message = $result ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
107100

108101
if ($input->getOption('flush') === true && ! $cache) {

src/Tools/SchemaTool.php

-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@
3131
use function array_filter;
3232
use function array_flip;
3333
use function array_intersect_key;
34-
use function assert;
3534
use function count;
3635
use function current;
3736
use function func_num_args;
3837
use function implode;
3938
use function in_array;
40-
use function is_array;
4139
use function is_numeric;
4240
use function method_exists;
4341
use function strtolower;
@@ -327,7 +325,6 @@ static function (ClassMetadata $class) use ($idMapping): bool {
327325
$pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
328326
} elseif (isset($class->associationMappings[$identifierField])) {
329327
$assoc = $class->associationMappings[$identifierField];
330-
assert(is_array($assoc));
331328

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

0 commit comments

Comments
 (0)