Skip to content

Commit 56ae8ae

Browse files
authored
Upgrade to PHPStan 2 (#35)
* Set PHP 7.4 as minimal version * Upgrade code to PHPStan 2.x * Fix bash scripts * Upgrade Github Actions dependencies
1 parent 72f54c9 commit 56ae8ae

11 files changed

+85
-69
lines changed

.github/linters/phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ includes:
33

44
parameters:
55
level: max
6-
checkGenericClassInNonGenericObjectType: false
76
ignoreErrors:
7+
- identifier: missingType.generics
88
- '#constructor invoked with#'
9-
- '#FunctionVariant constructor expects#'
9+
- '#FunctionVariant constructor expects#'

.github/workflows/ci.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ on: push
44

55
jobs:
66
php-tests:
7-
87
strategy:
98
matrix:
10-
php: [ '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
11-
prefer: [ 'lowest', 'stable' ]
9+
php: ["7.4", "8.0", "8.1", "8.2", "8.3"]
10+
prefer: ["lowest", "stable"]
1211

1312
name: Test on PHP ${{ matrix.php }} with ${{ matrix.prefer }} composer prefer option
1413
runs-on: ubuntu-latest
1514

1615
steps:
1716
- name: Checkout Code
18-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1918

2019
- name: Install PHP
2120
uses: shivammathur/setup-php@v2
@@ -27,7 +26,7 @@ jobs:
2726

2827
- name: Cache Composer packages
2928
id: composer-cache
30-
uses: actions/cache@v2
29+
uses: actions/cache@v4
3130
with:
3231
path: vendor
3332
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ matrix.prefer }}-
@@ -46,20 +45,30 @@ jobs:
4645
runs-on: ubuntu-latest
4746

4847
steps:
48+
# Master branch should be available for the linter
49+
- name: Checkout Code
50+
uses: actions/checkout@v4
51+
with:
52+
ref: master
53+
4954
- name: Checkout Code
50-
uses: actions/checkout@v2
55+
uses: actions/checkout@v4
5156

5257
- name: Install PHP
5358
uses: shivammathur/setup-php@v2
5459
with:
55-
php-version: 7.1
60+
php-version: 7.4
5661

5762
- name: Install dependencies
5863
run: composer update --prefer-stable --prefer-dist --no-progress
5964

6065
- name: Lint Code
61-
uses: github/super-linter@v4
66+
uses: super-linter/super-linter@v7
6267
env:
6368
FILTER_REGEX_EXCLUDE: .*vendor.*
6469
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
VALIDATE_CHECKOV: false
71+
VALIDATE_JSCPD: false
6572
VALIDATE_PHP_PSALM: false
73+
VALIDATE_PHP_PHPSTAN: false # temporary disabled until superlinter supports phpstan 2
74+
VALIDATE_YAML_PRETTIER: false

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7.1
1+
FROM php:7.4
22

