Skip to content

Commit 1e70535

Browse files
authored
Merge pull request #39 from CakeDC/feature/phpstan-2
Upgrade to PHPStan 2.0
2 parents e7bb4a4 + 2d6b693 commit 1e70535

42 files changed

Lines changed: 284 additions & 387 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ jobs:
7676

7777
- name: Run phpstan integration test app
7878
if: always()
79-
run: vendor/bin/phpstan analyse --debug --error-format=github tests/test_app/
79+
run: vendor/bin/phpstan analyse --debug -c phpstan-test-app.neon --error-format=github tests/test_app/
8080

8181
- name: Run phpstan integration test plugin
8282
if: always()
83-
run: vendor/bin/phpstan analyse --debug --error-format=github tests/test_plugin/
83+
run: vendor/bin/phpstan analyse --debug -c phpstan-test-plugin.neon --error-format=github tests/test_plugin/

composer.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
],
1313
"require": {
1414
"php": ">=8.1.0",
15-
"phpstan/phpstan": "^1.12",
15+
"phpstan/phpstan": "^2.0",
1616
"cakephp/cakephp": "^5.0"
1717
},
1818
"require-dev": {
19-
"phpstan/phpstan-phpunit": "^1.0",
19+
"phpstan/phpstan-phpunit": "^2.0",
2020
"phpunit/phpunit": "^10.1",
2121
"cakephp/cakephp-codesniffer": "^5.0",
22-
"symplify/phpstan-rules": "^12.4"
22+
"phpstan/phpstan-deprecation-rules": "^2.0",
23+
"phpstan/phpstan-strict-rules": "^2.0"
2324
},
2425
"extra": {
2526
"phpstan": {
@@ -45,10 +46,10 @@
4546
"cs-fix": "phpcbf -p src/ tests",
4647
"test": "phpunit --stderr",
4748
"stan-integration": [
48-
"phpstan analyse --debug tests/test_app/",
49-
"phpstan analyse --debug tests/test_plugin/"
49+
"phpstan analyse --debug -c phpstan-test-app.neon",
50+
"phpstan analyse --debug -c phpstan-test-plugin.neon"
5051
],
51-
"stan": "phpstan analyse --debug src/",
52+
"stan": "phpstan analyse --debug",
5253
"check": [
5354
"@cs-check",
5455
"@stan",
@@ -60,5 +61,7 @@
6061
"allow-plugins": {
6162
"dealerdirect/phpcodesniffer-composer-installer": true
6263
}
63-
}
64+
},
65+
"minimum-stability": "dev",
66+
"prefer-stable": true
6467
}

extension.neon

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ services:
1414
factory: CakeDC\PHPStan\Type\ControllerFetchTableDynamicReturnTypeExtension(Cake\Controller\Controller, fetchTable)
1515
tags:
1616
- phpstan.broker.dynamicMethodReturnTypeExtension
17-
-
18-
factory: CakeDC\PHPStan\Type\TableLocatorDynamicReturnTypeExtension(Cake\Command\Command, fetchTable)
19-
tags:
20-
- phpstan.broker.dynamicMethodReturnTypeExtension
21-
-
22-
factory: CakeDC\PHPStan\Type\TableLocatorDynamicReturnTypeExtension(Cake\Mailer\Mailer, fetchTable)
23-
tags:
24-
- phpstan.broker.dynamicMethodReturnTypeExtension
25-
-
26-
factory: CakeDC\PHPStan\Type\TableLocatorDynamicReturnTypeExtension(Cake\View\Cell, fetchTable)
27-
tags:
28-
- phpstan.broker.dynamicMethodReturnTypeExtension
29-
-
30-
factory: CakeDC\PHPStan\Type\TableLocatorDynamicReturnTypeExtension(Cake\Controller\Component, fetchTable)
31-
tags:
32-
- phpstan.broker.dynamicMethodReturnTypeExtension
3317
-
3418
factory: CakeDC\PHPStan\Type\TableLocatorDynamicReturnTypeExtension(Cake\ORM\Locator\LocatorInterface, get)
3519
tags:

phpstan-test-app.neon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
includes:
2+
- extension.neon
3+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
4+
parameters:
5+
level: max
6+
paths:
7+
- tests/test_app
8+
treatPhpDocTypesAsCertain: false
9+
cakeDC:
10+
disallowEntityArrayAccessRule: true
11+
ignoreErrors:
12+
-
13+
identifier: missingType.generics

phpstan-test-plugin.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
includes:
2+
- extension.neon
3+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
4+
parameters:
5+
level: max
6+
paths:
7+
- tests/test_plugin
8+
treatPhpDocTypesAsCertain: false
9+
cakeDC:
10+
disallowEntityArrayAccessRule: true

