diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 15b77335..00000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,17 +0,0 @@ -version: 2 -updates: - - package-ecosystem: composer - directory: "/" - schedule: - interval: monthly - open-pull-requests-limit: 10 - - package-ecosystem: composer - directory: "/build-cs" - schedule: - interval: monthly - open-pull-requests-limit: 10 - - package-ecosystem: github-actions - directory: "/" - schedule: - interval: monthly - open-pull-requests-limit: 10 diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 00000000..b775cc18 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,24 @@ +{ + "extends": [ + "config:base", + "schedule:weekly" + ], + "rangeStrategy": "update-lockfile", + "packageRules": [ + { + "matchPaths": ["+(composer.json)"], + "enabled": true, + "groupName": "root-composer" + }, + { + "matchPaths": ["build-cs/**"], + "enabled": true, + "groupName": "build-cs" + }, + { + "matchPaths": [".github/**"], + "enabled": true, + "groupName": "github-actions" + } + ] +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 59b79ed7..8d7d7aa8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,7 @@ on: pull_request: push: branches: - - "master" + - "1.4.x" jobs: lint: @@ -16,16 +16,16 @@ jobs: strategy: matrix: php-version: - - "7.1" - "7.2" - "7.3" - "7.4" - "8.0" - "8.1" + - "8.2" steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: actions/checkout@v3 - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -37,10 +37,10 @@ jobs: run: "composer validate" - name: "Install dependencies" - run: "composer install --no-interaction --no-progress --no-suggest" + run: "composer install --no-interaction --no-progress" - name: "Downgrade PHPUnit" - if: matrix.php-version == '7.1' || matrix.php-version == '7.2' || matrix.php-version == '7.3' + if: matrix.php-version == '7.2' || matrix.php-version == '7.3' run: "composer require --dev phpunit/phpunit:^7.5.20 --update-with-dependencies" - name: "Lint" @@ -53,7 +53,7 @@ jobs: steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: actions/checkout@v3 - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -65,7 +65,7 @@ jobs: run: "composer validate" - name: "Install dependencies" - run: "composer install --no-interaction --no-progress --no-suggest" + run: "composer install --no-interaction --no-progress" - name: "Lint" run: "make lint" @@ -81,19 +81,19 @@ jobs: fail-fast: false matrix: php-version: - - "7.1" - "7.2" - "7.3" - "7.4" - "8.0" - "8.1" + - "8.2" dependencies: - "lowest" - "highest" steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: actions/checkout@v3 - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -103,14 +103,14 @@ jobs: - name: "Install lowest dependencies" if: ${{ matrix.dependencies == 'lowest' }} - run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest" + run: "composer update --prefer-lowest --no-interaction --no-progress" - name: "Install highest dependencies" if: ${{ matrix.dependencies == 'highest' }} - run: "composer update --no-interaction --no-progress --no-suggest" + run: "composer update --no-interaction --no-progress" - name: "Downgrade PHPUnit" - if: matrix.php-version == '7.1' || matrix.php-version == '7.2' || matrix.php-version == '7.3' + if: matrix.php-version == '7.2' || matrix.php-version == '7.3' run: "composer require --dev phpunit/phpunit:^7.5.20 --update-with-dependencies" - name: "Tests" @@ -124,19 +124,19 @@ jobs: fail-fast: false matrix: php-version: - - "7.1" - "7.2" - "7.3" - "7.4" - "8.0" - "8.1" + - "8.2" dependencies: - "lowest" - "highest" steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: actions/checkout@v3 - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -148,14 +148,14 @@ jobs: - name: "Install lowest dependencies" if: ${{ matrix.dependencies == 'lowest' }} - run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest" + run: "composer update --prefer-lowest --no-interaction --no-progress" - name: "Install highest dependencies" if: ${{ matrix.dependencies == 'highest' }} - run: "composer update --no-interaction --no-progress --no-suggest" + run: "composer update --no-interaction --no-progress" - name: "Downgrade PHPUnit" - if: matrix.php-version == '7.1' || matrix.php-version == '7.2' || matrix.php-version == '7.3' + if: matrix.php-version == '7.2' || matrix.php-version == '7.3' run: "composer require --dev phpunit/phpunit:^7.5.20 --update-with-dependencies" - name: "PHPStan" diff --git a/.github/workflows/create-tag.yml b/.github/workflows/create-tag.yml new file mode 100644 index 00000000..8452d986 --- /dev/null +++ b/.github/workflows/create-tag.yml @@ -0,0 +1,53 @@ +# https://help.github.com/en/categories/automating-your-workflow-with-github-actions + +name: "Create tag" + +on: + # https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch + workflow_dispatch: + inputs: + version: + description: 'Next version' + required: true + default: 'patch' + type: choice + options: + - patch + - minor + +jobs: + create-tag: + name: "Create tag" + runs-on: "ubuntu-latest" + steps: + - name: "Checkout" + uses: actions/checkout@v3 + with: + fetch-depth: 0 + token: ${{ secrets.PHPSTAN_BOT_TOKEN }} + + - name: 'Get Previous tag' + id: previoustag + uses: "WyriHaximus/github-action-get-previous-tag@v1" + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + + - name: 'Get next versions' + id: semvers + uses: "WyriHaximus/github-action-next-semvers@v1" + with: + version: ${{ steps.previoustag.outputs.tag }} + + - name: "Create new minor tag" + uses: rickstaa/action-create-tag@v1 + if: inputs.version == 'minor' + with: + tag: ${{ steps.semvers.outputs.minor }} + message: ${{ steps.semvers.outputs.minor }} + + - name: "Create new patch tag" + uses: rickstaa/action-create-tag@v1 + if: inputs.version == 'patch' + with: + tag: ${{ steps.semvers.outputs.patch }} + message: ${{ steps.semvers.outputs.patch }} diff --git a/.github/workflows/lock-closed-issues.yml b/.github/workflows/lock-closed-issues.yml index 960c1ba5..4c7990df 100644 --- a/.github/workflows/lock-closed-issues.yml +++ b/.github/workflows/lock-closed-issues.yml @@ -8,14 +8,14 @@ jobs: lock: runs-on: ubuntu-latest steps: - - uses: dessant/lock-threads@v2 + - uses: dessant/lock-threads@v4 with: github-token: ${{ github.token }} - issue-lock-inactive-days: '31' - issue-exclude-created-before: '' - issue-exclude-labels: '' - issue-lock-labels: '' - issue-lock-comment: > + issue-inactive-days: '31' + exclude-issue-created-before: '' + exclude-any-issue-labels: '' + add-issue-labels: '' + issue-comment: > This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. diff --git a/.github/workflows/release-toot.yml b/.github/workflows/release-toot.yml new file mode 100644 index 00000000..6a1c8156 --- /dev/null +++ b/.github/workflows/release-toot.yml @@ -0,0 +1,21 @@ +name: Toot release + +# More triggers +# https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#release +on: + release: + types: [published] + +jobs: + toot: + runs-on: ubuntu-latest + steps: + - uses: cbrgm/mastodon-github-action@v1 + if: ${{ !github.event.repository.private }} + with: + # GitHub event payload + # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#release + message: "New release: ${{ github.event.repository.name }} ${{ github.event.release.tag_name }} ${{ github.event.release.html_url }} #phpstan" + env: + MASTODON_URL: https://phpc.social + MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} diff --git a/.github/workflows/release-tweet.yml b/.github/workflows/release-tweet.yml new file mode 100644 index 00000000..09b39ded --- /dev/null +++ b/.github/workflows/release-tweet.yml @@ -0,0 +1,24 @@ +name: Tweet release + +# More triggers +# https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#release +on: + release: + types: [published] + +jobs: + tweet: + runs-on: ubuntu-latest + steps: + - uses: Eomm/why-don-t-you-tweet@v1 + if: ${{ !github.event.repository.private }} + with: + # GitHub event payload + # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#release + tweet-message: "New release: ${{ github.event.repository.name }} ${{ github.event.release.tag_name }} ${{ github.event.release.html_url }} #phpstan" + env: + # Get your tokens from https://developer.twitter.com/apps + TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_CONSUMER_API_KEY }} + TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_CONSUMER_API_SECRET }} + TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} + TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 225470a6..bac4a006 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,19 +14,19 @@ jobs: steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: actions/checkout@v3 - name: Generate changelog id: changelog - uses: metcalfc/changelog-generator@v1.0.0 + uses: metcalfc/changelog-generator@v4.0.1 with: - myToken: ${{ secrets.GITHUB_TOKEN }} + myToken: ${{ secrets.PHPSTAN_BOT_TOKEN }} - name: "Create release" id: create-release uses: actions/create-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.PHPSTAN_BOT_TOKEN }} with: tag_name: ${{ github.ref }} release_name: ${{ github.ref }} diff --git a/.gitignore b/.gitignore index d6a83e59..2db21315 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /tests/tmp /vendor -composer.lock +/composer.lock .phpunit.result.cache diff --git a/README.md b/README.md index d5400c61..246c0c84 100644 --- a/README.md +++ b/README.md @@ -55,22 +55,44 @@ includes: ``` +## Disabling rules + +You can disable rules using configuration parameters: + +```neon +parameters: + strictRules: + disallowedLooseComparison: false + booleansInConditions: false + uselessCast: false + requireParentConstructorCall: false + disallowedConstructs: false + overwriteVariablesWithLoop: false + closureUsesThis: false + matchingInheritedMethodNames: false + numericOperandsInArithmeticOperators: false + strictCalls: false + switchConditionsMatchingType: false + noVariableVariables: false +``` ## Enabling rules one-by-one -If you don't want to start using all the available strict rules at once but only one or two, you can! Just don't include the whole `rules.neon` from this package in your configuration, but look at its contents and copy only the rules you want to your configuration under the `services` key: +If you don't want to start using all the available strict rules at once but only one or two, you can! -``` -services: - - - class: PHPStan\Rules\StrictCalls\StrictFunctionCallsRule - tags: - - phpstan.rules.rule - - - - class: PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule - tags: - - phpstan.rules.rule +You can disable all rules from the included `rules.neon` with: + +```neon +parameters: + strictRules: + allRules: false ``` -*Unfortunately, you cannot use phpstan/extension-installer in this case.* +Then you can re-enable individual rules with configuration parameters: + +```neon +parameters: + strictRules: + allRules: false + booleansInConditions: true +``` diff --git a/build-cs/.gitignore b/build-cs/.gitignore index ff72e2d0..61ead866 100644 --- a/build-cs/.gitignore +++ b/build-cs/.gitignore @@ -1,2 +1 @@ -/composer.lock /vendor diff --git a/build-cs/composer.json b/build-cs/composer.json index ed7744e1..e3079710 100644 --- a/build-cs/composer.json +++ b/build-cs/composer.json @@ -2,6 +2,11 @@ "require-dev": { "consistence-community/coding-standard": "^3.10", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", - "slevomat/coding-standard": "^6.4" + "slevomat/coding-standard": "^7.0" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/build-cs/composer.lock b/build-cs/composer.lock new file mode 100644 index 00000000..4bcc8de4 --- /dev/null +++ b/build-cs/composer.lock @@ -0,0 +1,322 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "4485bbedba7bcc71ace5f69dbb9b6c47", + "packages": [], + "packages-dev": [ + { + "name": "consistence-community/coding-standard", + "version": "3.11.1", + "source": { + "type": "git", + "url": "https://github.com/consistence-community/coding-standard.git", + "reference": "4632fead8c9ee8f50044fcbce9f66c797b34c0df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/consistence-community/coding-standard/zipball/4632fead8c9ee8f50044fcbce9f66c797b34c0df", + "reference": "4632fead8c9ee8f50044fcbce9f66c797b34c0df", + "shasum": "" + }, + "require": { + "php": ">=7.4", + "slevomat/coding-standard": "~7.0", + "squizlabs/php_codesniffer": "~3.6.0" + }, + "replace": { + "consistence/coding-standard": "3.10.*" + }, + "require-dev": { + "phing/phing": "2.16.4", + "php-parallel-lint/php-parallel-lint": "1.3.0", + "phpunit/phpunit": "9.5.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Consistence\\": [ + "Consistence" + ] + }, + "classmap": [ + "Consistence" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "VaĊĦek Purchart", + "email": "me@vasekpurchart.cz", + "homepage": "http://vasekpurchart.cz" + } + ], + "description": "Consistence - Coding Standard - PHP Code Sniffer rules", + "keywords": [ + "Coding Standard", + "PHPCodeSniffer", + "codesniffer", + "coding", + "cs", + "phpcs", + "ruleset", + "sniffer", + "standard" + ], + "support": { + "issues": "https://github.com/consistence-community/coding-standard/issues", + "source": "https://github.com/consistence-community/coding-standard/tree/3.11.1" + }, + "time": "2021-05-03T18:13:22+00:00" + }, + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v0.7.2", + "source": { + "type": "git", + "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" + }, + "require-dev": { + "composer/composer": "*", + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcbf", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "support": { + "issues": "https://github.com/dealerdirect/phpcodesniffer-composer-installer/issues", + "source": "https://github.com/dealerdirect/phpcodesniffer-composer-installer" + }, + "time": "2022-02-04T12:51:07+00:00" + }, + { + "name": "phpstan/phpdoc-parser", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpdoc-parser.git", + "reference": "981cc368a216c988e862a75e526b6076987d1b50" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/981cc368a216c988e862a75e526b6076987d1b50", + "reference": "981cc368a216c988e862a75e526b6076987d1b50", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^1.5", + "phpstan/phpstan-strict-rules": "^1.0", + "phpunit/phpunit": "^9.5", + "symfony/process": "^5.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPStan\\PhpDocParser\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPDoc parser with support for nullable, intersection and generic types", + "support": { + "issues": "https://github.com/phpstan/phpdoc-parser/issues", + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.5.1" + }, + "time": "2022-05-05T11:32:40+00:00" + }, + { + "name": "slevomat/coding-standard", + "version": "7.2.1", + "source": { + "type": "git", + "url": "https://github.com/slevomat/coding-standard.git", + "reference": "aff06ae7a84e4534bf6f821dc982a93a5d477c90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/aff06ae7a84e4534bf6f821dc982a93a5d477c90", + "reference": "aff06ae7a84e4534bf6f821dc982a93a5d477c90", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7", + "php": "^7.2 || ^8.0", + "phpstan/phpdoc-parser": "^1.5.1", + "squizlabs/php_codesniffer": "^3.6.2" + }, + "require-dev": { + "phing/phing": "2.17.3", + "php-parallel-lint/php-parallel-lint": "1.3.2", + "phpstan/phpstan": "1.4.10|1.7.1", + "phpstan/phpstan-deprecation-rules": "1.0.0", + "phpstan/phpstan-phpunit": "1.0.0|1.1.1", + "phpstan/phpstan-strict-rules": "1.2.3", + "phpunit/phpunit": "7.5.20|8.5.21|9.5.20" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-master": "7.x-dev" + } + }, + "autoload": { + "psr-4": { + "SlevomatCodingStandard\\": "SlevomatCodingStandard" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.", + "support": { + "issues": "https://github.com/slevomat/coding-standard/issues", + "source": "https://github.com/slevomat/coding-standard/tree/7.2.1" + }, + "funding": [ + { + "url": "https://github.com/kukulich", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/slevomat/coding-standard", + "type": "tidelift" + } + ], + "time": "2022-05-25T10:58:12+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.6.2", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5e4e71592f69da17871dba6e80dd51bce74a351a", + "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2021-12-12T21:44:58+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/composer.json b/composer.json index 08b74ead..a2424e49 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "MIT" ], "require": { - "php": "^7.1 || ^8.0", - "phpstan/phpstan": "^1.2.0" + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.7" }, "require-dev": { "nikic/php-parser": "^4.13.0", @@ -22,9 +22,6 @@ "sort-packages": true }, "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - }, "phpstan": { "includes": [ "rules.neon" diff --git a/phpcs.xml b/phpcs.xml index 067a6a8e..95032a6e 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,5 +1,6 @@ - + + @@ -8,59 +9,103 @@ src tests + - - + + - + + + + + - + - - - - - - + + + + + 10 - - - + + 10 + - - - + + + + + 10 + + + - - + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - tests/tmp + + tests/*/data diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c271301d..899d271d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -62,16 +62,6 @@ parameters: count: 1 path: src/Rules/Classes/RequireParentConstructCallRule.php - - - message: "#^Method PHPStan\\\\Rules\\\\Classes\\\\RequireParentConstructCallRule\\:\\:getParentConstructorClass\\(\\) has parameter \\$classReflection with generic class ReflectionClass but does not specify its types\\: T$#" - count: 1 - path: src/Rules/Classes/RequireParentConstructCallRule.php - - - - message: "#^Method PHPStan\\\\Rules\\\\Classes\\\\RequireParentConstructCallRule\\:\\:getParentConstructorClass\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#" - count: 1 - path: src/Rules/Classes/RequireParentConstructCallRule.php - - message: "#^Class PHPStan\\\\Rules\\\\DisallowedConstructs\\\\DisallowedBacktickRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#" count: 1 @@ -112,16 +102,6 @@ parameters: count: 1 path: src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php - - - message: "#^Class PHPStan\\\\Rules\\\\Methods\\\\WrongCaseOfInheritedMethodRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#" - count: 1 - path: src/Rules/Methods/WrongCaseOfInheritedMethodRule.php - - - - message: "#^Parameter \\#1 \\$node \\(PhpParser\\\\Node\\\\Stmt\\\\ClassMethod\\) of method PHPStan\\\\Rules\\\\Methods\\\\WrongCaseOfInheritedMethodRule\\:\\:processNode\\(\\) should be contravariant with parameter \\$node \\(PhpParser\\\\Node\\) of method PHPStan\\\\Rules\\\\Rule\\\\:\\:processNode\\(\\)$#" - count: 1 - path: src/Rules/Methods/WrongCaseOfInheritedMethodRule.php - - message: "#^Class PHPStan\\\\Rules\\\\Operators\\\\OperandInArithmeticIncrementOrDecrementRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index 717a8b30..5df5c578 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,20 +8,3 @@ includes: parameters: excludePaths: - tests/*/data/* - -services: - scopeIsInClass: - class: PHPStan\Internal\ScopeIsInClassTypeSpecifyingExtension - arguments: - isInMethodName: isInClass - removeNullMethodName: getClassReflection - tags: - - phpstan.typeSpecifier.methodTypeSpecifyingExtension - - scopeIsInTrait: - class: PHPStan\Internal\ScopeIsInClassTypeSpecifyingExtension - arguments: - isInMethodName: isInTrait - removeNullMethodName: getTraitReflection - tags: - - phpstan.typeSpecifier.methodTypeSpecifyingExtension diff --git a/phpunit.xml b/phpunit.xml index e5f534e4..f60bcee1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,7 +10,7 @@ beStrictAboutTodoAnnotatedTests="true" failOnRisky="true" failOnWarning="true" - xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xml" > diff --git a/rules.neon b/rules.neon index 12e6590b..7dadfaaa 100644 --- a/rules.neon +++ b/rules.neon @@ -4,66 +4,247 @@ parameters: checkAlwaysTrueCheckTypeFunctionCall: true checkAlwaysTrueInstanceof: true checkAlwaysTrueStrictComparison: true + checkAlwaysTrueLooseComparison: true + checkDynamicProperties: %featureToggles.bleedingEdge% checkExplicitMixedMissingReturn: true checkFunctionNameCase: true checkInternalClassCaseSensitivity: true reportMaybesInMethodSignatures: true reportStaticMethodSignatures: true reportMaybesInPropertyPhpDocTypes: true + featureToggles: + illegalConstructorMethodCall: %featureToggles.bleedingEdge% + strictRules: + allRules: true + disallowedLooseComparison: [%strictRules.allRules%, %featureToggles.bleedingEdge%] + booleansInConditions: %strictRules.allRules% + uselessCast: %strictRules.allRules% + requireParentConstructorCall: %strictRules.allRules% + disallowedConstructs: %strictRules.allRules% + overwriteVariablesWithLoop: %strictRules.allRules% + closureUsesThis: %strictRules.allRules% + matchingInheritedMethodNames: %strictRules.allRules% + numericOperandsInArithmeticOperators: %strictRules.allRules% + strictCalls: %strictRules.allRules% + switchConditionsMatchingType: %strictRules.allRules% + noVariableVariables: %strictRules.allRules% -rules: - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule - - PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule - - PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule - - PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule - - PHPStan\Rules\Classes\RequireParentConstructCallRule - - PHPStan\Rules\DisallowedConstructs\DisallowedBacktickRule - - PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule - - PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule - - PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule - - PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule - - PHPStan\Rules\ForLoop\OverwriteVariablesWithForLoopInitRule - - PHPStan\Rules\Functions\ClosureUsesThisRule - - PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule - - PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule - - PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule - - PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule - - PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule - - PHPStan\Rules\Operators\OperandsInArithmeticModuloRule - - PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule - - PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule - - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule - - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsCallableRule - - PHPStan\Rules\StrictCalls\StrictFunctionCallsRule - - PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule - - PHPStan\Rules\VariableVariables\VariableMethodCallRule - - PHPStan\Rules\VariableVariables\VariableMethodCallableRule - - PHPStan\Rules\VariableVariables\VariableStaticMethodCallRule - - PHPStan\Rules\VariableVariables\VariableStaticMethodCallableRule - - PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule - - PHPStan\Rules\VariableVariables\VariableVariablesRule +parametersSchema: + strictRules: structure([ + allRules: anyOf(bool(), arrayOf(bool())), + disallowedLooseComparison: anyOf(bool(), arrayOf(bool())), + booleansInConditions: anyOf(bool(), arrayOf(bool())) + uselessCast: anyOf(bool(), arrayOf(bool())) + requireParentConstructorCall: anyOf(bool(), arrayOf(bool())) + disallowedConstructs: anyOf(bool(), arrayOf(bool())) + overwriteVariablesWithLoop: anyOf(bool(), arrayOf(bool())) + closureUsesThis: anyOf(bool(), arrayOf(bool())) + matchingInheritedMethodNames: anyOf(bool(), arrayOf(bool())) + numericOperandsInArithmeticOperators: anyOf(bool(), arrayOf(bool())) + strictCalls: anyOf(bool(), arrayOf(bool())) + switchConditionsMatchingType: anyOf(bool(), arrayOf(bool())) + noVariableVariables: anyOf(bool(), arrayOf(bool())) + ]) + +conditionalTags: + PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule: + phpstan.rules.rule: %strictRules.disallowedLooseComparison% + PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule: + phpstan.rules.rule: %strictRules.booleansInConditions% + PHPStan\Rules\Cast\UselessCastRule: + phpstan.rules.rule: %strictRules.uselessCast% + PHPStan\Rules\Classes\RequireParentConstructCallRule: + phpstan.rules.rule: %strictRules.requireParentConstructorCall% + PHPStan\Rules\DisallowedConstructs\DisallowedBacktickRule: + phpstan.rules.rule: %strictRules.disallowedConstructs% + PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule: + phpstan.rules.rule: %strictRules.disallowedConstructs% + PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule: + phpstan.rules.rule: %strictRules.disallowedConstructs% + PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule: + phpstan.rules.rule: %strictRules.disallowedConstructs% + PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule: + phpstan.rules.rule: %strictRules.overwriteVariablesWithLoop% + PHPStan\Rules\ForLoop\OverwriteVariablesWithForLoopInitRule: + phpstan.rules.rule: %strictRules.overwriteVariablesWithLoop% + PHPStan\Rules\Functions\ClosureUsesThisRule: + phpstan.rules.rule: %strictRules.closureUsesThis% + PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule: + phpstan.rules.rule: %strictRules.matchingInheritedMethodNames% + PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticModuloRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule: + phpstan.rules.rule: %strictRules.numericOperandsInArithmeticOperators% + PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule: + phpstan.rules.rule: %strictRules.strictCalls% + PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsCallableRule: + phpstan.rules.rule: %strictRules.strictCalls% + PHPStan\Rules\StrictCalls\StrictFunctionCallsRule: + phpstan.rules.rule: %strictRules.strictCalls% + PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule: + phpstan.rules.rule: %strictRules.switchConditionsMatchingType% + PHPStan\Rules\VariableVariables\VariableMethodCallRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariableMethodCallableRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariableStaticMethodCallRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariableStaticMethodCallableRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariableVariablesRule: + phpstan.rules.rule: %strictRules.noVariableVariables% + PHPStan\Rules\VariableVariables\VariablePropertyFetchRule: + phpstan.rules.rule: %strictRules.noVariableVariables% services: - class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper + - + class: PHPStan\Rules\Operators\OperatorRuleHelper + + - + class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule + arguments: + universalObjectCratesClasses: %universalObjectCratesClasses% + + - + class: PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule + - class: PHPStan\Rules\Cast\UselessCastRule arguments: treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% - tags: - - phpstan.rules.rule - - class: PHPStan\Rules\Operators\OperatorRuleHelper + class: PHPStan\Rules\Classes\RequireParentConstructCallRule + - - class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule - arguments: - universalObjectCratesClasses: %universalObjectCratesClasses% - tags: - - phpstan.rules.rule + class: PHPStan\Rules\DisallowedConstructs\DisallowedBacktickRule + + - + class: PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule + + - + class: PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule + + - + class: PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule + + - + class: PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule + + - + class: PHPStan\Rules\ForLoop\OverwriteVariablesWithForLoopInitRule + + - + class: PHPStan\Rules\Functions\ClosureUsesThisRule + + - + class: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule + + - + class: PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule + + - + class: PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule + + - + class: PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule + + - + class: PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticModuloRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule + + - + class: PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule + + - + class: PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule + + - + class: PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsCallableRule + + - + class: PHPStan\Rules\StrictCalls\StrictFunctionCallsRule + + - + class: PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule + + - + class: PHPStan\Rules\VariableVariables\VariableMethodCallRule + + - + class: PHPStan\Rules\VariableVariables\VariableMethodCallableRule + + - + class: PHPStan\Rules\VariableVariables\VariableStaticMethodCallRule + + - + class: PHPStan\Rules\VariableVariables\VariableStaticMethodCallableRule + + - + class: PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule + + - + class: PHPStan\Rules\VariableVariables\VariableVariablesRule diff --git a/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php b/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php index 93f7ee63..53140ccf 100644 --- a/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php +++ b/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php @@ -2,13 +2,17 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PHPStan\Analyser\Scope; use PHPStan\Node\BooleanAndNode; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; /** - * @implements \PHPStan\Rules\Rule + * @implements Rule */ -class BooleanInBooleanAndRule implements \PHPStan\Rules\Rule +class BooleanInBooleanAndRule implements Rule { /** @var BooleanRuleHelper */ @@ -24,7 +28,7 @@ public function getNodeType(): string return BooleanAndNode::class; } - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $originalNode = $node->getOriginalNode(); $messages = []; diff --git a/src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php b/src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php index 7a492e82..0e53c873 100644 --- a/src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php +++ b/src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php @@ -2,9 +2,14 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PhpParser\Node\Expr\BooleanNot; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class BooleanInBooleanNotRule implements \PHPStan\Rules\Rule +class BooleanInBooleanNotRule implements Rule { /** @var BooleanRuleHelper */ @@ -17,15 +22,14 @@ public function __construct(BooleanRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BooleanNot::class; + return BooleanNot::class; } /** - * @param \PhpParser\Node\Expr\BooleanNot $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanNot $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { if ($this->helper->passesAsBoolean($scope, $node->expr)) { return []; diff --git a/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php b/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php index a95738b0..bf98c5cd 100644 --- a/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php +++ b/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php @@ -2,13 +2,17 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PHPStan\Analyser\Scope; use PHPStan\Node\BooleanOrNode; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; /** - * @implements \PHPStan\Rules\Rule + * @implements Rule */ -class BooleanInBooleanOrRule implements \PHPStan\Rules\Rule +class BooleanInBooleanOrRule implements Rule { /** @var BooleanRuleHelper */ @@ -24,7 +28,7 @@ public function getNodeType(): string return BooleanOrNode::class; } - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $originalNode = $node->getOriginalNode(); $messages = []; diff --git a/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php b/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php index 0553f31c..8dfe3dac 100644 --- a/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php +++ b/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php @@ -2,9 +2,14 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PhpParser\Node\Stmt\ElseIf_; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class BooleanInElseIfConditionRule implements \PHPStan\Rules\Rule +class BooleanInElseIfConditionRule implements Rule { /** @var BooleanRuleHelper */ @@ -17,15 +22,14 @@ public function __construct(BooleanRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Stmt\ElseIf_::class; + return ElseIf_::class; } /** - * @param \PhpParser\Node\Stmt\ElseIf_ $node - * @param \PHPStan\Analyser\Scope $scope + * @param ElseIf_ $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { if ($this->helper->passesAsBoolean($scope, $node->cond)) { return []; diff --git a/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php b/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php index a77cad92..7b1cbfb8 100644 --- a/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php +++ b/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php @@ -2,9 +2,14 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PhpParser\Node\Stmt\If_; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class BooleanInIfConditionRule implements \PHPStan\Rules\Rule +class BooleanInIfConditionRule implements Rule { /** @var BooleanRuleHelper */ @@ -17,15 +22,14 @@ public function __construct(BooleanRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Stmt\If_::class; + return If_::class; } /** - * @param \PhpParser\Node\Stmt\If_ $node - * @param \PHPStan\Analyser\Scope $scope + * @param If_ $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { if ($this->helper->passesAsBoolean($scope, $node->cond)) { return []; diff --git a/src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php b/src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php index 08e4f838..5d389b7a 100644 --- a/src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php +++ b/src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php @@ -2,9 +2,14 @@ namespace PHPStan\Rules\BooleansInConditions; +use PhpParser\Node; +use PhpParser\Node\Expr\Ternary; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class BooleanInTernaryOperatorRule implements \PHPStan\Rules\Rule +class BooleanInTernaryOperatorRule implements Rule { /** @var BooleanRuleHelper */ @@ -17,15 +22,14 @@ public function __construct(BooleanRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\Ternary::class; + return Ternary::class; } /** - * @param \PhpParser\Node\Expr\Ternary $node - * @param \PHPStan\Analyser\Scope $scope + * @param Ternary $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { if ($node->if === null) { return []; // elvis ?: diff --git a/src/Rules/BooleansInConditions/BooleanRuleHelper.php b/src/Rules/BooleansInConditions/BooleanRuleHelper.php index 98457542..9b167588 100644 --- a/src/Rules/BooleansInConditions/BooleanRuleHelper.php +++ b/src/Rules/BooleansInConditions/BooleanRuleHelper.php @@ -13,7 +13,7 @@ class BooleanRuleHelper { - /** @var \PHPStan\Rules\RuleLevelHelper */ + /** @var RuleLevelHelper */ private $ruleLevelHelper; public function __construct(RuleLevelHelper $ruleLevelHelper) @@ -31,7 +31,7 @@ public function passesAsBoolean(Scope $scope, Expr $expr): bool $scope, $expr, '', - function (Type $type): bool { + static function (Type $type): bool { return $type instanceof BooleanType; } ); diff --git a/src/Rules/Cast/UselessCastRule.php b/src/Rules/Cast/UselessCastRule.php index 1766c1c2..70f762f6 100644 --- a/src/Rules/Cast/UselessCastRule.php +++ b/src/Rules/Cast/UselessCastRule.php @@ -5,14 +5,16 @@ use PhpParser\Node; use PhpParser\Node\Expr\Cast; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\ErrorType; use PHPStan\Type\GeneralizePrecision; use PHPStan\Type\TypeUtils; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class UselessCastRule implements \PHPStan\Rules\Rule +class UselessCastRule implements Rule { /** @var bool */ @@ -29,8 +31,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\Cast $node - * @param \PHPStan\Analyser\Scope $scope + * @param Cast $node * @return RuleError[] errors */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/Classes/RequireParentConstructCallRule.php b/src/Rules/Classes/RequireParentConstructCallRule.php index d3420ad4..c07e04e4 100644 --- a/src/Rules/Classes/RequireParentConstructCallRule.php +++ b/src/Rules/Classes/RequireParentConstructCallRule.php @@ -3,11 +3,18 @@ namespace PHPStan\Rules\Classes; use PhpParser\Node; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Name; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; - -class RequireParentConstructCallRule implements \PHPStan\Rules\Rule +use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass; +use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum; +use PHPStan\Rules\Rule; +use PHPStan\ShouldNotHappenException; +use function property_exists; +use function sprintf; + +class RequireParentConstructCallRule implements Rule { public function getNodeType(): string @@ -16,14 +23,13 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Stmt\ClassMethod $node - * @param \PHPStan\Analyser\Scope $scope + * @param ClassMethod $node * @return string[] */ public function processNode(Node $node, Scope $scope): array { if (!$scope->isInClass()) { - throw new \PHPStan\ShouldNotHappenException(); + throw new ShouldNotHappenException(); } if ($scope->isInTrait()) { @@ -34,6 +40,10 @@ public function processNode(Node $node, Scope $scope): array return []; } + if ($node->isAbstract()) { + return []; + } + $classReflection = $scope->getClassReflection()->getNativeReflection(); if ($classReflection->isInterface() || $classReflection->isAnonymous()) { return []; @@ -75,7 +85,7 @@ public function processNode(Node $node, Scope $scope): array private function callsParentConstruct(Node $parserNode): bool { - if (!isset($parserNode->stmts)) { + if (!property_exists($parserNode, 'stmts')) { return false; } @@ -85,7 +95,7 @@ private function callsParentConstruct(Node $parserNode): bool } $statement = $this->ignoreErrorSuppression($statement); - if ($statement instanceof \PhpParser\Node\Expr\StaticCall) { + if ($statement instanceof StaticCall) { if ( $statement->class instanceof Name && ((string) $statement->class === 'parent') @@ -105,10 +115,10 @@ private function callsParentConstruct(Node $parserNode): bool } /** - * @param \ReflectionClass $classReflection - * @return \ReflectionClass|false + * @param ReflectionClass|ReflectionEnum $classReflection + * @return ReflectionClass|false */ - private function getParentConstructorClass(\ReflectionClass $classReflection) + private function getParentConstructorClass($classReflection) { while ($classReflection->getParentClass() !== false) { $constructor = $classReflection->getParentClass()->hasMethod('__construct') ? $classReflection->getParentClass()->getMethod('__construct') : null; diff --git a/src/Rules/DisallowedConstructs/DisallowedBacktickRule.php b/src/Rules/DisallowedConstructs/DisallowedBacktickRule.php index e90dfc9c..aab3824d 100644 --- a/src/Rules/DisallowedConstructs/DisallowedBacktickRule.php +++ b/src/Rules/DisallowedConstructs/DisallowedBacktickRule.php @@ -3,19 +3,21 @@ namespace PHPStan\Rules\DisallowedConstructs; use PhpParser\Node; +use PhpParser\Node\Expr\Empty_; +use PhpParser\Node\Expr\ShellExec; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; -class DisallowedBacktickRule implements \PHPStan\Rules\Rule +class DisallowedBacktickRule implements Rule { public function getNodeType(): string { - return \PhpParser\Node\Expr\ShellExec::class; + return ShellExec::class; } /** - * @param \PhpParser\Node\Expr\Empty_ $node - * @param \PHPStan\Analyser\Scope $scope + * @param Empty_ $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/DisallowedConstructs/DisallowedEmptyRule.php b/src/Rules/DisallowedConstructs/DisallowedEmptyRule.php index 547d5ecb..9a867cd5 100644 --- a/src/Rules/DisallowedConstructs/DisallowedEmptyRule.php +++ b/src/Rules/DisallowedConstructs/DisallowedEmptyRule.php @@ -3,19 +3,20 @@ namespace PHPStan\Rules\DisallowedConstructs; use PhpParser\Node; +use PhpParser\Node\Expr\Empty_; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; -class DisallowedEmptyRule implements \PHPStan\Rules\Rule +class DisallowedEmptyRule implements Rule { public function getNodeType(): string { - return \PhpParser\Node\Expr\Empty_::class; + return Empty_::class; } /** - * @param \PhpParser\Node\Expr\Empty_ $node - * @param \PHPStan\Analyser\Scope $scope + * @param Empty_ $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php b/src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php index 4aa7c1c4..322d0dac 100644 --- a/src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php +++ b/src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php @@ -2,33 +2,39 @@ namespace PHPStan\Rules\DisallowedConstructs; +use PhpParser\Node; +use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\Variable; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; +use function is_string; +use function sprintf; -class DisallowedImplicitArrayCreationRule implements \PHPStan\Rules\Rule +class DisallowedImplicitArrayCreationRule implements Rule { public function getNodeType(): string { - return \PhpParser\Node\Expr\Assign::class; + return Assign::class; } /** - * @param \PhpParser\Node\Expr\Assign $node - * @param \PHPStan\Analyser\Scope $scope + * @param Assign $node * @return string[] */ - public function processNode(\PhpParser\Node $node, Scope $scope): array + public function processNode(Node $node, Scope $scope): array { - if (!$node->var instanceof \PhpParser\Node\Expr\ArrayDimFetch) { + if (!$node->var instanceof ArrayDimFetch) { return []; } $node = $node->var; - while ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) { + while ($node instanceof ArrayDimFetch) { $node = $node->var; } - if (!$node instanceof \PhpParser\Node\Expr\Variable) { + if (!$node instanceof Variable) { return []; } diff --git a/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php b/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php new file mode 100644 index 00000000..a1425c76 --- /dev/null +++ b/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php @@ -0,0 +1,44 @@ + + */ +class DisallowedLooseComparisonRule implements Rule +{ + + public function getNodeType(): string + { + return BinaryOp::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($node instanceof Equal) { + return [ + RuleErrorBuilder::message( + 'Loose comparison via "==" is not allowed.' + )->tip('Use strict comparison via "===" instead.')->build(), + ]; + } + if ($node instanceof NotEqual) { + return [ + RuleErrorBuilder::message( + 'Loose comparison via "!=" is not allowed.' + )->tip('Use strict comparison via "!==" instead.')->build(), + ]; + } + + return []; + } + +} diff --git a/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php b/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php index a42e7cb8..59db24ac 100644 --- a/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php +++ b/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php @@ -3,17 +3,19 @@ namespace PHPStan\Rules\DisallowedConstructs; use PhpParser\Node; +use PhpParser\Node\Expr\Ternary; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; /** - * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\Ternary> + * @implements Rule */ -class DisallowedShortTernaryRule implements \PHPStan\Rules\Rule +class DisallowedShortTernaryRule implements Rule { public function getNodeType(): string { - return \PhpParser\Node\Expr\Ternary::class; + return Ternary::class; } public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php b/src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php index 286cab20..ca8f631c 100644 --- a/src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php +++ b/src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php @@ -8,6 +8,8 @@ use PhpParser\Node\Stmt\For_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use function is_string; +use function sprintf; /** * @implements Rule @@ -22,7 +24,6 @@ public function getNodeType(): string /** * @param For_ $node - * @param Scope $scope * @return string[] */ public function processNode(Node $node, Scope $scope): array @@ -42,8 +43,6 @@ public function processNode(Node $node, Scope $scope): array } /** - * @param Scope $scope - * @param Expr $expr * @return string[] */ private function checkValueVar(Scope $scope, Expr $expr): array diff --git a/src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php b/src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php index 2bb5ccb6..dbf16d25 100644 --- a/src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php +++ b/src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php @@ -4,8 +4,11 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Stmt\Foreach_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use function is_string; +use function sprintf; class OverwriteVariablesWithForeachRule implements Rule { @@ -16,8 +19,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Stmt\Foreach_ $node - * @param \PHPStan\Analyser\Scope $scope + * @param Foreach_ $node * @return string[] */ public function processNode(Node $node, Scope $scope): array @@ -39,8 +41,6 @@ public function processNode(Node $node, Scope $scope): array } /** - * @param Scope $scope - * @param Expr $expr * @return string[] */ private function checkValueVar(Scope $scope, Expr $expr): array diff --git a/src/Rules/Functions/ClosureUsesThisRule.php b/src/Rules/Functions/ClosureUsesThisRule.php index 60018f45..fbce62f9 100644 --- a/src/Rules/Functions/ClosureUsesThisRule.php +++ b/src/Rules/Functions/ClosureUsesThisRule.php @@ -7,6 +7,8 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\ThisType; +use function is_string; +use function sprintf; /** * @implements Rule @@ -25,6 +27,10 @@ public function processNode(Node $node, Scope $scope): array return []; } + if ($scope->isInClosureBind()) { + return []; + } + $messages = []; foreach ($node->uses as $closureUse) { $varType = $scope->getType($closureUse->var); diff --git a/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php b/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php index 76310a78..be62e245 100644 --- a/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php +++ b/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php @@ -2,12 +2,17 @@ namespace PHPStan\Rules\Methods; +use PhpParser\Node; use PHPStan\Analyser\Scope; use PHPStan\Node\InClassMethodNode; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\MethodReflection; +use PHPStan\Rules\Rule; +use function sprintf; -class WrongCaseOfInheritedMethodRule implements \PHPStan\Rules\Rule +/** + * @implements Rule + */ +class WrongCaseOfInheritedMethodRule implements Rule { public function getNodeType(): string @@ -15,21 +20,12 @@ public function getNodeType(): string return InClassMethodNode::class; } - /** - * @param \PhpParser\Node\Stmt\ClassMethod $node - * @param \PHPStan\Analyser\Scope $scope - * @return string[] - */ public function processNode( - \PhpParser\Node $node, + Node $node, Scope $scope ): array { - $methodReflection = $scope->getFunction(); - if (!$methodReflection instanceof MethodReflection) { - return []; - } - + $methodReflection = $node->getMethodReflection(); $declaringClass = $methodReflection->getDeclaringClass(); $messages = []; diff --git a/src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php b/src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php index 2822d19c..c11b6eaa 100644 --- a/src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php +++ b/src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php @@ -2,8 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\PostDec; +use PhpParser\Node\Expr\PostInc; +use PhpParser\Node\Expr\PreDec; +use PhpParser\Node\Expr\PreInc; +use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; abstract class OperandInArithmeticIncrementOrDecrementRule implements Rule { @@ -17,11 +24,10 @@ public function __construct(OperatorRuleHelper $helper) } /** - * @param \PhpParser\Node\Expr\PreInc|\PhpParser\Node\Expr\PreDec|\PhpParser\Node\Expr\PostInc|\PhpParser\Node\Expr\PostDec $node - * @param \PHPStan\Analyser\Scope $scope + * @param PreInc|PreDec|PostInc|PostDec $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $varType = $scope->getType($node->var); diff --git a/src/Rules/Operators/OperandInArithmeticPostDecrementRule.php b/src/Rules/Operators/OperandInArithmeticPostDecrementRule.php index 1bdaf989..d96763a0 100644 --- a/src/Rules/Operators/OperandInArithmeticPostDecrementRule.php +++ b/src/Rules/Operators/OperandInArithmeticPostDecrementRule.php @@ -2,12 +2,14 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node\Expr\PostDec; + class OperandInArithmeticPostDecrementRule extends OperandInArithmeticIncrementOrDecrementRule { public function getNodeType(): string { - return \PhpParser\Node\Expr\PostDec::class; + return PostDec::class; } protected function describeOperation(): string diff --git a/src/Rules/Operators/OperandInArithmeticPostIncrementRule.php b/src/Rules/Operators/OperandInArithmeticPostIncrementRule.php index d1fe3f30..a4e15eec 100644 --- a/src/Rules/Operators/OperandInArithmeticPostIncrementRule.php +++ b/src/Rules/Operators/OperandInArithmeticPostIncrementRule.php @@ -2,12 +2,14 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node\Expr\PostInc; + class OperandInArithmeticPostIncrementRule extends OperandInArithmeticIncrementOrDecrementRule { public function getNodeType(): string { - return \PhpParser\Node\Expr\PostInc::class; + return PostInc::class; } protected function describeOperation(): string diff --git a/src/Rules/Operators/OperandInArithmeticPreDecrementRule.php b/src/Rules/Operators/OperandInArithmeticPreDecrementRule.php index 74f67efb..e5da2efa 100644 --- a/src/Rules/Operators/OperandInArithmeticPreDecrementRule.php +++ b/src/Rules/Operators/OperandInArithmeticPreDecrementRule.php @@ -2,12 +2,14 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node\Expr\PreDec; + class OperandInArithmeticPreDecrementRule extends OperandInArithmeticIncrementOrDecrementRule { public function getNodeType(): string { - return \PhpParser\Node\Expr\PreDec::class; + return PreDec::class; } protected function describeOperation(): string diff --git a/src/Rules/Operators/OperandInArithmeticPreIncrementRule.php b/src/Rules/Operators/OperandInArithmeticPreIncrementRule.php index dbb2cfa1..32fb2474 100644 --- a/src/Rules/Operators/OperandInArithmeticPreIncrementRule.php +++ b/src/Rules/Operators/OperandInArithmeticPreIncrementRule.php @@ -2,12 +2,14 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node\Expr\PreInc; + class OperandInArithmeticPreIncrementRule extends OperandInArithmeticIncrementOrDecrementRule { public function getNodeType(): string { - return \PhpParser\Node\Expr\PreInc::class; + return PreInc::class; } protected function describeOperation(): string diff --git a/src/Rules/Operators/OperandsInArithmeticAdditionRule.php b/src/Rules/Operators/OperandsInArithmeticAdditionRule.php index e5ae9977..47323ffc 100644 --- a/src/Rules/Operators/OperandsInArithmeticAdditionRule.php +++ b/src/Rules/Operators/OperandsInArithmeticAdditionRule.php @@ -2,11 +2,16 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Plus; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function count; +use function sprintf; -class OperandsInArithmeticAdditionRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticAdditionRule implements Rule { /** @var OperatorRuleHelper */ @@ -19,21 +24,18 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Plus::class; + return Plus::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + if (count($leftType->getArrays()) > 0 && count($rightType->getArrays()) > 0) { return []; } diff --git a/src/Rules/Operators/OperandsInArithmeticDivisionRule.php b/src/Rules/Operators/OperandsInArithmeticDivisionRule.php index 7c1a9fa7..43656eaa 100644 --- a/src/Rules/Operators/OperandsInArithmeticDivisionRule.php +++ b/src/Rules/Operators/OperandsInArithmeticDivisionRule.php @@ -2,9 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Div; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class OperandsInArithmeticDivisionRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticDivisionRule implements Rule { /** @var OperatorRuleHelper */ @@ -17,15 +23,14 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Div::class; + return Div::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $leftType = $scope->getType($node->left); diff --git a/src/Rules/Operators/OperandsInArithmeticExponentiationRule.php b/src/Rules/Operators/OperandsInArithmeticExponentiationRule.php index 65ea2b41..59e55f14 100644 --- a/src/Rules/Operators/OperandsInArithmeticExponentiationRule.php +++ b/src/Rules/Operators/OperandsInArithmeticExponentiationRule.php @@ -2,9 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Pow; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class OperandsInArithmeticExponentiationRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticExponentiationRule implements Rule { /** @var OperatorRuleHelper */ @@ -17,15 +23,14 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Pow::class; + return Pow::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $leftType = $scope->getType($node->left); diff --git a/src/Rules/Operators/OperandsInArithmeticModuloRule.php b/src/Rules/Operators/OperandsInArithmeticModuloRule.php index 8f17353c..b89c8515 100644 --- a/src/Rules/Operators/OperandsInArithmeticModuloRule.php +++ b/src/Rules/Operators/OperandsInArithmeticModuloRule.php @@ -2,9 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Mod; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class OperandsInArithmeticModuloRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticModuloRule implements Rule { /** @var OperatorRuleHelper */ @@ -17,15 +23,14 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Mod::class; + return Mod::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $leftType = $scope->getType($node->left); diff --git a/src/Rules/Operators/OperandsInArithmeticMultiplicationRule.php b/src/Rules/Operators/OperandsInArithmeticMultiplicationRule.php index 46b0120b..c1e16ae8 100644 --- a/src/Rules/Operators/OperandsInArithmeticMultiplicationRule.php +++ b/src/Rules/Operators/OperandsInArithmeticMultiplicationRule.php @@ -2,9 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Mul; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class OperandsInArithmeticMultiplicationRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticMultiplicationRule implements Rule { /** @var OperatorRuleHelper */ @@ -17,15 +23,14 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Mul::class; + return Mul::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $leftType = $scope->getType($node->left); diff --git a/src/Rules/Operators/OperandsInArithmeticSubtractionRule.php b/src/Rules/Operators/OperandsInArithmeticSubtractionRule.php index 093876db..d5907ae5 100644 --- a/src/Rules/Operators/OperandsInArithmeticSubtractionRule.php +++ b/src/Rules/Operators/OperandsInArithmeticSubtractionRule.php @@ -2,9 +2,15 @@ namespace PHPStan\Rules\Operators; +use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\Minus; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; -class OperandsInArithmeticSubtractionRule implements \PHPStan\Rules\Rule +class OperandsInArithmeticSubtractionRule implements Rule { /** @var OperatorRuleHelper */ @@ -17,15 +23,14 @@ public function __construct(OperatorRuleHelper $helper) public function getNodeType(): string { - return \PhpParser\Node\Expr\BinaryOp\Minus::class; + return Minus::class; } /** - * @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node - * @param \PHPStan\Analyser\Scope $scope + * @param BooleanAnd $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $leftType = $scope->getType($node->left); diff --git a/src/Rules/Operators/OperatorRuleHelper.php b/src/Rules/Operators/OperatorRuleHelper.php index be071571..ca7b2349 100644 --- a/src/Rules/Operators/OperatorRuleHelper.php +++ b/src/Rules/Operators/OperatorRuleHelper.php @@ -6,6 +6,7 @@ use PHPStan\Analyser\Scope; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\Accessory\AccessoryNumericStringType; +use PHPStan\Type\BenevolentUnionType; use PHPStan\Type\ErrorType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; @@ -18,7 +19,7 @@ class OperatorRuleHelper { - /** @var \PHPStan\Rules\RuleLevelHelper */ + /** @var RuleLevelHelper */ private $ruleLevelHelper; public function __construct(RuleLevelHelper $ruleLevelHelper) @@ -59,7 +60,7 @@ private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool $scope, $expr, '', - function (Type $type) use ($acceptedType): bool { + static function (Type $type) use ($acceptedType): bool { return $acceptedType->isSuperTypeOf($type)->yes(); } )->getType(); @@ -69,7 +70,7 @@ function (Type $type) use ($acceptedType): bool { } $isSuperType = $acceptedType->isSuperTypeOf($type); - if ($type instanceof \PHPStan\Type\BenevolentUnionType) { + if ($type instanceof BenevolentUnionType) { return !$isSuperType->no(); } diff --git a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRule.php b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRule.php index 6aefe622..95e27fec 100644 --- a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRule.php +++ b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRule.php @@ -5,17 +5,19 @@ use PhpParser\Node; use PHPStan\Analyser\Scope; use PHPStan\Node\MethodCallableNode; +use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\ErrorType; use PHPStan\Type\Type; +use function sprintf; /** - * @implements \PHPStan\Rules\Rule + * @implements Rule */ -class DynamicCallOnStaticMethodsCallableRule implements \PHPStan\Rules\Rule +class DynamicCallOnStaticMethodsCallableRule implements Rule { - /** @var \PHPStan\Rules\RuleLevelHelper */ + /** @var RuleLevelHelper */ private $ruleLevelHelper; public function __construct(RuleLevelHelper $ruleLevelHelper) @@ -39,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array $scope, $node->getVar(), '', - function (Type $type) use ($name): bool { + static function (Type $type) use ($name): bool { return $type->canCallMethods()->yes() && $type->hasMethod($name)->yes(); } )->getType(); diff --git a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php index 1ae402a1..c412cb2b 100644 --- a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php +++ b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php @@ -5,14 +5,16 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\ErrorType; use PHPStan\Type\Type; +use function sprintf; -class DynamicCallOnStaticMethodsRule implements \PHPStan\Rules\Rule +class DynamicCallOnStaticMethodsRule implements Rule { - /** @var \PHPStan\Rules\RuleLevelHelper */ + /** @var RuleLevelHelper */ private $ruleLevelHelper; public function __construct(RuleLevelHelper $ruleLevelHelper) @@ -26,8 +28,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\MethodCall $node - * @param \PHPStan\Analyser\Scope $scope + * @param MethodCall $node * @return string[] */ public function processNode(Node $node, Scope $scope): array @@ -41,7 +42,7 @@ public function processNode(Node $node, Scope $scope): array $scope, $node->var, '', - function (Type $type) use ($name): bool { + static function (Type $type) use ($name): bool { return $type->canCallMethods()->yes() && $type->hasMethod($name)->yes(); } )->getType(); diff --git a/src/Rules/StrictCalls/StrictFunctionCallsRule.php b/src/Rules/StrictCalls/StrictFunctionCallsRule.php index 1aeb900d..9a50e548 100644 --- a/src/Rules/StrictCalls/StrictFunctionCallsRule.php +++ b/src/Rules/StrictCalls/StrictFunctionCallsRule.php @@ -2,9 +2,18 @@ namespace PHPStan\Rules\StrictCalls; +use PhpParser\Node; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Name; +use PHPStan\Analyser\Scope; +use PHPStan\Broker\Broker; +use PHPStan\Rules\Rule; use PHPStan\Type\Constant\ConstantBooleanType; +use function array_key_exists; +use function sprintf; +use function strtolower; -class StrictFunctionCallsRule implements \PHPStan\Rules\Rule +class StrictFunctionCallsRule implements Rule { /** @var int[] */ @@ -15,27 +24,26 @@ class StrictFunctionCallsRule implements \PHPStan\Rules\Rule 'array_keys' => 2, ]; - /** @var \PHPStan\Broker\Broker */ + /** @var Broker */ private $broker; - public function __construct(\PHPStan\Broker\Broker $broker) + public function __construct(Broker $broker) { $this->broker = $broker; } public function getNodeType(): string { - return \PhpParser\Node\Expr\FuncCall::class; + return FuncCall::class; } /** - * @param \PhpParser\Node\Expr\FuncCall $node - * @param \PHPStan\Analyser\Scope $scope + * @param FuncCall $node * @return string[] errors */ - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { - if (!$node->name instanceof \PhpParser\Node\Name) { + if (!$node->name instanceof Name) { return []; } diff --git a/src/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRule.php b/src/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRule.php index cdd61e82..bb4bebbf 100644 --- a/src/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRule.php +++ b/src/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRule.php @@ -2,29 +2,35 @@ namespace PHPStan\Rules\SwitchConditions; +use PhpParser\Node; +use PhpParser\Node\Stmt\Switch_; +use PhpParser\PrettyPrinter\Standard; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; +use function sprintf; /** - * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Switch_> + * @implements Rule */ -class MatchingTypeInSwitchCaseConditionRule implements \PHPStan\Rules\Rule +class MatchingTypeInSwitchCaseConditionRule implements Rule { - /** @var \PhpParser\PrettyPrinter\Standard */ + /** @var Standard */ private $printer; - public function __construct(\PhpParser\PrettyPrinter\Standard $printer) + public function __construct(Standard $printer) { $this->printer = $printer; } public function getNodeType(): string { - return \PhpParser\Node\Stmt\Switch_::class; + return Switch_::class; } - public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array + public function processNode(Node $node, Scope $scope): array { $messages = []; $conditionType = $scope->getType($node->cond); diff --git a/src/Rules/VariableVariables/VariableMethodCallRule.php b/src/Rules/VariableVariables/VariableMethodCallRule.php index 8733f15f..4221cfa8 100644 --- a/src/Rules/VariableVariables/VariableMethodCallRule.php +++ b/src/Rules/VariableVariables/VariableMethodCallRule.php @@ -3,9 +3,11 @@ namespace PHPStan\Rules\VariableVariables; use PhpParser\Node; +use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; class VariableMethodCallRule implements Rule { @@ -16,8 +18,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\MethodCall $node - * @param \PHPStan\Analyser\Scope $scope + * @param MethodCall $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/VariableVariables/VariableMethodCallableRule.php b/src/Rules/VariableVariables/VariableMethodCallableRule.php index 12e03563..f293b562 100644 --- a/src/Rules/VariableVariables/VariableMethodCallableRule.php +++ b/src/Rules/VariableVariables/VariableMethodCallableRule.php @@ -7,6 +7,7 @@ use PHPStan\Node\MethodCallableNode; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; /** * @implements Rule diff --git a/src/Rules/VariableVariables/VariablePropertyFetchRule.php b/src/Rules/VariableVariables/VariablePropertyFetchRule.php index d872c6a5..3195c16f 100644 --- a/src/Rules/VariableVariables/VariablePropertyFetchRule.php +++ b/src/Rules/VariableVariables/VariablePropertyFetchRule.php @@ -3,12 +3,14 @@ namespace PHPStan\Rules\VariableVariables; use PhpParser\Node; +use PhpParser\Node\Expr\PropertyFetch; use PHPStan\Analyser\Scope; use PHPStan\Broker\Broker; use PHPStan\Reflection\ClassReflection; use PHPStan\Rules\Rule; use PHPStan\Type\TypeUtils; use PHPStan\Type\VerbosityLevel; +use function sprintf; class VariablePropertyFetchRule implements Rule { @@ -20,7 +22,6 @@ class VariablePropertyFetchRule implements Rule private $universalObjectCratesClasses; /** - * @param Broker $broker * @param string[] $universalObjectCratesClasses */ public function __construct(Broker $broker, array $universalObjectCratesClasses) @@ -35,8 +36,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\PropertyFetch $node - * @param \PHPStan\Analyser\Scope $scope + * @param PropertyFetch $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/VariableVariables/VariableStaticMethodCallRule.php b/src/Rules/VariableVariables/VariableStaticMethodCallRule.php index 5887a4df..0d6b8202 100644 --- a/src/Rules/VariableVariables/VariableStaticMethodCallRule.php +++ b/src/Rules/VariableVariables/VariableStaticMethodCallRule.php @@ -3,9 +3,11 @@ namespace PHPStan\Rules\VariableVariables; use PhpParser\Node; +use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; class VariableStaticMethodCallRule implements Rule { @@ -16,8 +18,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\StaticCall $node - * @param \PHPStan\Analyser\Scope $scope + * @param StaticCall $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/VariableVariables/VariableStaticMethodCallableRule.php b/src/Rules/VariableVariables/VariableStaticMethodCallableRule.php index 5ea5afe4..8ede1f00 100644 --- a/src/Rules/VariableVariables/VariableStaticMethodCallableRule.php +++ b/src/Rules/VariableVariables/VariableStaticMethodCallableRule.php @@ -7,6 +7,7 @@ use PHPStan\Node\StaticMethodCallableNode; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; /** * @implements Rule diff --git a/src/Rules/VariableVariables/VariableStaticPropertyFetchRule.php b/src/Rules/VariableVariables/VariableStaticPropertyFetchRule.php index 7e457f93..4b410a7f 100644 --- a/src/Rules/VariableVariables/VariableStaticPropertyFetchRule.php +++ b/src/Rules/VariableVariables/VariableStaticPropertyFetchRule.php @@ -3,9 +3,11 @@ namespace PHPStan\Rules\VariableVariables; use PhpParser\Node; +use PhpParser\Node\Expr\StaticPropertyFetch; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; use PHPStan\Type\VerbosityLevel; +use function sprintf; class VariableStaticPropertyFetchRule implements Rule { @@ -16,8 +18,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\StaticPropertyFetch $node - * @param \PHPStan\Analyser\Scope $scope + * @param StaticPropertyFetch $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/src/Rules/VariableVariables/VariableVariablesRule.php b/src/Rules/VariableVariables/VariableVariablesRule.php index b250742b..9768f21b 100644 --- a/src/Rules/VariableVariables/VariableVariablesRule.php +++ b/src/Rules/VariableVariables/VariableVariablesRule.php @@ -3,8 +3,10 @@ namespace PHPStan\Rules\VariableVariables; use PhpParser\Node; +use PhpParser\Node\Expr\Variable; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use function is_string; class VariableVariablesRule implements Rule { @@ -15,8 +17,7 @@ public function getNodeType(): string } /** - * @param \PhpParser\Node\Expr\Variable $node - * @param \PHPStan\Analyser\Scope $scope + * @param Variable $node * @return string[] */ public function processNode(Node $node, Scope $scope): array diff --git a/tests/Levels/LevelsIntegrationTest.php b/tests/Levels/LevelsIntegrationTest.php index e492ae8d..d1e57d7e 100644 --- a/tests/Levels/LevelsIntegrationTest.php +++ b/tests/Levels/LevelsIntegrationTest.php @@ -2,7 +2,9 @@ namespace PHPStan\Levels; -class LevelsIntegrationTest extends \PHPStan\Testing\LevelsTestCase +use PHPStan\Testing\LevelsTestCase; + +class LevelsIntegrationTest extends LevelsTestCase { /** diff --git a/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php index 0c7a6f58..c9244204 100644 --- a/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php @@ -4,11 +4,12 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends RuleTestCase */ -class BooleanInBooleanAndRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInBooleanAndRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/BooleansInConditions/BooleanInBooleanNotRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInBooleanNotRuleTest.php index eb887e7f..33dd205c 100644 --- a/tests/Rules/BooleansInConditions/BooleanInBooleanNotRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInBooleanNotRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class BooleanInBooleanNotRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInBooleanNotRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php index bfecfd1f..1a4fafb0 100644 --- a/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php @@ -4,11 +4,12 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends RuleTestCase */ -class BooleanInBooleanOrRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInBooleanOrRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/BooleansInConditions/BooleanInElseIfConditionRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInElseIfConditionRuleTest.php index 880ca900..b2df79fc 100644 --- a/tests/Rules/BooleansInConditions/BooleanInElseIfConditionRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInElseIfConditionRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class BooleanInElseIfConditionRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInElseIfConditionRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/BooleansInConditions/BooleanInIfConditionRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInIfConditionRuleTest.php index 1922a0d5..196af111 100644 --- a/tests/Rules/BooleansInConditions/BooleanInIfConditionRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInIfConditionRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class BooleanInIfConditionRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInIfConditionRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/BooleansInConditions/BooleanInTernaryOperatorRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInTernaryOperatorRuleTest.php index 17c3fdd7..76bd4ad0 100644 --- a/tests/Rules/BooleansInConditions/BooleanInTernaryOperatorRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInTernaryOperatorRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class BooleanInTernaryOperatorRuleTest extends \PHPStan\Testing\RuleTestCase +class BooleanInTernaryOperatorRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Cast/UselessCastRuleTest.php b/tests/Rules/Cast/UselessCastRuleTest.php index 9d8bcf0a..c1affdde 100644 --- a/tests/Rules/Cast/UselessCastRuleTest.php +++ b/tests/Rules/Cast/UselessCastRuleTest.php @@ -2,13 +2,16 @@ namespace PHPStan\Rules\Cast; -class UselessCastRuleTest extends \PHPStan\Testing\RuleTestCase +use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; + +class UselessCastRuleTest extends RuleTestCase { /** @var bool */ private $treatPhpDocTypesAsCertain; - protected function getRule(): \PHPStan\Rules\Rule + protected function getRule(): Rule { return new UselessCastRule($this->treatPhpDocTypesAsCertain); } @@ -44,6 +47,7 @@ public function testUselessCast(): void [ 'Casting to string something that\'s already string.', 46, + 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.', ], ] ); diff --git a/tests/Rules/Classes/RequireParentConstructCallRuleTest.php b/tests/Rules/Classes/RequireParentConstructCallRuleTest.php index a390d998..caeee5c8 100644 --- a/tests/Rules/Classes/RequireParentConstructCallRuleTest.php +++ b/tests/Rules/Classes/RequireParentConstructCallRuleTest.php @@ -2,12 +2,14 @@ namespace PHPStan\Rules\Classes; +use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; use const PHP_VERSION_ID; -class RequireParentConstructCallRuleTest extends \PHPStan\Testing\RuleTestCase +class RequireParentConstructCallRuleTest extends RuleTestCase { - protected function getRule(): \PHPStan\Rules\Rule + protected function getRule(): Rule { return new RequireParentConstructCallRule(); } diff --git a/tests/Rules/DisallowedConstructs/DisallowedBacktickRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedBacktickRuleTest.php index 996f2c6e..1b697e0a 100644 --- a/tests/Rules/DisallowedConstructs/DisallowedBacktickRuleTest.php +++ b/tests/Rules/DisallowedConstructs/DisallowedBacktickRuleTest.php @@ -3,8 +3,9 @@ namespace PHPStan\Rules\DisallowedConstructs; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; -class DisallowedBacktickRuleTest extends \PHPStan\Testing\RuleTestCase +class DisallowedBacktickRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/DisallowedConstructs/DisallowedEmptyRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedEmptyRuleTest.php index 5b9e8188..5ffc7f67 100644 --- a/tests/Rules/DisallowedConstructs/DisallowedEmptyRuleTest.php +++ b/tests/Rules/DisallowedConstructs/DisallowedEmptyRuleTest.php @@ -3,8 +3,9 @@ namespace PHPStan\Rules\DisallowedConstructs; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; -class DisallowedEmptyRuleTest extends \PHPStan\Testing\RuleTestCase +class DisallowedEmptyRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRuleTest.php index 7b446a82..6092a5f4 100644 --- a/tests/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRuleTest.php +++ b/tests/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRuleTest.php @@ -3,8 +3,9 @@ namespace PHPStan\Rules\DisallowedConstructs; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; -class DisallowedImplicitArrayCreationRuleTest extends \PHPStan\Testing\RuleTestCase +class DisallowedImplicitArrayCreationRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php new file mode 100644 index 00000000..9e56b583 --- /dev/null +++ b/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php @@ -0,0 +1,35 @@ + + */ +class DisallowedLooseComparisonRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new DisallowedLooseComparisonRule(); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/weak-comparison.php'], [ + [ + 'Loose comparison via "==" is not allowed.', + 3, + 'Use strict comparison via "===" instead.', + ], + [ + 'Loose comparison via "!=" is not allowed.', + 5, + 'Use strict comparison via "!==" instead.', + ], + ]); + } + +} diff --git a/tests/Rules/DisallowedConstructs/DisallowedShortTernaryRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedShortTernaryRuleTest.php index 8d143c3b..3f3b944a 100644 --- a/tests/Rules/DisallowedConstructs/DisallowedShortTernaryRuleTest.php +++ b/tests/Rules/DisallowedConstructs/DisallowedShortTernaryRuleTest.php @@ -3,11 +3,12 @@ namespace PHPStan\Rules\DisallowedConstructs; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends RuleTestCase */ -class DisallowedShortTernaryRuleTest extends \PHPStan\Testing\RuleTestCase +class DisallowedShortTernaryRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/DisallowedConstructs/data/weak-comparison.php b/tests/Rules/DisallowedConstructs/data/weak-comparison.php new file mode 100644 index 00000000..2e7e3a97 --- /dev/null +++ b/tests/Rules/DisallowedConstructs/data/weak-comparison.php @@ -0,0 +1,6 @@ + */ abstract protected function getExpectedErrors(): array; diff --git a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php index f163e537..95b92b39 100644 --- a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php @@ -4,9 +4,10 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; use const PHP_VERSION_ID; -class OperandsInArithmeticAdditionRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticAdditionRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php index ece89c07..4e2d3055 100644 --- a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class OperandsInArithmeticDivisionRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticDivisionRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php index dc7395c7..53b8d1b8 100644 --- a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class OperandsInArithmeticExponentiationRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticExponentiationRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php index cbf12e7e..f8490466 100644 --- a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class OperandsInArithmeticModuloRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticModuloRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php index 5d7ac620..c4b6f25b 100644 --- a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class OperandsInArithmeticMultiplicationRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticMultiplicationRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php index f62e0a28..f7643fd5 100644 --- a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class OperandsInArithmeticSubtractionRuleTest extends \PHPStan\Testing\RuleTestCase +class OperandsInArithmeticSubtractionRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/Operators/data/increment-decrement.php b/tests/Rules/Operators/data/increment-decrement.php index f4539f19..bdb97c5f 100644 --- a/tests/Rules/Operators/data/increment-decrement.php +++ b/tests/Rules/Operators/data/increment-decrement.php @@ -58,3 +58,12 @@ ++$mixed; ++$union; })(); + + +function (): void { + for ($i = 5; $i < 4; $i++) { + } + + for ($y = 0; $y < 0; $y++) { + } +}; diff --git a/tests/Rules/Operators/data/operators.php b/tests/Rules/Operators/data/operators.php index ab132c83..92ec5930 100644 --- a/tests/Rules/Operators/data/operators.php +++ b/tests/Rules/Operators/data/operators.php @@ -113,3 +113,7 @@ function (array $array, int $int, $mixed) { /** @var numeric-string $numericString */ $numericString = doFoo(); $numericString + 1; + +function (\ReflectionClass $ref): void { + print_r(class_parents($ref->getName()) + class_implements($ref->getName())); +}; diff --git a/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRuleTest.php b/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRuleTest.php index e570adcf..53be1727 100644 --- a/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRuleTest.php +++ b/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsCallableRuleTest.php @@ -4,11 +4,13 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; +use const PHP_VERSION_ID; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends RuleTestCase */ -class DynamicCallOnStaticMethodsCallableRuleTest extends \PHPStan\Testing\RuleTestCase +class DynamicCallOnStaticMethodsCallableRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsRuleTest.php b/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsRuleTest.php index f9b0a93b..a35919a2 100644 --- a/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsRuleTest.php +++ b/tests/Rules/StrictCalls/DynamicCallOnStaticMethodsRuleTest.php @@ -4,8 +4,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Testing\RuleTestCase; -class DynamicCallOnStaticMethodsRuleTest extends \PHPStan\Testing\RuleTestCase +class DynamicCallOnStaticMethodsRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php index 1a7d0f35..b1d34ba8 100644 --- a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php +++ b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php @@ -3,8 +3,9 @@ namespace PHPStan\Rules\StrictCalls; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; -class StrictFunctionCallsRuleTest extends \PHPStan\Testing\RuleTestCase +class StrictFunctionCallsRuleTest extends RuleTestCase { protected function getRule(): Rule diff --git a/tests/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRuleTest.php b/tests/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRuleTest.php index 40b61d0e..553cba5e 100644 --- a/tests/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRuleTest.php +++ b/tests/Rules/SwitchConditions/MatchingTypeInSwitchCaseConditionRuleTest.php @@ -2,17 +2,19 @@ namespace PHPStan\Rules\SwitchConditions; +use PhpParser\PrettyPrinter\Standard; use PHPStan\Rules\Rule; +use PHPStan\Testing\RuleTestCase; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends RuleTestCase */ -class MatchingTypeInSwitchCaseConditionRuleTest extends \PHPStan\Testing\RuleTestCase +class MatchingTypeInSwitchCaseConditionRuleTest extends RuleTestCase { protected function getRule(): Rule { - return new MatchingTypeInSwitchCaseConditionRule(new \PhpParser\PrettyPrinter\Standard()); + return new MatchingTypeInSwitchCaseConditionRule(new Standard()); } public function testRule(): void diff --git a/tests/Rules/VariableVariables/VariableMethodCallableRuleTest.php b/tests/Rules/VariableVariables/VariableMethodCallableRuleTest.php index b1654ce3..8fc1e418 100644 --- a/tests/Rules/VariableVariables/VariableMethodCallableRuleTest.php +++ b/tests/Rules/VariableVariables/VariableMethodCallableRuleTest.php @@ -4,6 +4,7 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; +use const PHP_VERSION_ID; /** * @extends RuleTestCase