diff --git a/.github/linters/phpstan.neon b/.github/linters/phpstan.neon index bb467c3..048bc32 100644 --- a/.github/linters/phpstan.neon +++ b/.github/linters/phpstan.neon @@ -3,7 +3,7 @@ includes: parameters: level: max - checkGenericClassInNonGenericObjectType: false ignoreErrors: + - identifier: missingType.generics - '#constructor invoked with#' - - '#FunctionVariant constructor expects#' \ No newline at end of file + - '#FunctionVariant constructor expects#' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index befd8d2..93ec5bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 }}- @@ -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 diff --git a/Dockerfile b/Dockerfile index 3c2e56b..460aa40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.1 +FROM php:7.4 # Enable phpdebug RUN apt-get update \ diff --git a/README.md b/README.md index 2fbc906..fc12ae3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 289ff25..95a5987 100644 --- a/composer.json +++ b/composer.json @@ -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" + ] + } + } } diff --git a/src/Rule/EnumAlwaysUsedConstantsExtension.php b/src/Rule/EnumAlwaysUsedConstantsExtension.php index c0e3462..1bab1d2 100644 --- a/src/Rule/EnumAlwaysUsedConstantsExtension.php +++ b/src/Rule/EnumAlwaysUsedConstantsExtension.php @@ -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); } diff --git a/src/Rule/NoDuplicateEnumValueRule.php b/src/Rule/NoDuplicateEnumValueRule.php index 80cab59..b83f9c7 100644 --- a/src/Rule/NoDuplicateEnumValueRule.php +++ b/src/Rule/NoDuplicateEnumValueRule.php @@ -10,6 +10,7 @@ use PHPStan\Analyser\Scope; use PHPStan\Node\ClassConstantsNode; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; /** @@ -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(), ]; } diff --git a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php index aa816e8..dbe7fef 100644 --- a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php +++ b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php @@ -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())); } /** diff --git a/tools/composer b/tools/composer index 7de0507..0497759 100755 --- a/tools/composer +++ b/tools/composer @@ -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 "$@" diff --git a/tools/php b/tools/php index 93d6d5d..43e34d7 100755 --- a/tools/php +++ b/tools/php @@ -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 "$@" diff --git a/tools/phpdbg b/tools/phpdbg index 856cc80..ed6b75b 100755 --- a/tools/phpdbg +++ b/tools/phpdbg @@ -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 "$@"