phpstan.neon

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
includes:
22
- extension.neon
3-
rules:
4-
- Symplify\PHPStanRules\Rules\Explicit\NoMixedPropertyFetcherRule
5-
- Symplify\PHPStanRules\Rules\Explicit\NoMixedMethodCallerRule
3+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
4+
- vendor/phpstan/phpstan-strict-rules/rules.neon
65
parameters:
76
level: max
8-
checkGenericClassInNonGenericObjectType: false
7+
paths:
8+
- src
9+
- tests/TestCase
10+
excludePaths:
11+
- */Fake/*
912
treatPhpDocTypesAsCertain: false
10-
cakeDC:
11-
disallowEntityArrayAccessRule: true
13+
ignoreErrors:
14+
-
15+
identifier: missingType.generics

src/Constraint/ArrayOfStringStartsWith.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function matches(mixed $other): bool
4646
$this->notExpected = $this->actual;
4747
assert(is_array($other));
4848
foreach ($other as $key => $error) {
49+
$error = is_string($error) ? $error : 'Wrong error: ' . json_encode($error);
4950
if (!isset($this->actual[$key])) {
5051
$this->result[$key] = ['expected' => $error, 'type' => 'missing', 'actual' => null];
5152
$result = false;
@@ -58,7 +59,7 @@ protected function matches(mixed $other): bool
5859
}
5960
}
6061

61-
return $result && empty($this->notExpected);
62+
return $result && $this->notExpected === [];
6263
}
6364

6465
/**

src/Method/AssociationTableMixinClassReflectionExtension.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,36 @@
99

1010
use Cake\ORM\Association;
1111
use Cake\ORM\Table;
12-
use PHPStan\Broker\Broker;
13-
use PHPStan\Reflection\BrokerAwareExtension;
1412
use PHPStan\Reflection\ClassReflection;
1513
use PHPStan\Reflection\MethodReflection;
1614
use PHPStan\Reflection\MethodsClassReflectionExtension;
1715
use PHPStan\Reflection\PropertiesClassReflectionExtension;
1816
use PHPStan\Reflection\PropertyReflection;
17+
use PHPStan\Reflection\ReflectionProvider;
1918

2019
class AssociationTableMixinClassReflectionExtension implements
2120
PropertiesClassReflectionExtension,
22-
MethodsClassReflectionExtension,
23-
BrokerAwareExtension
21+
MethodsClassReflectionExtension
2422
{
2523
/**
26-
* @var \PHPStan\Broker\Broker
24+
* @var \PHPStan\Reflection\ReflectionProvider
2725
*/
28-
private Broker $broker;
26+
protected ReflectionProvider $reflectionProvider;
2927

3028
/**
31-
* @param \PHPStan\Broker\Broker $broker Class reflection broker
32-
* @return void
29+
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
3330
*/
34-
public function setBroker(Broker $broker): void
31+
public function __construct(ReflectionProvider $reflectionProvider)
3532
{
36-
$this->broker = $broker;
33+
$this->reflectionProvider = $reflectionProvider;
3734
}
3835

3936
/**
4037
* @return \PHPStan\Reflection\ClassReflection
4138
*/
4239
protected function getTableReflection(): ClassReflection
4340
{
44-
return $this->broker->getClass(Table::class);
41+
return $this->reflectionProvider->getClass(Table::class);
4542
}
4643

4744
/**

src/Method/TableFindByPropertyMethodReflection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TableFindByPropertyMethodReflection implements MethodReflection
3232
private ClassReflection $declaringClass;
3333

3434
/**
35-
* @var array<\PHPStan\Reflection\FunctionVariant>
35+
* @var list<\PHPStan\Reflection\FunctionVariant>
3636
*/
3737
private array $variants;
3838

@@ -205,7 +205,7 @@ public function hasSideEffects(): TrinaryLogic
205205

206206
/**
207207
* @param string $method
208-
* @return array<string>
208+
* @return list<string>
209209
*/
210210
protected function getParams(string $method): array
211211
{

src/PhpDoc/TableAssociationTypeNodeResolverExtension.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
1616
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1717
use PHPStan\Type\Generic\GenericObjectType;
18-
use PHPStan\Type\ObjectType;
1918
use PHPStan\Type\Type;
2019

2120
/**
@@ -65,19 +64,22 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type
6564
'table' => null,
6665
];
6766
foreach ($types as $type) {
68-
if (!$type instanceof ObjectType) {
67+
if (!$type->isObject()->yes()) {
6968
continue;
7069
}
71-
$className = $type->getClassName();
72-
if ($config['association'] === null && in_array($className, $this->associationTypes)) {
70+
$className = $type->getObjectClassNames()[0] ?? null;
71+
if ($className === null) {
72+
continue;
73+
}
74+
if ($config['association'] === null && in_array($className, $this->associationTypes, true)) {
7375
$config['association'] = $type;
7476
} elseif ($config['table'] === null && str_ends_with($className, 'Table')) {
7577
$config['table'] = $type;
7678
}
7779
}
78-
if ($config['table'] && $config['association']) {
80+
if ($config['table'] !== null && $config['association'] !== null) {
7981
return new GenericObjectType(
80-
$config['association']->getClassName(),
82+
$config['association']->getObjectClassNames()[0],
8183
[$config['table']]
8284
);
8385
}

0 commit comments

Comments
 (0)