Skip to content

Upgrade to PHPStan 2 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/linters/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ includes:

parameters:
level: max
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- identifier: missingType.generics
- '#constructor invoked with#'
- '#FunctionVariant constructor expects#'
- '#FunctionVariant constructor expects#'
25 changes: 17 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ on: push

jobs:
php-tests:

strategy:
matrix:
php: [ '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
prefer: [ 'lowest', 'stable' ]
php: ["7.4", "8.0", "8.1", "8.2", "8.3"]
prefer: ["lowest", "stable"]

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

steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand All @@ -27,7 +26,7 @@ jobs:

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

steps:
# Master branch should be available for the linter
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: master

- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.1
php-version: 7.4

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

- name: Lint Code
uses: github/super-linter@v4
uses: super-linter/super-linter@v7
env:
FILTER_REGEX_EXCLUDE: .*vendor.*
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_CHECKOV: false
VALIDATE_JSCPD: false
VALIDATE_PHP_PSALM: false
VALIDATE_PHP_PHPSTAN: false # temporary disabled until superlinter supports phpstan 2
VALIDATE_YAML_PRETTIER: false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7.1
FROM php:7.4

# Enable phpdebug
RUN apt-get update \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[![Packagist Version](https://img.shields.io/packagist/v/timeweb/phpstan-enum)](https://packagist.org/packages/timeweb/phpstan-enum)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/timeweb/phpstan-enum/CI)

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

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

Expand Down
69 changes: 36 additions & 33 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
{
"name": "timeweb/phpstan-enum",
"description": "Enum class reflection extension for PHPStan",
"type": "phpstan-extension",
"keywords": ["enum", "phpstan"],
"license": "MIT",
"require": {
"php": "^7.1|^8.0",
"myclabs/php-enum": "^1.2",
"phpstan/phpstan": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^9.0"
},
"autoload": {
"psr-4": {
"Timeweb\\PHPStan\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Timeweb\\Tests\\PHPStan\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
},
"extra": {
"phpstan": {
"includes": [
"extension.neon",
"rules.neon"
]
}
"name": "timeweb/phpstan-enum",
"description": "Enum class reflection extension for PHPStan",
"type": "phpstan-extension",
"keywords": [
"enum",
"phpstan"
],
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"myclabs/php-enum": "^1.2",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^9.0"
},
"autoload": {
"psr-4": {
"Timeweb\\PHPStan\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Timeweb\\Tests\\PHPStan\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
},
"extra": {
"phpstan": {
"includes": [
"extension.neon",
"rules.neon"
]
}
}
}
4 changes: 2 additions & 2 deletions src/Rule/EnumAlwaysUsedConstantsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Timeweb\PHPStan\Rule;

use MyCLabs\Enum\Enum;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Rules\Constants\AlwaysUsedClassConstantsExtension;

class EnumAlwaysUsedConstantsExtension implements AlwaysUsedClassConstantsExtension
{
public function isAlwaysUsed(ConstantReflection $constant): bool
public function isAlwaysUsed(ClassConstantReflection $constant): bool
{
return $constant->getDeclaringClass()->isSubclassOf(Enum::class);
}
Expand Down
16 changes: 11 additions & 5 deletions src/Rule/NoDuplicateEnumValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassConstantsNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;

/**
Expand Down Expand Up @@ -43,11 +44,16 @@ public function processNode(Node $node, Scope $scope): array
}

return [
sprintf(
'Enum %s contains duplicated values for %s properties',
$classReflection->getName(),
implode(', ', $duplicatedKeysValue)
),
RuleErrorBuilder::message(
sprintf(
'Enum %s contains duplicated values for %s properties',
$classReflection->getName(),
implode(', ', $duplicatedKeysValue)
)
)
->line($node->getLine())
->identifier('timewebEnum.duplicatedValues')
->build(),
];
}

Expand Down
2 changes: 0 additions & 2 deletions tests/Reflection/EnumMethodsClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ public function testEnumMethodProperties(string $propertyName): void
{
$classReflection = $this->reflectionProvider->getClass(EnumFixture::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, $propertyName);
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$this->assertSame($propertyName, $methodReflection->getName());
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
$this->assertTrue($methodReflection->isStatic());
$this->assertFalse($methodReflection->isPrivate());
$this->assertTrue($methodReflection->isPublic());
$this->assertSame(EnumFixture::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tools/composer
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ mkdir -p "$HOME/.composer/cache/"

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

docker run --rm --interactive ${USE_TTY} \
--user $UID:$UID \
--volume "$PWD":/app \
--volume "$HOME/.composer":/tmp/.composer \
--env COMPOSER_HOME=/tmp/.composer \
timeweb/phpstan-enum composer "$@"
docker run --rm --interactive "${USE_TTY}" \
--user $UID:$UID \
--volume "$PWD":/app \
--volume "$HOME/.composer":/tmp/.composer \
--env COMPOSER_HOME=/tmp/.composer \
timeweb/phpstan-enum composer "$@"
8 changes: 4 additions & 4 deletions tools/php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

docker run --rm --init --interactive ${USE_TTY} \
--user $UID:$UID \
--volume "$PWD:/app" \
timeweb/phpstan-enum php "$@"
docker run --rm --init --interactive "${USE_TTY}" \
--user $UID:$UID \
--volume "$PWD:/app" \
timeweb/phpstan-enum php "$@"
8 changes: 4 additions & 4 deletions tools/phpdbg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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