Skip to content

Commit c82c8d2

Browse files
authored
Support PHPStan 1 (#29)
* Support PHPStan 1 * --ignore-platform-reqs everywhere * Code upgrade * Support laminas-form:3 * composer-normalize * Restore lowest/highest diff
1 parent d9243d9 commit c82c8d2

19 files changed

+136
-116
lines changed

.github/workflows/integrate.yaml

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
php-version:
18-
- "7.4"
18+
- "8.0"
1919

2020
steps:
2121
- name: "Checkout"
@@ -42,8 +42,8 @@ jobs:
4242
strategy:
4343
matrix:
4444
php-version:
45-
- "7.4"
4645
- "8.0"
46+
- "8.1"
4747
dependencies:
4848
- "lowest"
4949
- "highest"
@@ -71,23 +71,15 @@ jobs:
7171
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
7272

7373
- name: "Install lowest dependencies"
74-
if: ${{ matrix.dependencies == 'lowest' && matrix.php-version != '8.0' }}
75-
run: "composer update --no-interaction --no-progress --prefer-lowest"
76-
77-
- name: "Install highest dependencies"
78-
if: ${{ matrix.dependencies == 'highest' && matrix.php-version != '8.0' }}
79-
run: "composer update --no-interaction --no-progress"
80-
81-
- name: "Install lowest dependencies PHP 8.0"
82-
if: ${{ matrix.dependencies == 'lowest' && matrix.php-version == '8.0' }}
74+
if: ${{ matrix.dependencies == 'lowest' }}
8375
run: "composer update --no-interaction --no-progress --ignore-platform-reqs --prefer-lowest"
8476

85-
- name: "Install highest dependencies PHP 8.0"
86-
if: ${{ matrix.dependencies == 'highest' && matrix.php-version == '8.0' }}
77+
- name: "Install highest dependencies"
78+
if: ${{ matrix.dependencies == 'highest' }}
8779
run: "composer update --no-interaction --no-progress --ignore-platform-reqs"
8880

8981
- name: "Run tests"
90-
timeout-minutes: 3
82+
timeout-minutes: 5
9183
run: "vendor/bin/phpunit --no-coverage --no-logging"
9284

9385
code-coverage:
@@ -98,7 +90,7 @@ jobs:
9890
strategy:
9991
matrix:
10092
php-version:
101-
- "7.4"
93+
- "8.0"
10294

10395
steps:
10496
- name: "Checkout"
@@ -123,7 +115,7 @@ jobs:
123115
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
124116

125117
- name: "Install dependencies"
126-
run: "composer update --no-interaction --no-progress"
118+
run: "composer update --no-interaction --no-progress --ignore-platform-reqs"
127119

128120
- name: "Run tests"
129121
timeout-minutes: 3
@@ -144,7 +136,7 @@ jobs:
144136
strategy:
145137
matrix:
146138
php-version:
147-
- "7.4"
139+
- "8.0"
148140

149141
steps:
150142
- name: "Checkout"
@@ -168,7 +160,7 @@ jobs:
168160
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
169161

170162
- name: "Install dependencies"
171-
run: "composer update --no-interaction --no-progress"
163+
run: "composer update --no-interaction --no-progress --ignore-platform-reqs"
172164

173165
- name: "Check coding standards"
174166
run: "vendor/bin/php-cs-fixer fix --verbose --dry-run --diff"
@@ -181,7 +173,7 @@ jobs:
181173
strategy:
182174
matrix:
183175
php-version:
184-
- "7.4"
176+
- "8.0"
185177

186178
steps:
187179
- name: "Checkout"
@@ -206,7 +198,7 @@ jobs:
206198
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
207199

208200
- name: "Install dependencies"
209-
run: "composer update --no-interaction --no-progress"
201+
run: "composer update --no-interaction --no-progress --ignore-platform-reqs"
210202

211203
- name: "Run static analysis"
212204
run: "vendor/bin/phpstan analyse --no-progress --error-format=checkstyle | cs2pr"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ static-analysis: vendor
1515

1616
.PHONY: test
1717
test: vendor
18-
php -d zend.assertions=1 vendor/bin/phpunit
18+
php -d zend.assertions=1 vendor/bin/phpunit ${arg}

composer.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.4 || ^8.0",
22-
"phpstan/phpstan": "^0.12.99"
21+
"php": ">=8.0",
22+
"phpstan/phpstan": "^1.1.2"
2323
},
2424
"conflict": {
2525
"laminas/laminas-cache": "<2.13",
2626
"laminas/laminas-filter": "<2.11",
27-
"laminas/laminas-form": "<2.17",
27+
"laminas/laminas-form": "<3.0",
2828
"laminas/laminas-hydrator": "<4.3",
2929
"laminas/laminas-i18n": "<2.11",
3030
"laminas/laminas-inputfilter": "<2.12",
@@ -35,20 +35,21 @@
3535
"laminas/laminas-validator": "<2.15"
3636
},
3737
"require-dev": {
38-
"laminas/laminas-cache": "^2.13.0",
39-
"laminas/laminas-filter": "^2.11.1",
40-
"laminas/laminas-form": "^2.17.0",
38+
"laminas/laminas-cache": "^2.13.2",
39+
"laminas/laminas-filter": "^2.12.0",
40+
"laminas/laminas-form": "^3.0.1",
4141
"laminas/laminas-hydrator": "^4.3.1",
42-
"laminas/laminas-i18n": "^2.11.2",
42+
"laminas/laminas-i18n": "^2.11.3",
4343
"laminas/laminas-inputfilter": "^2.12.0",
4444
"laminas/laminas-log": "^2.13.1",
45-
"laminas/laminas-mail": "^2.15.0",
46-
"laminas/laminas-mvc": "^3.2.0",
47-
"laminas/laminas-paginator": "^2.10.0",
45+
"laminas/laminas-mail": "^2.15.1",
46+
"laminas/laminas-mvc": "^3.3.0",
47+
"laminas/laminas-paginator": "^2.11.0",
4848
"laminas/laminas-validator": "^2.15.0",
4949
"malukenho/mcbumpface": "^1.1.5",
50-
"phpstan/phpstan-phpunit": "^0.12.22",
51-
"phpunit/phpunit": "^9.5.9",
50+
"phpstan/phpstan-deprecation-rules": "^1.0.0",
51+
"phpstan/phpstan-phpunit": "^1.0.0",
52+
"phpunit/phpunit": "^9.5.10",
5253
"slam/php-cs-fixer-extensions": "^v3.1.0",
5354
"slam/php-debug-r": "^v1.7.0"
5455
},

