Skip to content

Commit c976e55

Browse files
authored
CI: use PHP versions matrix for more checks (#145)
1 parent f3dd073 commit c976e55

File tree

7 files changed

+69
-45
lines changed

7 files changed

+69
-45
lines changed

.github/workflows/checks.yml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
name: Setup PHP
1919
uses: shivammathur/setup-php@v2
2020
with:
21-
php-version: 8.3
21+
php-version: 8.4
2222
-
2323
name: Install dependencies
2424
run: composer install --no-progress --prefer-dist --no-interaction
@@ -27,6 +27,52 @@ jobs:
2727
name: Run checks
2828
run: composer check
2929

30+
cda:
31+
runs-on: ubuntu-latest
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
php-version: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
36+
steps:
37+
-
38+
name: Checkout code
39+
uses: actions/checkout@v4
40+
-
41+
name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php-version }}
45+
ini-file: development
46+
-
47+
name: Update dependencies
48+
run: composer update --no-progress --prefer-dist --no-interaction
49+
-
50+
name: Run dependency analyser
51+
run: composer check:dependencies
52+
53+
phpstan:
54+
runs-on: ubuntu-latest
55+
strategy:
56+
fail-fast: false
57+
matrix:
58+
php-version: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
59+
steps:
60+
-
61+
name: Checkout code
62+
uses: actions/checkout@v4
63+
-
64+
name: Setup PHP
65+
uses: shivammathur/setup-php@v2
66+
with:
67+
php-version: ${{ matrix.php-version }}
68+
ini-file: development
69+
-
70+
name: Update dependencies
71+
run: composer update --no-progress --prefer-dist --no-interaction
72+
-
73+
name: Run PHPStan
74+
run: composer check:types
75+
3076
tests:
3177
runs-on: ubuntu-latest
3278
strategy:
@@ -43,6 +89,7 @@ jobs:
4389
uses: shivammathur/setup-php@v2
4490
with:
4591
php-version: ${{ matrix.php-version }}
92+
ini-file: development
4693
-
4794
name: Update dependencies
4895
run: composer update --no-progress --${{ matrix.dependency-version }} --prefer-dist --no-interaction

phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ parameters:
4949
-
5050
message: "#but it's missing from the PHPDoc @throws tag\\.$#" # allow uncatched exceptions in tests
5151
path: tests/*
52+
53+
# allow referencing any attribute classes
54+
- '#^Parameter \#1 \$name of method PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass\:\:getAttributes\(\) expects class\-string\|null, string given\.$#'
55+
- '#^Parameter \#1 \$name of method PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionMethod\:\:getAttributes\(\) expects class\-string\|null, string given\.$#'

src/Provider/DoctrineUsageProvider.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Stmt\Return_;
88
use PHPStan\Analyser\Scope;
9+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
10+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
911
use PHPStan\Node\InClassNode;
1012
use PHPStan\Reflection\ExtendedMethodReflection;
1113
use PHPStan\Reflection\MethodReflection;
12-
use ReflectionClass;
13-
use ReflectionMethod;
1414
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
1515
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage;
16-
use const PHP_VERSION_ID;
1716

1817
class DoctrineUsageProvider implements MemberUsageProvider
1918
{
@@ -165,22 +164,11 @@ protected function isProbablyDoctrineListener(string $methodName): bool
165164

166165
protected function hasAttribute(ReflectionMethod $method, string $attributeClass): bool
167166
{
168-
if (PHP_VERSION_ID < 8_00_00) {
169-
return false;
170-
}
171-
172167
return $method->getAttributes($attributeClass) !== [];
173168
}
174169

175-
/**
176-
* @param ReflectionClass<object> $class
177-
*/
178170
protected function isPartOfAsEntityListener(ReflectionClass $class, string $methodName): bool
179171
{
180-
if (PHP_VERSION_ID < 8_00_00) {
181-
return false;
182-
}
183-
184172
foreach ($class->getAttributes('Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener') as $attribute) {
185173
$listenerMethodName = $attribute->getArguments()['method'] ?? $attribute->getArguments()[1] ?? null;
186174

@@ -192,9 +180,6 @@ protected function isPartOfAsEntityListener(ReflectionClass $class, string $meth
192180
return false;
193181
}
194182

195-
/**
196-
* @param ReflectionClass<object> $class
197-
*/
198183
protected function isEntityRepositoryConstructor(ReflectionClass $class, ReflectionMethod $method): bool
199184
{
200185
if (!$method->isConstructor()) {

src/Provider/NetteUsageProvider.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Nette\SmartObject;
1111
use PHPStan\Reflection\ClassReflection;
1212
use PHPStan\Reflection\ReflectionProvider;
13-
use ReflectionClass;
1413
use ReflectionMethod;
1514
use function lcfirst;
1615
use function preg_match_all;
@@ -104,7 +103,7 @@ private function isNetteMagic(ClassReflection $reflection, string $methodName):
104103

105104
if ($name !== null) {
106105
$name = lcfirst($name);
107-
$property = $this->getMagicProperties($reflection->getNativeReflection())[$name] ?? null;
106+
$property = $this->getMagicProperties($reflection)[$name] ?? null;
108107

109108
if ($property !== null) {
110109
return true;
@@ -116,12 +115,12 @@ private function isNetteMagic(ClassReflection $reflection, string $methodName):
116115
}
117116

118117
/**
119-
* @param ReflectionClass<object> $rc
120118
* @return array<string, true>
121119
* @see ObjectHelpers::getMagicProperties() Modified to use static reflection
122120
*/
123-
private function getMagicProperties(ReflectionClass $rc): array
121+
private function getMagicProperties(ClassReflection $reflection): array
124122
{
123+
$rc = $reflection->getNativeReflection();
125124
$class = $rc->getName();
126125

127126
if (isset($this->smartObjectCache[$class])) {
@@ -151,13 +150,11 @@ private function getMagicProperties(ReflectionClass $rc): array
151150
}
152151
}
153152

154-
foreach ($rc->getTraits() as $trait) {
153+
foreach ($reflection->getTraits() as $trait) {
155154
$props += $this->getMagicProperties($trait);
156155
}
157156

158-
$parent = $rc->getParentClass();
159-
160-
if ($parent !== false) {
157+
foreach ($reflection->getParents() as $parent) {
161158
$props += $this->getMagicProperties($parent);
162159
}
163160

src/Provider/PhpUnitUsageProvider.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
use Composer\InstalledVersions;
66
use PhpParser\Node;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
89
use PHPStan\Node\InClassNode;
910
use PHPStan\PhpDocParser\Lexer\Lexer;
1011
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1112
use PHPStan\PhpDocParser\Parser\TokenIterator;
1213
use PHPStan\Reflection\ExtendedMethodReflection;
1314
use PHPUnit\Framework\TestCase;
14-
use ReflectionMethod;
1515
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
1616
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage;
1717
use function array_merge;
1818
use function is_string;
1919
use function strpos;
20-
use const PHP_VERSION_ID;
2120

2221
class PhpUnitUsageProvider implements MemberUsageProvider
2322
{
@@ -115,10 +114,6 @@ private function getDataProvidersFromAnnotations($rawPhpDoc): array
115114
*/
116115
private function getDataProvidersFromAttributes(ReflectionMethod $method): array
117116
{
118-
if (PHP_VERSION_ID < 8_00_00) {
119-
return [];
120-
}
121-
122117
$result = [];
123118

124119
foreach ($method->getAttributes('PHPUnit\Framework\Attributes\DataProvider') as $providerAttributeReflection) {
@@ -134,7 +129,7 @@ private function getDataProvidersFromAttributes(ReflectionMethod $method): array
134129

135130
private function hasAttribute(ReflectionMethod $method, string $attributeClass): bool
136131
{
137-
return PHP_VERSION_ID >= 8_00_00 && $method->getAttributes($attributeClass) !== [];
132+
return $method->getAttributes($attributeClass) !== [];
138133
}
139134

140135
private function hasAnnotation(ReflectionMethod $method, string $string): bool

src/Provider/SymfonyUsageProvider.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PhpParser\Node;
1010
use PhpParser\Node\Stmt\Return_;
1111
use PHPStan\Analyser\Scope;
12+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
13+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
1214
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
1315
use PHPStan\DependencyInjection\Container;
1416
use PHPStan\DependencyInjection\ParameterNotFoundException;
@@ -19,8 +21,6 @@
1921
use RecursiveDirectoryIterator;
2022
use RecursiveIteratorIterator;
2123
use ReflectionAttribute;
22-
use ReflectionClass;
23-
use ReflectionMethod;
2424
use Reflector;
2525
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantRef;
2626
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantUsage;
@@ -42,7 +42,6 @@
4242
use function simplexml_load_string;
4343
use function sprintf;
4444
use function strpos;
45-
use const PHP_VERSION_ID;
4645

4746
class SymfonyUsageProvider implements MemberUsageProvider
4847
{
@@ -350,8 +349,10 @@ protected function isConstructorWithAsControllerAttribute(ReflectionMethod $meth
350349

351350
protected function isMethodWithRouteAttribute(ReflectionMethod $method): bool
352351
{
353-
return $this->hasAttribute($method, 'Symfony\Component\Routing\Attribute\Route', ReflectionAttribute::IS_INSTANCEOF)
354-
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', ReflectionAttribute::IS_INSTANCEOF);
352+
$isInstanceOf = 2; // ReflectionAttribute::IS_INSTANCEOF, since PHP 8.0
353+
354+
return $this->hasAttribute($method, 'Symfony\Component\Routing\Attribute\Route', $isInstanceOf)
355+
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', $isInstanceOf);
355356
}
356357

357358
/**
@@ -371,15 +372,11 @@ protected function isProbablySymfonyListener(ReflectionMethod $method): bool
371372
}
372373

373374
/**
374-
* @param ReflectionClass<object>|ReflectionMethod $classOrMethod
375+
* @param ReflectionClass|ReflectionMethod $classOrMethod
375376
* @param ReflectionAttribute::IS_*|0 $flags
376377
*/
377378
protected function hasAttribute(Reflector $classOrMethod, string $attributeClass, int $flags = 0): bool
378379
{
379-
if (PHP_VERSION_ID < 8_00_00) {
380-
return false;
381-
}
382-
383380
if ($classOrMethod->getAttributes($attributeClass) !== []) {
384381
return true;
385382
}

src/Provider/VendorUsageProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use ReflectionClass;
77
use ReflectionMethod;
88
use function array_keys;
9-
use function str_starts_with;
109
use function strlen;
1110
use function strpos;
1211
use function substr;
@@ -82,7 +81,7 @@ private function isForeignMethod(ReflectionClass $reflectionClass, string $metho
8281
}
8382

8483
foreach ($this->vendorDirs as $vendorDir) {
85-
if (str_starts_with($filePath, $vendorDir)) {
84+
if (strpos($filePath, $vendorDir) === 0) {
8685
return true;
8786
}
8887
}

0 commit comments

Comments
 (0)