33
# Enable phpdebug
44
RUN apt-get update \

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
[![Packagist Version](https://img.shields.io/packagist/v/timeweb/phpstan-enum)](https://packagist.org/packages/timeweb/phpstan-enum)
44
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/timeweb/phpstan-enum/CI)
55

6-
* [PHPStan](https://phpstan.org/)
7-
* [PHP Enum](https://github.com/myclabs/php-enum)
6+
- [PHPStan](https://phpstan.org/)
7+
- [PHP Enum](https://github.com/myclabs/php-enum)
88

99
This extension defines dynamic methods for `MyCLabs\Enum\Enum` subclasses.
1010

composer.json

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
{
2-
"name": "timeweb/phpstan-enum",
3-
"description": "Enum class reflection extension for PHPStan",
4-
"type": "phpstan-extension",
5-
"keywords": ["enum", "phpstan"],
6-
"license": "MIT",
7-
"require": {
8-
"php": "^7.1|^8.0",
9-
"myclabs/php-enum": "^1.2",
10-
"phpstan/phpstan": "^1.0"
11-
},
12-
"require-dev": {
13-
"phpunit/phpunit": "^7.0|^9.0"
14-
},
15-
"autoload": {
16-
"psr-4": {
17-
"Timeweb\\PHPStan\\": "src/"
18-
}
19-
},
20-
"autoload-dev": {
21-
"psr-4": {
22-
"Timeweb\\Tests\\PHPStan\\": "tests/"
23-
}
24-
},
25-
"scripts": {
26-
"test": "phpunit"
27-
},
28-
"extra": {
29-
"phpstan": {
30-
"includes": [
31-
"extension.neon",
32-
"rules.neon"
33-
]
34-
}
2+
"name": "timeweb/phpstan-enum",
3+
"description": "Enum class reflection extension for PHPStan",
4+
"type": "phpstan-extension",
5+
"keywords": [
6+
"enum",
7+
"phpstan"
8+
],
9+
"license": "MIT",
10+
"require": {
11+
"php": "^7.4|^8.0",
12+
"myclabs/php-enum": "^1.2",
13+
"phpstan/phpstan": "^2.0"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^7.0|^9.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Timeweb\\PHPStan\\": "src/"
3521
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Timeweb\\Tests\\PHPStan\\": "tests/"
26+
}
27+
},
28+
"scripts": {
29+
"test": "phpunit"
30+
},
31+
"extra": {
32+
"phpstan": {
33+
"includes": [
34+
"extension.neon",
35+
"rules.neon"
36+
]
37+
}
38+
}
3639
}

src/Rule/EnumAlwaysUsedConstantsExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace Timeweb\PHPStan\Rule;
66

77
use MyCLabs\Enum\Enum;
8-
use PHPStan\Reflection\ConstantReflection;
8+
use PHPStan\Reflection\ClassConstantReflection;
99
use PHPStan\Rules\Constants\AlwaysUsedClassConstantsExtension;
1010

1111
class EnumAlwaysUsedConstantsExtension implements AlwaysUsedClassConstantsExtension
1212
{
13-
public function isAlwaysUsed(ConstantReflection $constant): bool
13+
public function isAlwaysUsed(ClassConstantReflection $constant): bool
1414
{
1515
return $constant->getDeclaringClass()->isSubclassOf(Enum::class);
1616
}

src/Rule/NoDuplicateEnumValueRule.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Node\ClassConstantsNode;
1212
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
1314
use PHPStan\ShouldNotHappenException;
1415

1516
/**
@@ -43,11 +44,16 @@ public function processNode(Node $node, Scope $scope): array
4344
}
4445

4546
return [
46-
sprintf(
47-
'Enum %s contains duplicated values for %s properties',
48-
$classReflection->getName(),
49-
implode(', ', $duplicatedKeysValue)
50-
),
47+
RuleErrorBuilder::message(
48+
sprintf(
49+
'Enum %s contains duplicated values for %s properties',
50+
$classReflection->getName(),
51+
implode(', ', $duplicatedKeysValue)
52+
)
53+
)
54+
->line($node->getLine())
55+
->identifier('timewebEnum.duplicatedValues')
56+
->build(),
5157
];
5258
}
5359

tests/Reflection/EnumMethodsClassReflectionExtensionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function testEnumMethodProperties(string $propertyName): void
9393
{
9494
$classReflection = $this->reflectionProvider->getClass(EnumFixture::class);
9595
$methodReflection = $this->reflectionExtension->getMethod($classReflection, $propertyName);
96-
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
9796

9897
$this->assertSame($propertyName, $methodReflection->getName());
9998
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
10099
$this->assertTrue($methodReflection->isStatic());
101100
$this->assertFalse($methodReflection->isPrivate());
102101
$this->assertTrue($methodReflection->isPublic());
103-
$this->assertSame(EnumFixture::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
104102
}
105103

106104
/**

tools/composer

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ mkdir -p "$HOME/.composer/cache/"
44

55
test -t 1 && USE_TTY="--tty"
66

7-
docker run --rm --interactive ${USE_TTY} \
8-
--user $UID:$UID \
9-
--volume "$PWD":/app \
10-
--volume "$HOME/.composer":/tmp/.composer \
11-
--env COMPOSER_HOME=/tmp/.composer \
12-
timeweb/phpstan-enum composer "$@"
7+
docker run --rm --interactive "${USE_TTY}" \
8+
--user $UID:$UID \
9+
--volume "$PWD":/app \
10+
--volume "$HOME/.composer":/tmp/.composer \
11+
--env COMPOSER_HOME=/tmp/.composer \
12+
timeweb/phpstan-enum composer "$@"

tools/php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
test -t 1 && USE_TTY="--tty"
44

5-
docker run --rm --init --interactive ${USE_TTY} \
6-
--user $UID:$UID \
7-
--volume "$PWD:/app" \
8-
timeweb/phpstan-enum php "$@"
5+
docker run --rm --init --interactive "${USE_TTY}" \
6+
--user $UID:$UID \
7+
--volume "$PWD:/app" \
8+
timeweb/phpstan-enum php "$@"

tools/phpdbg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
test -t 1 && USE_TTY="--tty"
44

5-
docker run --rm --init --interactive ${USE_TTY} \
6-
--user $UID:$UID \
7-
--volume "$PWD:/app" \
8-
timeweb/phpstan-enum phpdbg "$@"
5+
docker run --rm --init --interactive "${USE_TTY}" \
6+
--user $UID:$UID \
7+
--volume "$PWD:/app" \
8+
timeweb/phpstan-enum phpdbg "$@"

0 commit comments

Comments
 (0)