phpstan-baseline.neon

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: "#^Parameter \\#2 \\$args of static method PHPStan\\\\Reflection\\\\ParametersAcceptorSelector\\:\\:selectFromArgs\\(\\) expects array\\<PhpParser\\\\Node\\\\Arg\\>, array\\<PhpParser\\\\Node\\\\Arg\\|PhpParser\\\\Node\\\\VariadicPlaceholder\\> given\\.$#"
4+
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'LaminasPhpStan\\\\\\\\TestAsset\\\\\\\\BarService' and Laminas\\\\Stdlib\\\\DispatchableInterface will always evaluate to false\\.$#"
55
count: 1
6-
path: src/Type/Laminas/PluginMethodDynamicReturnTypeExtension/AbstractPluginMethodDynamicReturnTypeExtension.php
6+
path: tests/Type/Laminas/ServiceManagerLoaderTest.php
7+
8+
-
9+
message:
10+
"""
11+
#^Fetching class constant class of deprecated class Laminas\\\\Cache\\\\PatternPluginManager\\:
12+
This will be removed in v3\\.0\\.0\\. Cache pattern will require dependency injection and thus, a generic
13+
plugin manager makes no sense anymore\\.$#
14+
"""
15+
count: 1
16+
path: tests/Type/Laminas/ServiceManagerLoaderTest.php
717

phpstan.neon

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
includes:
22
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
34
- phpstan-baseline.neon
45

56
parameters:
67
level: max
78
paths:
89
- src/
910
- tests/
10-
excludes_analyse:
11-
- tests/Rules/Laminas/ServiceManagerGetMethodCallRule/
12-
- tests/Rules/Laminas/PluginManagerGetMethodCallRule/
13-
- tests/TestAsset/
14-
- tests/LaminasIntegration/data/
11+
excludePaths:
12+
analyseAndScan:
13+
- tests/Rules/Laminas/ServiceManagerGetMethodCallRule/
14+
- tests/Rules/Laminas/PluginManagerGetMethodCallRule/
15+
- tests/TestAsset/
16+
- tests/LaminasIntegration/data/

src/Rules/Laminas/ServiceManagerGetMethodCallRule.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PhpParser\Node;
1313
use PhpParser\Node\Arg;
1414
use PHPStan\Analyser\Scope;
15-
use PHPStan\Broker\Broker;
15+
use PHPStan\Reflection\ReflectionProvider;
1616
use PHPStan\Rules\Rule;
1717
use PHPStan\Type\Constant\ConstantStringType;
1818
use PHPStan\Type\ObjectType;
@@ -24,14 +24,10 @@
2424
*/
2525
final class ServiceManagerGetMethodCallRule implements Rule
2626
{
27-
private Broker $broker;
28-
29-
private ServiceManagerLoader $serviceManagerLoader;
30-
31-
public function __construct(Broker $broker, ServiceManagerLoader $serviceManagerLoader)
32-
{
33-
$this->broker = $broker;
34-
$this->serviceManagerLoader = $serviceManagerLoader;
27+
public function __construct(
28+
private ReflectionProvider $reflectionProvider,
29+
private ServiceManagerLoader $serviceManagerLoader
30+
) {
3531
}
3632

3733
public function getNodeType(): string
@@ -46,11 +42,12 @@ public function getNodeType(): string
4642
*/
4743
public function processNode(Node $node, Scope $scope): array
4844
{
49-
if (1 !== \count($node->args)) {
45+
$args = $node->getArgs();
46+
if (1 !== \count($args)) {
5047
return [];
5148
}
5249

53-
$firstArg = $node->args[0];
50+
$firstArg = $args[0];
5451
if (! $firstArg instanceof Arg) {
5552
return [];
5653
}
@@ -88,7 +85,7 @@ public function processNode(Node $node, Scope $scope): array
8885
$refProperty->setAccessible(true);
8986
$autoAddInvokableClass = $refProperty->getValue($serviceManager);
9087
if ($autoAddInvokableClass) {
91-
if ($this->broker->hasClass($serviceName)) {
88+
if ($this->reflectionProvider->hasClass($serviceName)) {
9289
return [];
9390
}
9491
$classDoesNotExistNote = \sprintf(' nor the class "%s" exists', $serviceName);

src/ServiceManagerLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ public function getServiceLocator(string $serviceManagerName): ServiceLocatorInt
7272
$refProp = new ReflectionProperty(ServiceListenerFactory::class, 'defaultServiceConfig');
7373
$refProp->setAccessible(true);
7474
$config = $refProp->getValue(new ServiceListenerFactory());
75+
\assert(\is_array($config));
76+
\assert(\is_array($config['factories']));
7577
unset($config['factories']['config']);
7678
$refProp->setAccessible(false);
7779
$serviceManager->configure($config);
7880
}
7981
foreach ($this->knownModules as $module) {
8082
if (\class_exists($module)) {
81-
$serviceManager->configure((new $module())->getDependencyConfig());
83+
$module = new $module();
84+
\assert(\method_exists($module, 'getDependencyConfig'));
85+
$serviceManager->configure($module->getDependencyConfig());
8286
}
8387
}
8488

@@ -88,6 +92,7 @@ public function getServiceLocator(string $serviceManagerName): ServiceLocatorInt
8892
$serviceLocator = $this->serviceLocator;
8993
if (! isset($this->serviceManagerNames[$serviceManagerName])) {
9094
$serviceLocator = $serviceLocator->get($serviceManagerName);
95+
\assert($serviceLocator instanceof ServiceLocatorInterface);
9196
}
9297

9398
return $serviceLocator;

src/Type/Laminas/ControllerPluginClassReflectionExtension.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@
77
use Laminas\Mvc\Controller\AbstractController;
88
use Laminas\ServiceManager\ServiceLocatorInterface;
99
use LaminasPhpStan\ServiceManagerLoader;
10-
use PHPStan\Broker\Broker;
11-
use PHPStan\Reflection\BrokerAwareExtension;
1210
use PHPStan\Reflection\ClassReflection;
1311
use PHPStan\Reflection\MethodReflection;
1412
use PHPStan\Reflection\MethodsClassReflectionExtension;
13+
use PHPStan\Reflection\ReflectionProvider;
1514
use PHPStan\Type\ObjectType;
1615

17-
final class ControllerPluginClassReflectionExtension implements BrokerAwareExtension, MethodsClassReflectionExtension
16+
final class ControllerPluginClassReflectionExtension implements MethodsClassReflectionExtension
1817
{
19-
private ServiceManagerLoader $serviceManagerLoader;
20-
21-
private Broker $broker;
22-
23-
public function __construct(ServiceManagerLoader $serviceManagerLoader)
24-
{
25-
$this->serviceManagerLoader = $serviceManagerLoader;
26-
}
27-
28-
public function setBroker(Broker $broker): void
29-
{
30-
$this->broker = $broker;
18+
public function __construct(
19+
private ReflectionProvider $reflectionProvider,
20+
private ServiceManagerLoader $serviceManagerLoader
21+
) {
3122
}
3223

3324
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
@@ -37,11 +28,13 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
3728

3829
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
3930
{
40-
$plugin = $this->getControllerPluginManager()->get($methodName);
31+
$plugin = $this->getControllerPluginManager()->get($methodName);
32+
\assert(\is_object($plugin));
33+
4134
$pluginClassName = \get_class($plugin);
4235

4336
if (\is_callable($plugin)) {
44-
return $this->broker->getClass($pluginClassName)->getNativeMethod('__invoke');
37+
return $this->reflectionProvider->getClass($pluginClassName)->getNativeMethod('__invoke');
4538
}
4639

4740
$returnType = new ObjectType($pluginClassName);

src/Type/Laminas/PluginMethodDynamicReturnTypeExtension/AbstractPluginMethodDynamicReturnTypeExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final public function getTypeFromMethodCall(
3636
MethodCall $methodCall,
3737
Scope $scope
3838
): Type {
39-
$firstArg = $methodCall->args[0];
39+
$firstArg = $methodCall->getArgs()[0];
4040
if (! $firstArg instanceof Arg) {
4141
throw new \PHPStan\ShouldNotHappenException(\sprintf(
4242
'Argument passed to %s::%s should be a string, %s given',
@@ -52,11 +52,14 @@ final public function getTypeFromMethodCall(
5252
if (null !== $plugin) {
5353
$pluginManager = $this->serviceManagerLoader->getServiceLocator($this->getPluginManagerName());
5454

55-
return new ObjectType(\get_class($pluginManager->get($plugin)));
55+
$pluginInstance = $pluginManager->get($plugin);
56+
\assert(\is_object($pluginInstance));
57+
58+
return new ObjectType(\get_class($pluginInstance));
5659
}
5760

5861
if ($argType instanceof StringType) {
59-
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $methodReflection->getVariants())->getReturnType();
62+
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
6063
}
6164

6265
throw new \PHPStan\ShouldNotHappenException(\sprintf(

src/Type/Laminas/ServiceGetterDynamicReturnTypeExtension/AbstractServiceGetterDynamicReturnTypeExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ final public function getTypeFromMethodCall(
4343
MethodCall $methodCall,
4444
Scope $scope
4545
): Type {
46-
$serviceManager = $this->serviceManagerLoader->getServiceLocator(ServiceLocatorInterface::class);
47-
$serviceName = $this->methodToServiceMap[$methodReflection->getName()];
48-
$serviceClass = \get_class($serviceManager->get($serviceName));
46+
$serviceManager = $this->serviceManagerLoader->getServiceLocator(ServiceLocatorInterface::class);
47+
$serviceName = $this->methodToServiceMap[$methodReflection->getName()];
48+
$serviceInstance = $serviceManager->get($serviceName);
49+
\assert(\is_object($serviceInstance));
50+
$serviceClass = \get_class($serviceInstance);
4951

5052
return new ObjectType($serviceClass);
5153
}

0 commit comments

Comments
 (0)