diff --git a/.github/workflows/build_scoped_rector.yaml b/.github/workflows/build_scoped_rector.yaml index 4ab301f7432..bbf3d9a734c 100644 --- a/.github/workflows/build_scoped_rector.yaml +++ b/.github/workflows/build_scoped_rector.yaml @@ -66,11 +66,11 @@ jobs: # 3. prefix classes - run: sh build/build-rector-scoped.sh rector-build rector-prefixed-downgraded - # 4. lint the code for PHP 7.2 - this must happen here, as setup-php allows only one PHP version switch: https://github.com/shivammathur/setup-php/issues/434 + # 4. lint the code for PHP 7.4 - this must happen here, as setup-php allows only one PHP version switch: https://github.com/shivammathur/setup-php/issues/434 - uses: shivammathur/setup-php@v2 with: - php-version: 7.2 + php-version: 7.4 coverage: none - run: composer global require php-parallel-lint/php-parallel-lint --ansi - run: /home/runner/.composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/console/Debug/CliRequest.php diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 88fc7a2448d..512e422cf7c 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -27,7 +27,7 @@ jobs: - name: 'PHPStan' - run: vendor/bin/phpstan analyse --ansi --error-format symplify + run: vendor/bin/phpstan analyse --ansi - name: 'Commented Code' diff --git a/.github/workflows/packages_tests.yaml b/.github/workflows/packages_tests.yaml index 10b45db2e83..1387530bb2b 100644 --- a/.github/workflows/packages_tests.yaml +++ b/.github/workflows/packages_tests.yaml @@ -50,10 +50,19 @@ jobs: - run: composer config minimum-stability dev - # test with current commit in a pull-request - - - run: composer require rector/rector-src dev-main#${{github.event.pull_request.head.sha}} --no-update - if: ${{ github.event_name == 'pull_request' }} + # temporary for PR 6431 as rector/rector-* packages already require this PR branch + # so avoid pointed to rector-src main branch composer.json + - name: Conditionally to not require rector/rector-src on PR 6431 + if: ${{ github.event_name == 'pull_request' }} + run: | + if [ "${{ github.event.pull_request.number }}" != "6431" ]; then + composer require rector/rector-src dev-main#${{ github.event.pull_request.head.sha }} --no-update + fi + +# # test with current commit in a pull-request +# - +# run: composer require rector/rector-src dev-main#${{github.event.pull_request.head.sha}} --no-update +# if: ${{ github.event_name == 'pull_request' }} - run: composer install --ansi diff --git a/README.md b/README.md index ec253381e3e..eba5f8e6325 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Head to [`rectorphp/rector`](http://github.com/rectorphp/rector) for documentati ## Building `rectorphp/rector` -Code of this repository requires PHP 8. For `rector/rector` package the build downgrades code to PHP 7.2+. +Code of this repository requires PHP 8. For `rector/rector` package the build downgrades code to PHP 7.4+.
diff --git a/build/build-preload.php b/build/build-preload.php index 0e2cef9139e..d12919768a9 100755 --- a/build/build-preload.php +++ b/build/build-preload.php @@ -76,6 +76,7 @@ final class PreloadBuilder 'TokenEmulator.php', 'KeywordEmulator.php', 'Comment.php', + 'PrettyPrinter.php', 'PrettyPrinterAbstract.php', 'Parser.php', 'ParserAbstract.php', @@ -104,6 +105,21 @@ final class PreloadBuilder 'TypeNode.php', ]; + /** + * The classes are deprecated and moved under Node + * + * @var string[] + */ + private const IN_USE_CLASS_FILES = [ + 'Node/Expr/ArrayItem.php', + 'Node/Expr/ClosureUse.php', + 'Node/Scalar/EncapsedStringPart.php', + 'Node/Scalar/LNumber.php', + 'Node/Stmt/DeclareDeclare.php', + 'Node/Stmt/PropertyProperty.php', + 'Node/Stmt/StaticVar.php', + ]; + public function buildPreloadScript(string $buildDirectory, string $preloadFile): void { $this->buildPreloadScriptPhpParser($buildDirectory, $preloadFile); @@ -315,6 +331,17 @@ private function findPhpParserFilesAndSortThem(string $vendorDir): array // 2. put first-class usages first $fileInfos = $this->sortFileInfos($fileInfos); + foreach ($fileInfos as $key => $fileInfo) { + foreach (self::IN_USE_CLASS_FILES as $inUseClassFile) { + if (str_ends_with($fileInfo->getPathname(), $inUseClassFile)) { + unset($fileInfos[$key]); + continue 2; + } + } + } + + $fileInfos = array_values($fileInfos); + $stmtsAwareInterface = new SplFileInfo(__DIR__ . '/../src/Contract/PhpParser/Node/StmtsAwareInterface.php'); array_splice($fileInfos, 1, 0, [$stmtsAwareInterface]); diff --git a/build/config/config-downgrade.php b/build/config/config-downgrade.php index 6351c05971a..d82be6a89d2 100644 --- a/build/config/config-downgrade.php +++ b/build/config/config-downgrade.php @@ -9,7 +9,7 @@ return RectorConfig::configure() ->withSkip(DowngradeRectorConfig::DEPENDENCY_EXCLUDE_PATHS) ->withPHPStanConfigs([__DIR__ . '/phpstan-for-downgrade.neon']) - ->withDowngradeSets(php72: true); + ->withDowngradeSets(php74: true); /** * Configuration consts for the different rector.php config files diff --git a/build/config/phpstan-for-downgrade.neon b/build/config/phpstan-for-downgrade.neon index b3c3d93f88f..5868ff3f660 100644 --- a/build/config/phpstan-for-downgrade.neon +++ b/build/config/phpstan-for-downgrade.neon @@ -5,7 +5,3 @@ parameters: scanDirectories: # this is needed for symfony/dependendency-injection as it has hidden dependency on symfony/expression-language that we don't use here - ../../stubs - - # see https://github.com/rectorphp/rector/issues/3490#issue-634342324 - featureToggles: - disableRuntimeReflectionProvider: true diff --git a/build/target-repository/.github/workflows/along_other_packages.yaml b/build/target-repository/.github/workflows/along_other_packages.yaml index 4a291ca3c95..d2a88426d3e 100644 --- a/build/target-repository/.github/workflows/along_other_packages.yaml +++ b/build/target-repository/.github/workflows/along_other_packages.yaml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - php_version: ['7.2', '7.3', '7.4', '8.0', '8.1'] + php_version: ['7.4', '8.0', '8.1'] commands: - name: 'Composer Dependency' diff --git a/build/target-repository/.github/workflows/e2e.yaml b/build/target-repository/.github/workflows/e2e.yaml index 8da797bd0d3..c09aa86b2f8 100644 --- a/build/target-repository/.github/workflows/e2e.yaml +++ b/build/target-repository/.github/workflows/e2e.yaml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - php_version: ['7.2', '7.3', '7.4', '8.0', '8.1'] + php_version: ['7.4', '8.0', '8.1'] directory: - 'e2e/define-constant' - 'e2e/dont-execute-code' diff --git a/build/target-repository/.github/workflows/e2e_diff.yaml b/build/target-repository/.github/workflows/e2e_diff.yaml index 4f98627d235..2bf284f0be9 100644 --- a/build/target-repository/.github/workflows/e2e_diff.yaml +++ b/build/target-repository/.github/workflows/e2e_diff.yaml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - php_version: ['7.2', '7.3', '7.4', '8.0', '8.1'] + php_version: ['7.4', '8.0', '8.1'] directory: - 'e2e/attributes' diff --git a/build/target-repository/.github/workflows/e2e_global.yaml b/build/target-repository/.github/workflows/e2e_global.yaml index f50f7c9c3e5..8e7c55f9471 100644 --- a/build/target-repository/.github/workflows/e2e_global.yaml +++ b/build/target-repository/.github/workflows/e2e_global.yaml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php_version: ['7.2', '7.3', '7.4', '8.0', '8.1'] + php_version: ['7.4', '8.0', '8.1'] directory: - 'e2e/global-install' diff --git a/build/target-repository/.github/workflows/e2e_php72.yaml b/build/target-repository/.github/workflows/e2e_php72.yaml deleted file mode 100644 index 2e1420c0b6a..00000000000 --- a/build/target-repository/.github/workflows/e2e_php72.yaml +++ /dev/null @@ -1,31 +0,0 @@ -name: End to End tests on PHP 7.2 - -on: - push: - branches: - - main - -jobs: - end_to_end_on_php72: - runs-on: ubuntu-latest - - name: End to end test - PHP 7.2 with load ReflectionUnionType stub - - steps: - - uses: actions/checkout@v4 - - - uses: shivammathur/setup-php@v2 - with: - php-version: "7.2" - coverage: none - - # wait for deploy to packagist - - run: sleep 40 - - - - run: composer require rector/rector:dev-main --ansi - working-directory: e2e/reflection-union-php72 - - - - run: vendor/bin/rector process --ansi - working-directory: e2e/reflection-union-php72 diff --git a/build/target-repository/.github/workflows/standalone_rule_test.yaml b/build/target-repository/.github/workflows/standalone_rule_test.yaml index 6435cf6d0fb..edcefd315d0 100644 --- a/build/target-repository/.github/workflows/standalone_rule_test.yaml +++ b/build/target-repository/.github/workflows/standalone_rule_test.yaml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php_version: ['7.3'] + php_version: ['7.4'] directory: - 'e2e/rector-prefixed-rule-test' diff --git a/build/target-repository/composer.json b/build/target-repository/composer.json index e34e112c82a..16857cf6ca2 100644 --- a/build/target-repository/composer.json +++ b/build/target-repository/composer.json @@ -7,8 +7,8 @@ "bin/rector" ], "require": { - "php": "^7.2|^8.0", - "phpstan/phpstan": "^1.12.5" + "php": "^7.4|^8.0", + "phpstan/phpstan": "^2.0.1" }, "autoload": { "files": [ diff --git a/build/target-repository/e2e/reflection-union-php72/composer.json b/build/target-repository/e2e/reflection-union-php72/composer.json deleted file mode 100644 index 17cdb632c2f..00000000000 --- a/build/target-repository/e2e/reflection-union-php72/composer.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "autoload": { - "psr-4": { - "App\\": "src/" - } - }, - "require": { - "php": "7.2.*", - "rector/rector": "dev-main" - } -} diff --git a/build/target-repository/e2e/reflection-union-php72/rector.php b/build/target-repository/e2e/reflection-union-php72/rector.php deleted file mode 100644 index ba64f6a2083..00000000000 --- a/build/target-repository/e2e/reflection-union-php72/rector.php +++ /dev/null @@ -1,17 +0,0 @@ -paths([ - __DIR__ . '/src' - ]); - - $rectorConfig->skip([ - __DIR__ . '/vendor', - ]); - - $rectorConfig->sets([\Rector\Set\ValueObject\SetList::CODE_QUALITY]); -}; diff --git a/build/target-repository/e2e/reflection-union-php72/src/foo.php b/build/target-repository/e2e/reflection-union-php72/src/foo.php deleted file mode 100644 index 844a77709f1..00000000000 --- a/build/target-repository/e2e/reflection-union-php72/src/foo.php +++ /dev/null @@ -1,14 +0,0 @@ -format('C'); - } -} diff --git a/composer.json b/composer.json index bb95bf2265a..ffe96401870 100644 --- a/composer.json +++ b/composer.json @@ -20,11 +20,11 @@ "doctrine/inflector": "^2.0.10", "illuminate/container": "^11.25", "nette/utils": "^4.0", - "nikic/php-parser": "^4.19.4", + "nikic/php-parser": "^5.3.1", "ocramius/package-versions": "^2.9", "ondram/ci-detector": "^4.2", - "phpstan/phpdoc-parser": "^1.32", - "phpstan/phpstan": "^1.12.5", + "phpstan/phpdoc-parser": "^2.0", + "phpstan/phpstan": "^2.0.1", "react/event-loop": "^1.5", "react/promise": "^3.2", "react/socket": "^1.15", @@ -45,21 +45,17 @@ "require-dev": { "php-parallel-lint/php-parallel-lint": "^1.4", "phpstan/extension-installer": "^1.4", - "phpstan/phpstan-deprecation-rules": "^1.2", - "phpstan/phpstan-phpunit": "^1.4", - "phpstan/phpstan-webmozart-assert": "^1.2.6", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpstan/phpstan-webmozart-assert": "^2.0", "phpunit/phpunit": "^10.5", "rector/release-notes-generator": "^0.3.0", "rector/swiss-knife": "^0.2.16", - "rector/type-perfect": "^1.0", "shipmonk/composer-dependency-analyser": "^1.8", "symplify/easy-coding-standard": "^12.3", - "symplify/phpstan-extensions": "^11.4", - "symplify/phpstan-rules": "^13.0", "symplify/rule-doc-generator": "^12.2.5", "symplify/vendor-patches": "^11.3", "tomasvotruba/class-leak": "^1.1.2", - "tomasvotruba/unused-public": "^1.0", "tracy/tracy": "^2.9" }, "replace": { @@ -111,7 +107,7 @@ ], "check-cs": "vendor/bin/ecs check --ansi", "fix-cs": "vendor/bin/ecs check --fix --ansi", - "phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify --memory-limit=512M", + "phpstan": "vendor/bin/phpstan analyse --ansi --memory-limit=512M", "docs": "vendor/bin/rule-doc-generator validate rules", "rector": "bin/rector process --ansi", "preload": "php build/build-preload.php .", @@ -126,22 +122,22 @@ "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/illuminate-container-container-php.patch" ], "nikic/php-parser": [ - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-expr-closure-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-finally-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-function-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-do-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-catch-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-trycatch-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-for-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-classmethod-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-else-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-while-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-foreach-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-if-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-case-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-namespace-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-nodetraverser-php.patch" + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-expr-closure-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-finally-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-function-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-do-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-catch-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-trycatch-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-for-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-classmethod-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-else-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-while-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-foreach-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-if-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-case-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-node-stmt-namespace-php.patch", + "https://raw.githubusercontent.com/rectorphp/vendor-patches/php-parser-5-latest/patches/nikic-php-parser-lib-phpparser-nodetraverser-php.patch" ] }, "composer-exit-on-patch-failure": true, diff --git a/config/phpstan/parser.neon b/config/phpstan/parser.neon index 623e87feb66..3b0f385dce2 100644 --- a/config/phpstan/parser.neon +++ b/config/phpstan/parser.neon @@ -24,5 +24,4 @@ services: class: PHPStan\Parser\RichParser arguments: parser: @currentPhpVersionPhpParser - lexer: @currentPhpVersionLexer autowired: no diff --git a/config/phpstan/static-reflection.neon b/config/phpstan/static-reflection.neon index a54673113a7..238529fc630 100644 --- a/config/phpstan/static-reflection.neon +++ b/config/phpstan/static-reflection.neon @@ -1,9 +1,3 @@ -parameters: - # see https://github.com/rectorphp/rector/issues/3490#issue-634342324 - featureToggles: - disableRuntimeReflectionProvider: false - nodeConnectingVisitorCompatibility: false - services: - Rector\NodeTypeResolver\Reflection\BetterReflection\RectorBetterReflectionSourceLocatorFactory - Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator diff --git a/full_build.sh b/full_build.sh index 30322dba2cd..55543734e0c 100644 --- a/full_build.sh +++ b/full_build.sh @@ -2,7 +2,7 @@ # usage: # -# export PHP72_BIN_PATH=/opt/local/bin/php72 && sh ./full_build.sh +# export PHP74_BIN_PATH=/opt/local/bin/php74 && sh ./full_build.sh # see https://stackoverflow.com/questions/66644233/how-to-propagate-colors-from-bash-script-to-github-action?noredirect=1#comment117811853_66644233 export TERM=xterm-color @@ -37,14 +37,14 @@ php -d memory_limit=-1 bin/rector process rector-build/bin rector-build/config r sh build/build-rector-scoped.sh rector-build rector-prefixed-downgraded -# verify syntax valid in php 7.2 +# verify syntax valid in php 7.4 composer global require php-parallel-lint/php-parallel-lint -if test -z ${PHP72_BIN_PATH+y}; then +if test -z ${PHP74_BIN_PATH+y}; then ~/.config/composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/console/Debug/CliRequest.php else - echo "verify syntax valid in php 7.2 with specify PHP72_BIN_PATH env"; - $PHP72_BIN_PATH ~/.composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/console/Debug/CliRequest.php + echo "verify syntax valid in php 7.4 with specify PHP74_BIN_PATH env"; + $PHP74_BIN_PATH ~/.composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/console/Debug/CliRequest.php fi # rollback, done testing succeed diff --git a/phpstan.neon b/phpstan.neon index 78a8499851c..c01b25a7b8b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,7 @@ -includes: - - vendor/symplify/phpstan-rules/config/symplify-rules.neon - - vendor/symplify/phpstan-rules/config/rector-rules.neon +# to be enabled later once rector upgraded to use phpstan v2 +#includes: +# - vendor/symplify/phpstan-rules/config/symplify-rules.neon +# - vendor/symplify/phpstan-rules/config/rector-rules.neon parameters: level: 8 @@ -10,6 +11,8 @@ parameters: # requires exact closure types checkMissingCallableSignature: true + treatPhpDocTypesAsCertain: false + paths: - rector.php - bin @@ -34,16 +37,68 @@ parameters: - '*tests/*/Source*' # https://github.com/rectorphp/type-perfect/ - type_perfect: - no_mixed: true - null_over_false: true - narrow_param: true - narrow_return: true +# to be enabled later once rector upgraded to use phpstan v2 +# type_perfect: +# no_mixed: true +# null_over_false: true +# narrow_param: true +# narrow_return: true ignoreErrors: - identifier: missingType.generics + # phpstan class instance + - + identifier: phpstanApi.class + + # phpstan class constant value + - + identifier: phpstanApi.classConstant + + # phpstan instanceof + - + identifier: phpstanApi.instanceofAssumption + + # assert phpunit + - + identifier: method.alreadyNarrowedType + path: tests + + # runtime php id + - + identifier: greaterOrEqual.alwaysTrue + path: src/Util/FileHasher.php + + # unreliable on loop + - + identifier: instanceof.alwaysTrue + path: rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php + + - + identifier: phpstanApi.constructor + + # enforce native type + - + identifier: varTag.nativeType + + # on runtime check + - + identifier: deadCode.unreachable + paths: + - src/Console/Notifier.php + - src/Util/FileHasher.php + + - + identifier: argument.type + + - + identifier: phpstanApi.varTagAssumption + + - + identifier: assign.propertyType + path: src/PhpParser/NodeTraverser/RectorNodeTraverser.php + # is nested expr - message: '#Access to an undefined property PhpParser\\Node\\Expr\:\:\$expr#' @@ -307,3 +362,37 @@ parameters: # deprecated - '#Register "Rector\\Php80\\Rector\\FunctionLike\\MixedTypeRector" service to "php80\.php" config set#' + + - '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#' + + # runtime comparison + - '#Comparison operation ".*" between int<\d+, \d+> and \d+ is always true#' + + # from mapper interface + - '#mapToPhpParserNode\(\) never returns PhpParser\\.* so it can be removed from the return#' + + # from constant string + - '#Method Rector\\.*::providePolyfillPackage\(\) never returns#' + - '#Method Rector\\.*::filterPolyfillPackages\(\) should return array\<#' + + # realpath() usage + - + message: '#should return string\|null but returns string\|false#' + paths: + - src/Bootstrap/RectorConfigsResolver.php + - src/ValueObject/Error/SystemError.php + + - + message: '#should return array\ but returns list\#' + path: src/Set/SetManager.php + + - + message: '#should return non-empty-string but returns non-empty-string\|false#' + paths: + - src/Configuration/RectorConfigBuilder.php + - src/FileSystem/FilesFinder.php + + # collection of Type + - + message: '#Method Rector\\NodeTypeResolver\\PHPStan\\Type\\TypeFactory::uniquateTypes\(\) should return array\ but returns list\#' + path: src/NodeTypeResolver/PHPStan/Type/TypeFactory.php diff --git a/preload-split-package.php b/preload-split-package.php index 609c076611d..03dbe323638 100644 --- a/preload-split-package.php +++ b/preload-split-package.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/TokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Comment.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php'; @@ -56,28 +57,30 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Internal/DiffElem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Internal/Differ.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Internal/PrintableNewAnonClassNode.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Internal/TokenPolyfill.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/JsonDecoder.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/AsymmetricVisibilityTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/AttributeEmulator.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/EnumTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ExplicitOctalEmulator.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/FlexibleDocStringEmulator.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/MatchTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/NullsafeTokenEmulator.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/NumericLiteralSeparatorEmulator.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/PropertyTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReadonlyFunctionTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReverseEmulator.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Modifiers.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NameContext.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Arg.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/ArrayItem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Attribute.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/AttributeGroup.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/ClosureUse.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Const_.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/DeclareItem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrayDimFetch.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrayItem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Array_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrowFunction.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Assign.php'; @@ -135,7 +138,6 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ClassConstFetch.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Clone_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Closure.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ClosureUse.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ConstFetch.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Empty_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Error.php'; @@ -169,16 +171,20 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/YieldFrom.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Yield_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Identifier.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/InterpolatedStringPart.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/IntersectionType.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/MatchArm.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Name/FullyQualified.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Name/Relative.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/NullableType.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Param.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/PropertyHook.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/PropertyItem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/DNumber.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Encapsed.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/EncapsedStringPart.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/LNumber.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Float_.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Int_.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/InterpolatedString.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Class_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Dir.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/File.php'; @@ -186,8 +192,11 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Line.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Method.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Namespace_.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Property.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Trait_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/String_.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/StaticVar.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Block.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Break_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Case_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Catch_.php'; @@ -196,7 +205,6 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Class_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Const_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Continue_.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/DeclareDeclare.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Declare_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Do_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Echo_.php'; @@ -220,12 +228,9 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Namespace_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Nop.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Property.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/PropertyProperty.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Return_.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/StaticVar.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Static_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Switch_.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Throw_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUse.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdaptation/Alias.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdaptation/Precedence.php'; @@ -236,23 +241,26 @@ require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Use_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/While_.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/UnionType.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/UseItem.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/VarLikeIdentifier.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/VariadicPlaceholder.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeDumper.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeFinder.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CloningVisitor.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/FindingVisitor.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/FirstFindingVisitor.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NameResolver.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NodeConnectingVisitor.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser/Multiple.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser/Php5.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser/Php7.php'; -require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser/Tokens.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Parser/Php8.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/ParserFactory.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/PhpVersion.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter/Standard.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Token.php'; +require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/compatibility_tokens.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/Node.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/NodeVisitor.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Lexer/Lexer.php'; @@ -272,7 +280,6 @@ require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/ConstExprTrueNode.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/ConstFetchNode.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/DoctrineConstExprStringNode.php'; -require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/QuoteAwareConstExprStringNode.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/NodeTraverser.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/NodeVisitor/CloningVisitor.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/AssertTagMethodValueNode.php'; @@ -338,6 +345,7 @@ require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Parser/TypeParser.php'; +require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/ParserConfig.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Printer/DiffElem.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Printer/Differ.php'; require_once __DIR__ . '/../../../vendor/phpstan/phpdoc-parser/src/Printer/Printer.php'; diff --git a/preload.php b/preload.php index 5794a8c1c2d..3895d091c0e 100644 --- a/preload.php +++ b/preload.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/TokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Comment.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php'; @@ -56,28 +57,30 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Internal/DiffElem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Internal/Differ.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Internal/PrintableNewAnonClassNode.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenPolyfill.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/JsonDecoder.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/AsymmetricVisibilityTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/AttributeEmulator.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/EnumTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ExplicitOctalEmulator.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/FlexibleDocStringEmulator.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/MatchTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/NullsafeTokenEmulator.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/NumericLiteralSeparatorEmulator.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/PropertyTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReadonlyFunctionTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/ReverseEmulator.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Modifiers.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NameContext.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Arg.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/ArrayItem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Attribute.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/AttributeGroup.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/ClosureUse.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Const_.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/DeclareItem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrayDimFetch.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrayItem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Array_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrowFunction.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Assign.php'; @@ -135,7 +138,6 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ClassConstFetch.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Clone_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Closure.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ClosureUse.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ConstFetch.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Empty_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Error.php'; @@ -169,16 +171,20 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/YieldFrom.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Yield_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Identifier.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/InterpolatedStringPart.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/IntersectionType.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/MatchArm.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Name/FullyQualified.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Name/Relative.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/NullableType.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Param.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/PropertyHook.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/PropertyItem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/DNumber.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Encapsed.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/EncapsedStringPart.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/LNumber.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Float_.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Int_.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/InterpolatedString.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Class_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Dir.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/File.php'; @@ -186,8 +192,11 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Line.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Method.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Namespace_.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Property.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Trait_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/String_.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/StaticVar.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Block.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Break_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Case_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Catch_.php'; @@ -196,7 +205,6 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Class_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Const_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Continue_.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/DeclareDeclare.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Declare_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Do_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Echo_.php'; @@ -220,12 +228,9 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Namespace_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Nop.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Property.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/PropertyProperty.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Return_.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/StaticVar.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Static_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Switch_.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Throw_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUse.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdaptation/Alias.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdaptation/Precedence.php'; @@ -236,23 +241,26 @@ require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Use_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/While_.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/UnionType.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/UseItem.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/VarLikeIdentifier.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/VariadicPlaceholder.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeDumper.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeFinder.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CloningVisitor.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/FindingVisitor.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/FirstFindingVisitor.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NameResolver.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NodeConnectingVisitor.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Multiple.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Php5.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Php7.php'; -require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Tokens.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Php8.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/ParserFactory.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/PhpVersion.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter/Standard.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Token.php'; +require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/compatibility_tokens.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/Node.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/NodeVisitor.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Lexer/Lexer.php'; @@ -272,7 +280,6 @@ require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/ConstExprTrueNode.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/ConstFetchNode.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/DoctrineConstExprStringNode.php'; -require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/QuoteAwareConstExprStringNode.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/NodeTraverser.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/NodeVisitor/CloningVisitor.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/AssertTagMethodValueNode.php'; @@ -338,6 +345,7 @@ require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Parser/TypeParser.php'; +require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/ParserConfig.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Printer/DiffElem.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Printer/Differ.php'; require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Printer/Printer.php'; diff --git a/rules-tests/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector/Fixture/first_class_callable.php.inc b/rules-tests/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector/Fixture/first_class_callable.php.inc index a6ceec32fd0..372391c4510 100644 --- a/rules-tests/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector/Fixture/first_class_callable.php.inc +++ b/rules-tests/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector/Fixture/first_class_callable.php.inc @@ -20,7 +20,7 @@ class FirstClassCallable { public function run() { - $time = static fn() => \Carbon\Carbon::now()->timestamp; + $time = (static fn() => \Carbon\Carbon::now()->timestamp); } } diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc index 060b7831fb0..7ca52ea09e0 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc @@ -33,7 +33,7 @@ final class DoWhileMaybeReturned return 2; } while (++$i < 1); - return null; + return null; } } diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc index 018241763b0..9e4ed5c6aa9 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc @@ -33,7 +33,7 @@ final class DoWhileMaybeReturned2 return 2; } while (++$i < 1); - return null; + return null; } } diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned3.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned3.php.inc index 4cd74951b1b..6eba0ae8155 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned3.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned3.php.inc @@ -29,7 +29,7 @@ final class DoWhileMaybeReturned3 return 1; } } while (++$i < 1); - return null; + return null; } } diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned4.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned4.php.inc index 6f3a4af8eac..c5a124abbc9 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned4.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned4.php.inc @@ -37,7 +37,7 @@ final class DoWhileMaybeReturned4 } while (++$i < 1); execute: echo 'test'; - return null; + return null; } } diff --git a/rules-tests/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector/Fixture/fixture7.php.inc b/rules-tests/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector/Fixture/fixture7.php.inc index e02f93742d3..2fc21af9030 100644 --- a/rules-tests/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector/Fixture/fixture7.php.inc +++ b/rules-tests/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector/Fixture/fixture7.php.inc @@ -48,13 +48,12 @@ trait ValidatesAttributes public function function2($value, $secondValue) { - return !(($value === true) || ($secondValue === true)); + return !($value === true || $secondValue === true); } public function function3($attribute, $value, $parameters) { - return !($this->failsBasicDimensionChecks($parameters, $width, $height) || - $this->failsRatioCheck($parameters, $width, $height)); + return !($this->failsBasicDimensionChecks($parameters, $width, $height) || $this->failsRatioCheck($parameters, $width, $height)); } } diff --git a/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/and.php.inc b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/and.php.inc index a5f1da1d1b9..9b6fefec637 100644 --- a/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/and.php.inc +++ b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/and.php.inc @@ -12,7 +12,7 @@ if (false and true) { ----- diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/reference_to_inner_closure_unused.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/reference_to_inner_closure_unused.php.inc index 3883f9d5bac..f5e766391d3 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/reference_to_inner_closure_unused.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/reference_to_inner_closure_unused.php.inc @@ -26,7 +26,7 @@ class ReferenceToInnerClosureUnused { public function run($a) { - $y = fn() => fn() => 1; + $y = (fn() => fn() => 1); return $y; } diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_but_not_used.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_but_not_used.php.inc index 0dd4566b0b2..42c561f484f 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_but_not_used.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_but_not_used.php.inc @@ -22,7 +22,7 @@ class ReferencedButNotUsed { public function run() { - $callback = fn($b) => ++$b; + $callback = (fn($b) => ++$b); } } diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_inner_class.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_inner_class.php.inc index 1902262433a..e30d558c51d 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_inner_class.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/referenced_inner_class.php.inc @@ -25,10 +25,10 @@ class ReferencedInnerClass { public function run() { - $callback = fn($b) => new class { public function __construct($i) + $callback = (fn($b) => new class { public function __construct($i) { } - }; + }); } } diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/multiline_content.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/multiline_content.php.inc index 2aede27ecf2..85eb9d379d0 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/multiline_content.php.inc +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/multiline_content.php.inc @@ -22,8 +22,8 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation; #[GenericAnnotation(' - summary: Send webcam reward -')] + summary: Send webcam reward + ')] final class MultilineContent { } diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quote.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quote.php.inc index caeccf74c89..3ad19b1b7c1 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quote.php.inc +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quote.php.inc @@ -23,9 +23,9 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation; #[GenericAnnotation(' - summary: key - description: \'something `id => name`.\' -')] + summary: key + description: \'something `id => name`.\' + ')] final class NestedQuote { } diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quoted_asterisk.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quoted_asterisk.php.inc index 8dc53344c50..d8918e2fd88 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quoted_asterisk.php.inc +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_quoted_asterisk.php.inc @@ -25,11 +25,11 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation; #[GenericAnnotation(' - key: value - another_key: - another_value/*: - schema: 100 -')] + key: value + another_key: + another_value/*: + schema: 100 + ')] final class NestedQuote { } diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/sql_statement.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/sql_statement.php.inc index 5a2dabe3262..7dc06e396cd 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/sql_statement.php.inc +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/sql_statement.php.inc @@ -28,11 +28,11 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\FixturePh use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation; #[GenericAnnotation(filters: [new GenericAnnotation(some: ' - $this.id IN( - SELECT id - FROM foo - WHERE bar = \'baz\' - )')])] + $this.id IN( + SELECT id + FROM foo + WHERE bar = \'baz\' + )')])] class SqlStatement { } diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php index 99e6e40c127..6935f12ecb5 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php @@ -2,11 +2,12 @@ declare(strict_types=1); -use Doctrine\ORM\Mapping\Table; -use Doctrine\ORM\Mapping\UniqueConstraint; use Rector\Config\RectorConfig; use Rector\Php80\Rector\Class_\AnnotationToAttributeRector; +use Rector\Php80\Rector\Property\NestedAnnotationToAttributeRector; +use Rector\Php80\ValueObject\AnnotationPropertyToAttributeClass; use Rector\Php80\ValueObject\AnnotationToAttribute; +use Rector\Php80\ValueObject\NestedAnnotationToAttribute; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\SourcePhp81\All; use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\SourcePhp81\Length; @@ -22,9 +23,12 @@ new AnnotationToAttribute(Length::class), new AnnotationToAttribute(NotNumber::class), new AnnotationToAttribute(GenericAnnotation::class), + ]); - // PHP 8.1 doctrine annotations - new AnnotationToAttribute(Table::class), - new AnnotationToAttribute(UniqueConstraint::class), + $rectorConfig->ruleWithConfiguration(NestedAnnotationToAttributeRector::class, [ + new NestedAnnotationToAttribute('Doctrine\ORM\Mapping\Table', [ + new AnnotationPropertyToAttributeClass('Doctrine\ORM\Mapping\Index', 'indexes'), + new AnnotationPropertyToAttributeClass('Doctrine\ORM\Mapping\UniqueConstraint', 'uniqueConstraints'), + ]), ]); }; diff --git a/rules-tests/Renaming/Rector/Name/RenameClassRector/Fixture/some_rename_before_attribute.php.inc b/rules-tests/Renaming/Rector/Name/RenameClassRector/Fixture/some_rename_before_attribute.php.inc index c7a05e52842..ea26226509f 100644 --- a/rules-tests/Renaming/Rector/Name/RenameClassRector/Fixture/some_rename_before_attribute.php.inc +++ b/rules-tests/Renaming/Rector/Name/RenameClassRector/Fixture/some_rename_before_attribute.php.inc @@ -22,7 +22,6 @@ class SomeRenameBeforeAttribute namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture; -use Doctrine\DBAL\DBALException; use Symfony\Component\Routing\Annotation\Route; class SomeRenameBeforeAttribute diff --git a/rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/config/configured_rule.php b/rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/config/configured_rule.php index 17bc1c39d43..abf1a2551f2 100644 --- a/rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/config/configured_rule.php +++ b/rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/config/configured_rule.php @@ -2,11 +2,11 @@ declare(strict_types=1); +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\Config\RectorConfig; use Rector\Tests\Transform\Rector\Scalar\ScalarValueToConstFetchRector\Source\ClassWithConst; @@ -19,11 +19,11 @@ ScalarValueToConstFetchRector::class, [ new ScalarValueToConstFetch( - new LNumber(10), + new Int_(10), new ClassConstFetch(new FullyQualified(ClassWithConst::class), new Identifier('FOOBAR_INT')) ), new ScalarValueToConstFetch( - new DNumber(10.1), + new Float_(10.1), new ClassConstFetch(new FullyQualified(ClassWithConst::class), new Identifier('FOOBAR_FLOAT')) ), new ScalarValueToConstFetch( diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_type_modified_between_assign_and_return_by_ref.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_type_modified_between_assign_and_return_by_ref.php.inc deleted file mode 100644 index f09185311bc..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_type_modified_between_assign_and_return_by_ref.php.inc +++ /dev/null @@ -1,26 +0,0 @@ -verify($response); - - return $response; - } - - private function verify(SomeResponse &$someResponse): void - { - if (is_cli()) { - $someResponse = new CLIResponse(); - } - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector/Fixture/float_ternary.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector/Fixture/float_ternary.php.inc new file mode 100644 index 00000000000..fb0ba3f6485 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector/Fixture/float_ternary.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php b/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php index 442247f0192..69d4ce5c929 100644 --- a/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php +++ b/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php @@ -4,6 +4,7 @@ namespace Rector\Arguments\NodeAnalyzer; +use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Param; use PHPStan\Type\Type; @@ -31,7 +32,7 @@ public function isDefaultValueChanged(Param $param, mixed $value): bool public function isTypeChanged(Param $param, ?Type $newType): bool { - if ($param->type === null) { + if (!$param->type instanceof Node) { return false; } diff --git a/rules/Carbon/NodeFactory/CarbonCallFactory.php b/rules/Carbon/NodeFactory/CarbonCallFactory.php index 49e23596f41..a5dddba9428 100644 --- a/rules/Carbon/NodeFactory/CarbonCallFactory.php +++ b/rules/Carbon/NodeFactory/CarbonCallFactory.php @@ -4,13 +4,13 @@ namespace Rector\Carbon\NodeFactory; +use PhpParser\Node\Scalar\Int_; use Nette\Utils\Strings; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; final class CarbonCallFactory @@ -38,7 +38,7 @@ public function createFromDateTimeString( while ($match = Strings::match($string->value, self::PLUS_MINUS_COUNT_REGEX)) { $methodCall = $this->createModifyMethodCall( $carbonCall, - new LNumber((int) $match['count']), + new Int_((int) $match['count']), $match['unit'], $match['operator'] ); @@ -62,7 +62,7 @@ public function createFromDateTimeString( } // If we fallback to a parse we want to include tomorrow/today/yesterday etc - if ($currentCall->name instanceof Identifier && $currentCall->name->name != 'now') { + if ($currentCall->name instanceof Identifier && $currentCall->name->name !== 'now') { $rest .= ' ' . $currentCall->name->name; } @@ -85,7 +85,7 @@ private function createStaticCall(FullyQualified $carbonFullyQualified, String_ private function createModifyMethodCall( MethodCall|StaticCall $carbonCall, - LNumber $countLNumber, + Int_ $int, string $unit, string $operator ): ?MethodCall { @@ -112,7 +112,7 @@ private function createModifyMethodCall( $methodName = $operator . ucfirst($unit); - return new MethodCall($carbonCall, new Identifier($methodName), [new Arg($countLNumber)]); + return new MethodCall($carbonCall, new Identifier($methodName), [new Arg($int)]); } /** diff --git a/rules/CodeQuality/CompactConverter.php b/rules/CodeQuality/CompactConverter.php index 82f8393ce33..890a6a5c96f 100644 --- a/rules/CodeQuality/CompactConverter.php +++ b/rules/CodeQuality/CompactConverter.php @@ -6,7 +6,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; diff --git a/rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php b/rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php index 08e0f30713d..51e61b33850 100644 --- a/rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php @@ -4,6 +4,7 @@ namespace Rector\CodeQuality\NodeAnalyzer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; @@ -14,7 +15,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Type\MixedType; use PHPStan\Type\Type; @@ -54,7 +54,7 @@ public function resolveFetchedPropertiesToTypesFromClass(Class_ $class): array &$fetchedLocalPropertyNameToTypes ): ?int { if ($this->shouldSkip($node)) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof Assign && ($node->var instanceof PropertyFetch || $node->var instanceof ArrayDimFetch)) { @@ -65,7 +65,7 @@ public function resolveFetchedPropertiesToTypesFromClass(Class_ $class): array ); if ($propertyName === null) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($propertyFetch instanceof ArrayDimFetch) { @@ -73,11 +73,11 @@ public function resolveFetchedPropertiesToTypesFromClass(Class_ $class): array $propertyFetch, $node ); - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } $fetchedLocalPropertyNameToTypes[$propertyName][] = $this->nodeTypeResolver->getType($node->expr); - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } $propertyName = $this->resolvePropertyName($node); diff --git a/rules/CodeQuality/NodeFactory/MissingPropertiesFactory.php b/rules/CodeQuality/NodeFactory/MissingPropertiesFactory.php index 3b47f58bc8e..fb5885e0a82 100644 --- a/rules/CodeQuality/NodeFactory/MissingPropertiesFactory.php +++ b/rules/CodeQuality/NodeFactory/MissingPropertiesFactory.php @@ -4,9 +4,9 @@ namespace Rector\CodeQuality\NodeFactory; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use PHPStan\Type\Type; final readonly class MissingPropertiesFactory @@ -29,7 +29,7 @@ public function create(array $fetchedLocalPropertyNameToTypes, array $propertyNa continue; } - $property = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty($propertyName)]); + $property = new Property(Modifiers::PUBLIC, [new PropertyItem($propertyName)]); $this->propertyTypeDecorator->decorateProperty($property, $propertyType); $newProperties[] = $property; diff --git a/rules/CodeQuality/NodeFactory/TypedPropertyFactory.php b/rules/CodeQuality/NodeFactory/TypedPropertyFactory.php index 2f65f3e9e64..f67db05c242 100644 --- a/rules/CodeQuality/NodeFactory/TypedPropertyFactory.php +++ b/rules/CodeQuality/NodeFactory/TypedPropertyFactory.php @@ -4,6 +4,7 @@ namespace Rector\CodeQuality\NodeFactory; +use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\ComplexType; use PhpParser\Node\Identifier; @@ -11,7 +12,7 @@ use PhpParser\Node\NullableType; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -28,10 +29,10 @@ public function createFromPropertyTagValueNode( Class_ $class, string $propertyName ): Property { - $propertyProperty = new PropertyProperty($propertyName); + $propertyItem = new PropertyItem($propertyName); $propertyTypeNode = $this->createPropertyTypeNode($propertyTagValueNode, $class); - return new Property(Class_::MODIFIER_PRIVATE, [$propertyProperty], [], $propertyTypeNode); + return new Property(Modifiers::PRIVATE, [$propertyItem], [], $propertyTypeNode); } public function createPropertyTypeNode( diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index e300839e55e..b768b8271d4 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -4,16 +4,16 @@ namespace Rector\CodeQuality\Rector\Catch_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Catch_; -use PhpParser\Node\Stmt\Throw_; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; @@ -177,7 +177,7 @@ private function refactorThrow(Throw_ $throw, Variable $catchedThrowableVariable $new->setAttribute(AttributeKey::ORIGINAL_NODE, null); // nothing more to add - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } private function resolveExceptionArgumentPosition(Name $exceptionName): ?int @@ -200,11 +200,11 @@ private function resolveExceptionArgumentPosition(Name $exceptionName): ?int } $extendedMethodReflection = $classReflection->getConstructor(); - $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors( + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors( $extendedMethodReflection->getVariants() ); - foreach ($parametersAcceptorWithPhpDocs->getParameters() as $position => $parameterReflectionWithPhpDoc) { + foreach ($extendedParametersAcceptor->getParameters() as $position => $parameterReflectionWithPhpDoc) { $parameterType = $parameterReflectionWithPhpDoc->getType(); $className = ClassNameFromObjectTypeResolver::resolve($parameterReflectionWithPhpDoc->getType()); diff --git a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php index 246b3a71996..1b8e71fabbd 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php @@ -4,6 +4,7 @@ namespace Rector\CodeQuality\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; @@ -13,7 +14,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PhpParser\NodeTraverser; use PHPStan\Type\NullType; use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; @@ -116,7 +116,7 @@ public function refactor(Node $node): ?Node &$hasChanged ): int|null|Return_ { if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof Return_ && ! $node->expr instanceof Expr) { diff --git a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php index ed40b203f1e..5f83ab8be25 100644 --- a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; diff --git a/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php b/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php index de0c01e8899..d7d8b483630 100644 --- a/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php @@ -4,6 +4,7 @@ namespace Rector\CodeQuality\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; @@ -12,7 +13,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ClassReflection; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard; @@ -153,11 +153,11 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): ? } $shouldSkip = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; }); if ($shouldSkip) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; @@ -230,7 +230,7 @@ private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, Cl } $isInsideStaticClassMethod = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; }); if ($isInsideStaticClassMethod) { diff --git a/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php b/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php index e9859c2193c..7f25b5c0239 100644 --- a/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php +++ b/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php @@ -162,11 +162,11 @@ private function createNewPropertyFromPropertyTagValueNodes(array $propertyPhpDo } // is property already defined? - if ($class->getProperty($propertyName)) { + if ($class->getProperty($propertyName) instanceof Property) { // improve exising one type if needed $existingProperty = $class->getProperty($propertyName); - if ($existingProperty->type !== null) { + if ($existingProperty->type instanceof Node) { continue; } diff --git a/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php b/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php index e29bcd9e983..2ffbb5ca8fa 100644 --- a/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php +++ b/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php @@ -4,6 +4,7 @@ namespace Rector\CodeQuality\Rector\Foreach_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; -use PhpParser\NodeTraverser; use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\Value\ValueResolver; @@ -132,7 +132,7 @@ function (Node $subNode) use ($emptyArrayVariables, &$isAppend): ?int { $isAppend = $this->isNames($subNode->var->var, $emptyArrayVariables); if ($isAppend) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } } @@ -141,7 +141,7 @@ function (Node $subNode) use ($emptyArrayVariables, &$isAppend): ?int { $this->isNames($subNode->var, $emptyArrayVariables) && ! $this->valueResolver->isValue($subNode->expr, [])) { $isAppend = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php b/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php index b35ee50ed47..246cf3644bc 100644 --- a/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php +++ b/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php @@ -6,8 +6,8 @@ use PhpParser\Node; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; +use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Foreach_; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; @@ -100,14 +100,16 @@ public function refactor(Node $node): ?Node } // special case of nested array items - if ($stmt->valueVar instanceof Array_) { + if ($stmt->valueVar instanceof List_) { $valueArray = $this->refactorArrayForeachValue($stmt->valueVar, $stmt); - if ($valueArray instanceof Array_) { - $stmt->valueVar = $valueArray; + if (! $valueArray instanceof List_) { + continue; } + $stmt->valueVar = $valueArray; + // not sure what does this mean :) - if ($stmt->valueVar->items !== []) { + if ($valueArray->items !== []) { continue; } @@ -146,11 +148,11 @@ public function refactor(Node $node): ?Node /** * @param int[] $removedKeys */ - private function isArrayItemsRemovalWithoutChangingOrder(Array_ $array, array $removedKeys): bool + private function isArrayItemsRemovalWithoutChangingOrder(List_ $list, array $removedKeys): bool { $hasRemovingStarted = false; - foreach (array_keys($array->items) as $key) { + foreach (array_keys($list->items) as $key) { if (in_array($key, $removedKeys, true)) { $hasRemovingStarted = true; } elseif ($hasRemovingStarted) { @@ -162,12 +164,12 @@ private function isArrayItemsRemovalWithoutChangingOrder(Array_ $array, array $r return true; } - private function refactorArrayForeachValue(Array_ $array, Foreach_ $foreach): ?Array_ + private function refactorArrayForeachValue(List_ $list, Foreach_ $foreach): ?List_ { // only last items can be removed, without changing the order $removedKeys = []; - foreach ($array->items as $key => $arrayItem) { + foreach ($list->items as $key => $arrayItem) { if (! $arrayItem instanceof ArrayItem) { // only known values can be processes return null; @@ -186,16 +188,16 @@ private function refactorArrayForeachValue(Array_ $array, Foreach_ $foreach): ?A $removedKeys[] = $key; } - if (! $this->isArrayItemsRemovalWithoutChangingOrder($array, $removedKeys)) { + if (! $this->isArrayItemsRemovalWithoutChangingOrder($list, $removedKeys)) { return null; } // clear removed items foreach ($removedKeys as $removedKey) { - unset($array->items[$removedKey]); + unset($list->items[$removedKey]); } - return $array; + return $list; } private function isVariableUsedInForeach(Variable $variable, Foreach_ $foreach): bool diff --git a/rules/CodeQuality/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php b/rules/CodeQuality/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php index d17d187c387..c6765a2e5db 100644 --- a/rules/CodeQuality/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php +++ b/rules/CodeQuality/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; diff --git a/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php b/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php index 9597e42d0df..1848c7164b9 100644 --- a/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php +++ b/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php @@ -6,7 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; diff --git a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php index 4621d61854b..02b9ec1aeda 100644 --- a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php @@ -4,10 +4,11 @@ namespace Rector\CodeQuality\Rector\FuncCall; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Cast; use PhpParser\Node\Expr\Cast\Array_; @@ -18,7 +19,6 @@ use PhpParser\Node\Expr\Cast\String_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Expression; -use PhpParser\NodeTraverser; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -94,7 +94,7 @@ public function getNodeTypes(): array } /** - * @param FuncCall|Expression|Assign|Expr\ArrayItem|Node\Arg $node + * @param FuncCall|Expression|Assign|ArrayItem|Node\Arg $node */ public function refactor(Node $node): null|int|Expression|Assign|Cast { @@ -111,7 +111,7 @@ public function refactor(Node $node): null|int|Expression|Assign|Cast return null; } - return NodeTraverser::DONT_TRAVERSE_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CHILDREN; } if ($node instanceof Expression) { diff --git a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php index d067dc64b7e..abf3f53a5a1 100644 --- a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; diff --git a/rules/CodeQuality/Rector/If_/CombineIfRector.php b/rules/CodeQuality/Rector/If_/CombineIfRector.php index 83f48153fc7..5e466c10275 100644 --- a/rules/CodeQuality/Rector/If_/CombineIfRector.php +++ b/rules/CodeQuality/Rector/If_/CombineIfRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; @@ -86,6 +87,17 @@ public function refactor(Node $node): ?Node } $node->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null); + + $cond = $node->cond; + + while ($cond instanceof BinaryOp) { + if (! $cond->right instanceof BinaryOp) { + $cond->right->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + + $cond = $cond->right; + } + $node->cond = new BooleanAnd($node->cond, $subIf->cond); $node->stmts = $subIf->stmts; diff --git a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php index 18cb6a0a91c..2a15152e16a 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -99,7 +99,7 @@ private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $i return false; } - if (in_array($oldTokens[$i], ['{', ':'], true)) { + if (in_array((string) $oldTokens[$i], ['{', ':'], true)) { // all good return true; } diff --git a/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php b/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php index 8ecc9988a6a..af1859104ef 100644 --- a/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php +++ b/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php @@ -7,11 +7,11 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Expr\Throw_ as ExprThrow_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; +use PhpParser\Node\Stmt\Expression; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\IfManipulator; use Rector\PhpParser\Node\Value\ValueResolver; @@ -126,9 +126,9 @@ public function refactor(Node $node): ?Node continue; } - if ($stmt instanceof Throw_) { + if ($stmt instanceof Expression && $stmt->expr instanceof Throw_) { unset($node->stmts[$key]); - $appendExpr = new ExprThrow_($stmt->expr); + $appendExpr = $stmt->expr; continue; } diff --git a/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php b/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php index 6491e726144..25c30b0caf9 100644 --- a/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php +++ b/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php @@ -4,6 +4,8 @@ namespace Rector\CodeQuality\Rector\If_; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -19,8 +21,6 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ElseIf_; @@ -176,14 +176,14 @@ private function resolveCount(bool $isNegated, FuncCall $funcCall): Identical | return null; } - $lNumber = new LNumber(0); + $int = new Int_(0); // compare === 0, assumption if ($isNegated) { - return new Identical($funcCall, $lNumber); + return new Identical($funcCall, $int); } - return new Greater($funcCall, $lNumber); + return new Greater($funcCall, $int); } /** @@ -255,24 +255,24 @@ private function resolveZeroIdenticalstring( private function resolveInteger(bool $isNegated, Expr $expr): Identical | NotIdentical { - $lNumber = new LNumber(0); + $int = new Int_(0); if ($isNegated) { - return new Identical($expr, $lNumber); + return new Identical($expr, $int); } - return new NotIdentical($expr, $lNumber); + return new NotIdentical($expr, $int); } private function resolveFloat(bool $isNegated, Expr $expr): Identical | NotIdentical { - $dNumber = new DNumber(0.0); + $float = new Float_(0.0); if ($isNegated) { - return new Identical($expr, $dNumber); + return new Identical($expr, $float); } - return new NotIdentical($expr, $dNumber); + return new NotIdentical($expr, $float); } private function resolveNullable(bool $isNegated, Expr $expr): Identical | NotIdentical diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php index 12d21ee35f6..86afbc6a1b0 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php @@ -17,6 +17,7 @@ use Rector\BetterPhpDocParser\Comment\CommentsMerger; use Rector\CodeQuality\NodeManipulator\ExprBoolCaster; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\Rector\AbstractRector; @@ -100,6 +101,7 @@ public function refactor(Node $node): ?Node continue; } + $if->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null); $newReturn = $this->resolveReturn($innerIfInnerNode, $if, $return); if (! $newReturn instanceof Return_) { continue; diff --git a/rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php b/rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php index e3978e1696c..b5db1d55a22 100644 --- a/rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php +++ b/rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php @@ -51,7 +51,7 @@ public function getNodeTypes(): array /** * @param LogicalOr|LogicalAnd $node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): BooleanAnd|BooleanOr { return $this->refactorLogicalToBoolean($node); } diff --git a/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php b/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php index 72b7e1dcbc6..5caf971b692 100644 --- a/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php +++ b/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php @@ -76,11 +76,8 @@ private function doesNotEqualContainsShipCompareToken(NotEqual $notEqual): bool for ($i = $tokenStartPos; $i < $tokenEndPos; ++$i) { $token = $this->file->getOldTokens()[$i]; - if (! isset($token[1])) { - continue; - } - if ($token[1] === '<>') { + if ((string) $token === '<>') { return true; } } diff --git a/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php b/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php index c4d9dfe979b..5383af1415c 100644 --- a/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php +++ b/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php @@ -74,7 +74,11 @@ public function refactor(Node $node): ?Node } $node->cond = $node->cond->expr; - [$node->if, $node->else] = [$node->else, $node->if]; + $else = clone $node->else; + $if = clone $node->if; + + $node->else = $if; + $node->if = $else; if ($node->if instanceof Ternary) { $ternary = $node->if; @@ -82,6 +86,11 @@ public function refactor(Node $node): ?Node $ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null); } + if ($node->else instanceof Ternary) { + $ternary = $node->else; + $ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + return $node; } } diff --git a/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php b/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php index 92e64a504f8..011f805608d 100644 --- a/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php +++ b/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php @@ -4,12 +4,12 @@ namespace Rector\CodingStyle\ClassNameImport; +use PhpParser\Node\UseItem; use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; final readonly class AliasUsesResolver @@ -52,14 +52,14 @@ public function resolveFromStmts(array $stmts): array /** @param Use_::TYPE_* $useType */ $this->useImportsTraverser->traverserStmts($stmts, static function ( int $useType, - UseUse $useUse, + UseItem $useItem, string $name ) use (&$aliasedUses): void { if ($useType !== Use_::TYPE_NORMAL) { return; } - if (! $useUse->alias instanceof Identifier) { + if (! $useItem->alias instanceof Identifier) { return; } diff --git a/rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php b/rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php index 5d98ddc81d2..7c26e6bfd79 100644 --- a/rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php +++ b/rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php @@ -4,13 +4,13 @@ namespace Rector\CodingStyle\ClassNameImport; +use PhpParser\Node\UseItem; use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; @@ -116,15 +116,15 @@ private function isFunctionOrConstantImport(FullyQualified $fullyQualified): boo } private function isConflictedShortNameInUse( - UseUse $useUse, + UseItem $useItem, string $useName, string $lastUseName, string $stringName ): bool { - if (! $useUse->alias instanceof Identifier && $useName !== $stringName && $lastUseName === $stringName) { + if (! $useItem->alias instanceof Identifier && $useName !== $stringName && $lastUseName === $stringName) { return true; } - return $useUse->alias instanceof Identifier && $useUse->alias->toString() === $stringName; + return $useItem->alias instanceof Identifier && $useItem->alias->toString() === $stringName; } } diff --git a/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php b/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php index 18a892c13e3..24e84d42aa4 100644 --- a/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php +++ b/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php @@ -4,11 +4,11 @@ namespace Rector\CodingStyle\ClassNameImport; +use PhpParser\Node\UseItem; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; @@ -21,7 +21,7 @@ public function __construct( /** * @param Stmt[] $stmts - * @param callable(Use_::TYPE_* $useType, UseUse $useUse, string $name): void $callable + * @param callable(Use_::TYPE_* $useType, UseItem $useUse, string $name):void $callable */ public function traverserStmts(array $stmts, callable $callable): void { @@ -51,7 +51,7 @@ public function traverserStmts(array $stmts, callable $callable): void } /** - * @param callable(Use_::TYPE_* $useType, UseUse $useUse, string $name): void $callable + * @param callable(Use_::TYPE_* $useType, UseItem $useUse, string $name):void $callable */ private function processGroupUse(GroupUse $groupUse, callable $callable): void { diff --git a/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php b/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php index 596808fcccd..c278a3fed00 100644 --- a/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php +++ b/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php @@ -4,11 +4,11 @@ namespace Rector\CodingStyle\ClassNameImport; +use PhpParser\Node\UseItem; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\CodingStyle\ClassNameImport\ValueObject\UsedImports; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpParser\Node\BetterNodeFinder; @@ -46,12 +46,12 @@ public function resolveForStmts(array $stmts): UsedImports /** @param Use_::TYPE_* $useType */ $this->useImportsTraverser->traverserStmts($stmts, static function ( int $useType, - UseUse $useUse, + UseItem $useItem, string $name ) use (&$usedImports, &$usedFunctionImports, &$usedConstImports): void { if ($useType === Use_::TYPE_NORMAL) { - if ($useUse->alias instanceof Identifier) { - $usedImports[] = new AliasedObjectType($useUse->alias->toString(), $name); + if ($useItem->alias instanceof Identifier) { + $usedImports[] = new AliasedObjectType($useItem->alias->toString(), $name); } else { $usedImports[] = new FullyQualifiedObjectType($name); } diff --git a/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php b/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php index 23c410d0753..4fd5ef3f006 100644 --- a/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php +++ b/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php @@ -4,12 +4,12 @@ namespace Rector\CodingStyle\NodeAnalyzer; +use PhpParser\Node\UseItem; use Nette\Utils\Strings; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\Exception\ShouldNotHappenException; use Rector\Naming\Naming\UseImportsResolver; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -61,11 +61,11 @@ public function matchNameWithUses(string $tag, array $uses): ?string return null; } - private function resolveName(string $prefix, string $tag, UseUse $useUse): string + private function resolveName(string $prefix, string $tag, UseItem $useItem): string { // useuse can be renamed on the fly, so just in case, use the original one - $originalUseUseNode = $useUse->getAttribute(AttributeKey::ORIGINAL_NODE); - if (! $originalUseUseNode instanceof UseUse) { + $originalUseUseNode = $useItem->getAttribute(AttributeKey::ORIGINAL_NODE); + if (! $originalUseUseNode instanceof UseItem) { throw new ShouldNotHappenException(); } @@ -87,11 +87,11 @@ private function resolveName(string $prefix, string $tag, UseUse $useUse): strin return $prefix . $originalUseUseNode->name . '\\' . $unaliasedShortClass; } - private function isUseMatchingName(string $tag, UseUse $useUse): bool + private function isUseMatchingName(string $tag, UseItem $useItem): bool { // useuse can be renamed on the fly, so just in case, use the original one - $originalUseUseNode = $useUse->getAttribute(AttributeKey::ORIGINAL_NODE); - if (! $originalUseUseNode instanceof UseUse) { + $originalUseUseNode = $useItem->getAttribute(AttributeKey::ORIGINAL_NODE); + if (! $originalUseUseNode instanceof UseItem) { return false; } diff --git a/rules/CodingStyle/NodeFactory/ArrayCallableToMethodCallFactory.php b/rules/CodingStyle/NodeFactory/ArrayCallableToMethodCallFactory.php index e9facac25ce..5940d0ad542 100644 --- a/rules/CodingStyle/NodeFactory/ArrayCallableToMethodCallFactory.php +++ b/rules/CodingStyle/NodeFactory/ArrayCallableToMethodCallFactory.php @@ -5,7 +5,7 @@ namespace Rector\CodingStyle\NodeFactory; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; diff --git a/rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php b/rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php index 2a3f446a81b..8e6b4036e48 100644 --- a/rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php +++ b/rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php @@ -108,7 +108,6 @@ private function collectExpressions(Assign $assign, Expr $expr): array break; } - /** @var Expr $assign */ $assign = $assign->expr; } diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 6d783ed4c6c..08ebac597cd 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -4,6 +4,7 @@ namespace Rector\CodingStyle\Rector\Catch_; +use PhpParser\NodeVisitor; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr\Assign; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\TryCatch; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Type\ObjectType; use Rector\Naming\Naming\PropertyNaming; @@ -232,7 +232,7 @@ private function replaceNextUsageVariable( &$nonAssignedVariables ): ?int { if ($node instanceof Assign && $node->var instanceof Variable) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof Variable) { diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index 7716b2dc1ad..48637f488cf 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -186,6 +186,6 @@ private function isPrecededByEmptyLine(ClassMethod | Function_ | Closure $node, $previousNode = $node->stmts[$key - 1]; $currentNode = $node->stmts[$key]; - return abs($currentNode->getLine() - $previousNode->getLine()) >= 2; + return abs($currentNode->getStartLine() - $previousNode->getStartLine()) >= 2; } } diff --git a/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php b/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php index 016f29083ac..6caadbc221b 100644 --- a/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php +++ b/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php @@ -4,6 +4,8 @@ namespace Rector\CodingStyle\Rector\Encapsed; +use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\Node\InterpolatedStringPart; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Arg; @@ -13,8 +15,6 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\Encapsed; -use PhpParser\Node\Scalar\EncapsedStringPart; use PhpParser\Node\Scalar\String_; use PHPStan\Type\Type; use Rector\Contract\Rector\ConfigurableRectorInterface; @@ -109,11 +109,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [Encapsed::class]; + return [InterpolatedString::class]; } /** - * @param Encapsed $node + * @param InterpolatedString $node */ public function refactor(Node $node): ?Node { @@ -125,7 +125,7 @@ public function refactor(Node $node): ?Node $this->argumentVariables = []; foreach ($node->parts as $part) { - if ($part instanceof EncapsedStringPart) { + if ($part instanceof InterpolatedStringPart) { $this->collectEncapsedStringPart($part); } else { $this->collectExpr($part); @@ -135,14 +135,14 @@ public function refactor(Node $node): ?Node return $this->createSprintfFuncCallOrConcat($this->sprintfFormat, $this->argumentVariables); } - private function shouldSkip(Encapsed $encapsed): bool + private function shouldSkip(InterpolatedString $interpolatedString): bool { - return $encapsed->hasAttribute(AttributeKey::DOC_LABEL); + return $interpolatedString->hasAttribute(AttributeKey::DOC_LABEL); } - private function collectEncapsedStringPart(EncapsedStringPart $encapsedStringPart): void + private function collectEncapsedStringPart(InterpolatedStringPart $interpolatedStringPart): void { - $stringValue = $encapsedStringPart->value; + $stringValue = $interpolatedStringPart->value; if ($stringValue === "\n") { $this->argumentVariables[] = new ConstFetch(new Name('PHP_EOL')); $this->sprintfFormat .= '%s'; diff --git a/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php b/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php index a1242b17141..aab43a8729a 100644 --- a/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php +++ b/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php @@ -4,9 +4,9 @@ namespace Rector\CodingStyle\Rector\Encapsed; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\Encapsed; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -42,11 +42,11 @@ function run($world) */ public function getNodeTypes(): array { - return [Encapsed::class]; + return [InterpolatedString::class]; } /** - * @param Encapsed $node + * @param InterpolatedString $node */ public function refactor(Node $node): ?Node { diff --git a/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php b/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php index 276085c39f4..fcbfb66d252 100644 --- a/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php +++ b/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php @@ -4,15 +4,15 @@ namespace Rector\CodingStyle\Rector\Foreach_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeFinder; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -128,7 +128,7 @@ private function replaceValueArrayAccessorsInForeachTree(Foreach_ $foreach): arr if (! $dim instanceof String_) { $createdDestructedVariables = []; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $destructedVariable = $this->getDestructedVariableName($usedVariableNames, $dim); @@ -144,7 +144,7 @@ private function replaceValueArrayAccessorsInForeachTree(Foreach_ $foreach): arr * Get all variable names which are used in the foreach tree. We need this so that we don't create array destructor * with variable name which is already used somewhere bellow * - * @return list + * @return string[] */ private function getUsedVariableNamesInForeachTree(Foreach_ $foreach): array { @@ -165,7 +165,7 @@ private function getUsedVariableNamesInForeachTree(Foreach_ $foreach): array */ private function getDestructedVariableName(array $usedVariableNames, String_ $string): string { - $desiredVariableName = (string) $string->value; + $desiredVariableName = $string->value; if (in_array($desiredVariableName, $usedVariableNames, true) === false) { return $desiredVariableName; diff --git a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index 9eb5b3b0d30..c2b9f0a6be8 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -8,11 +8,12 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PHPStan\Type\ArrayType; +use PHPStan\Type\Constant\ConstantArrayType; use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer; use Rector\Php\PhpVersionProvider; use Rector\Rector\AbstractRector; @@ -141,14 +142,14 @@ private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool } $arrayStaticType = $this->getType($expr); - if (! $arrayStaticType instanceof ArrayType) { + if (! $arrayStaticType->isArray()->yes()) { return true; } return ! $this->isArrayKeyTypeAllowed($arrayStaticType); } - private function isArrayKeyTypeAllowed(ArrayType $arrayType): bool + private function isArrayKeyTypeAllowed(ArrayType|ConstantArrayType $arrayType): bool { if ($arrayType->getKeyType()->isInteger()->yes()) { return true; diff --git a/rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php b/rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php index a55b40efc6b..18076869362 100644 --- a/rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php +++ b/rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php @@ -4,6 +4,7 @@ namespace Rector\CodingStyle\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -13,7 +14,6 @@ use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\ElseIf_; use PhpParser\Node\Stmt\If_; use Rector\Rector\AbstractRector; @@ -207,7 +207,7 @@ private function matchCountFuncCallArgExpr(Expr $expr): ?Expr private function isZeroLNumber(Expr $expr): bool { - if (! $expr instanceof LNumber) { + if (! $expr instanceof Int_) { return false; } diff --git a/rules/CodingStyle/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php b/rules/CodingStyle/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php index f3d95cf50be..47b6fd545c7 100644 --- a/rules/CodingStyle/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php +++ b/rules/CodingStyle/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php @@ -4,6 +4,7 @@ namespace Rector\CodingStyle\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp; @@ -16,7 +17,6 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; use Rector\Util\PhpVersionFactory; @@ -135,7 +135,7 @@ private function isPhpVersionConstant(Expr $expr): bool return $expr->name->toString() === 'PHP_VERSION'; } - private function getNewNodeForArg(Expr $expr): ConstFetch | LNumber | null + private function getNewNodeForArg(Expr $expr): ConstFetch | Int_ | null { if ($this->isPhpVersionConstant($expr)) { return new ConstFetch(new Name('PHP_VERSION_ID')); @@ -144,13 +144,13 @@ private function getNewNodeForArg(Expr $expr): ConstFetch | LNumber | null return $this->getVersionNumberFormVersionString($expr); } - private function getVersionNumberFormVersionString(Expr $expr): ?LNumber + private function getVersionNumberFormVersionString(Expr $expr): ?Int_ { if (! $expr instanceof String_) { return null; } $value = PhpVersionFactory::createIntVersion($expr->value); - return new LNumber($value); + return new Int_($value); } } diff --git a/rules/CodingStyle/Rector/Property/SplitGroupedPropertiesRector.php b/rules/CodingStyle/Rector/Property/SplitGroupedPropertiesRector.php index 504b87d202f..8b03e4118ba 100644 --- a/rules/CodingStyle/Rector/Property/SplitGroupedPropertiesRector.php +++ b/rules/CodingStyle/Rector/Property/SplitGroupedPropertiesRector.php @@ -6,7 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -70,7 +70,7 @@ public function refactor(Node $node): ?array return null; } - /** @var PropertyProperty $firstPropertyProperty */ + /** @var PropertyItem $firstPropertyProperty */ $firstPropertyProperty = array_shift($allProperties); $node->props = [$firstPropertyProperty]; diff --git a/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php b/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php index 1b863f06456..8e381f1833a 100644 --- a/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php +++ b/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php @@ -128,7 +128,6 @@ private function refactorTraitUse(TraitUse $traitUse): ?array foreach ($traitUse->adaptations as $traitAdaptation) { if ($traitAdaptation instanceof Alias - && $traitAdaptation->trait && $traitAdaptation->trait instanceof Name && $traitAdaptation->trait->toString() === $singleTraitUse->toString()) { $adaptation[] = $traitAdaptation; diff --git a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php index 28aad7bf68a..6e301520a47 100644 --- a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php @@ -4,10 +4,10 @@ namespace Rector\DeadCode\NodeAnalyzer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\NullsafeMethodCall; @@ -17,7 +17,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Trait_; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Parser\ArrayMapArgVisitor; use PHPStan\Reflection\ClassReflection; @@ -110,10 +109,6 @@ private function isInArrayMap(Class_ $class, Array_ $array): bool return false; } - if (! $array->items[1] instanceof ArrayItem) { - return false; - } - $value = $this->valueResolver->getValue($array->items[1]->value); if (! is_string($value)) { @@ -198,19 +193,19 @@ private function isUsedByTrait(Trait_ $trait, string $classMethodName, string $c (array) $classMethod->stmts, function (Node $subNode) use ($className, $classMethodName, &$callMethod): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($subNode instanceof MethodCall && $this->nodeNameResolver->isName($subNode->var, 'this') && $this->nodeNameResolver->isName($subNode->name, $classMethodName)) { $callMethod = $subNode; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if ($this->isStaticCallMatch($subNode, $className, $classMethodName)) { $callMethod = $subNode; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/rules/DeadCode/NodeManipulator/ControllerClassMethodManipulator.php b/rules/DeadCode/NodeManipulator/ControllerClassMethodManipulator.php index dfaaf4d6fbd..d08cfac84d7 100644 --- a/rules/DeadCode/NodeManipulator/ControllerClassMethodManipulator.php +++ b/rules/DeadCode/NodeManipulator/ControllerClassMethodManipulator.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; +use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\NodeNameResolver\NodeNameResolver; @@ -30,7 +31,7 @@ public function isControllerClassMethod(Class_ $class, ClassMethod $classMethod) } $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); - return $phpDocInfo->hasByType(GenericTagValueNode::class); + return $phpDocInfo->hasByTypes([GenericTagValueNode::class, SpacelessPhpDocTagNode::class]); } private function hasParentClassController(Class_ $class): bool diff --git a/rules/DeadCode/NodeManipulator/CountManipulator.php b/rules/DeadCode/NodeManipulator/CountManipulator.php index ab65ec9e7d0..d621490f40d 100644 --- a/rules/DeadCode/NodeManipulator/CountManipulator.php +++ b/rules/DeadCode/NodeManipulator/CountManipulator.php @@ -4,13 +4,13 @@ namespace Rector\DeadCode\NodeManipulator; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use PHPStan\Type\NeverType; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; @@ -88,7 +88,7 @@ private function isSmallerOrEqual(SmallerOrEqual $smallerOrEqual, Expr $expr): b private function isNumber(Expr $expr, int $value): bool { - if (! $expr instanceof LNumber) { + if (! $expr instanceof Int_) { return false; } diff --git a/rules/DeadCode/NodeManipulator/VariadicFunctionLikeDetector.php b/rules/DeadCode/NodeManipulator/VariadicFunctionLikeDetector.php index 495d640d089..66edf58afa7 100644 --- a/rules/DeadCode/NodeManipulator/VariadicFunctionLikeDetector.php +++ b/rules/DeadCode/NodeManipulator/VariadicFunctionLikeDetector.php @@ -4,10 +4,10 @@ namespace Rector\DeadCode\NodeManipulator; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\FunctionLike; -use PhpParser\NodeTraverser; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -44,7 +44,7 @@ function (Node $node) use (&$isVariadic): ?int { $isVariadic = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } ); diff --git a/rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php b/rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php index bb1285ff975..a1e2842c7d9 100644 --- a/rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php +++ b/rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\PhpDoc; +use PhpParser\Node; use PhpParser\Node\FunctionLike; use PhpParser\Node\Name; use PhpParser\Node\Param; @@ -42,7 +43,7 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct return false; } - if ($param->type === null) { + if (!$param->type instanceof Node) { return false; } diff --git a/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php b/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php index 2090398bfe9..e600c825fac 100644 --- a/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php +++ b/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\PhpDoc; +use PhpParser\Node; use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\Type\IntersectionType; @@ -25,7 +26,7 @@ public function __construct( public function isDead(VarTagValueNode $varTagValueNode, Property $property): bool { - if ($property->type === null) { + if (!$property->type instanceof Node) { return false; } diff --git a/rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php b/rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php index 8361f0fc970..575bf1f4fda 100644 --- a/rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php +++ b/rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\PreDec; use PhpParser\Node\Expr\PreInc; use Rector\PhpParser\Printer\BetterStandardPrinter; diff --git a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php index 233f968f586..1d1655fcbbe 100644 --- a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php +++ b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\Cast; +use PHPStan\Type\Constant\ConstantArrayType; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Cast; @@ -101,6 +102,18 @@ public function refactor(Node $node): ?Node return null; } + if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) { + if ($this->shouldSkip($node->expr)) { + return null; + } + + if ($this->shouldSkipCall($node->expr)) { + return null; + } + + return $node->expr; + } + $sameNodeType = self::CAST_CLASS_TO_NODE_TYPE[$nodeClass]; if (! $nodeType instanceof $sameNodeType) { return null; diff --git a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php index bff6d9e63f1..f8acc0268ec 100644 --- a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php +++ b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php @@ -4,9 +4,9 @@ namespace Rector\DeadCode\Rector\ClassConst; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Stmt\ClassConst; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ClassReflection; use Rector\NodeManipulator\ClassConstManipulator; use Rector\PHPStan\ScopeFetcher; @@ -79,7 +79,7 @@ public function refactor(Node $node): ?int return null; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } private function shouldSkipClassConst(ClassConst $classConst): bool diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php index d41dd924e46..c5aa63aa49a 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PhpParser\NodeTraverser; use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; @@ -104,7 +104,7 @@ public function refactor(Node $node): ?Node &$hasChanged ): int|null|array|Return_ { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Return_) { diff --git a/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php b/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php index dbe7ec6117e..a6fc76317f8 100644 --- a/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php +++ b/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php @@ -4,16 +4,16 @@ namespace Rector\DeadCode\Rector\ConstFetch; +use PhpParser\NodeVisitor; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\If_; -use PhpParser\NodeTraverser; use Rector\Php\PhpVersionProvider; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersion; @@ -149,12 +149,12 @@ private function processGreaterOrEqual( private function refactorSmallerLeft(Smaller $smaller): ?int { $value = $smaller->right; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } if ($this->phpVersion >= $value->value) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return null; @@ -166,7 +166,7 @@ private function refactorSmallerLeft(Smaller $smaller): ?int private function refactorSmallerRight(Smaller $smaller, If_ $if): null|array|int { $value = $smaller->left; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } @@ -175,7 +175,7 @@ private function refactorSmallerRight(Smaller $smaller, If_ $if): null|array|int } if ($if->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $if->stmts; @@ -187,7 +187,7 @@ private function refactorSmallerRight(Smaller $smaller, If_ $if): null|array|int private function refactorGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ $if): null|array|int { $value = $greaterOrEqual->right; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } @@ -196,7 +196,7 @@ private function refactorGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ } if ($if->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $if->stmts; @@ -205,12 +205,12 @@ private function refactorGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ private function refactorGreaterOrEqualRight(GreaterOrEqual $greaterOrEqual): ?int { $value = $greaterOrEqual->left; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } if ($this->phpVersion >= $value->value) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return null; @@ -238,7 +238,7 @@ private function refactorGreater(ConstFetch $constFetch, Greater $greater, If_ $ private function refactorGreaterLeft(Greater $greater, If_ $if): null|array|int { $value = $greater->right; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } @@ -247,7 +247,7 @@ private function refactorGreaterLeft(Greater $greater, If_ $if): null|array|int } if ($if->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $if->stmts; @@ -256,12 +256,12 @@ private function refactorGreaterLeft(Greater $greater, If_ $if): null|array|int private function refactorGreaterRight(Greater $greater): ?int { $value = $greater->left; - if (! $value instanceof LNumber) { + if (! $value instanceof Int_) { return null; } if ($this->phpVersion >= $value->value) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return null; diff --git a/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php b/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php index acf63b4929f..688a02071ba 100644 --- a/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php +++ b/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php @@ -4,12 +4,12 @@ namespace Rector\DeadCode\Rector\Expression; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Nop; -use PhpParser\NodeTraverser; use PHPStan\Reflection\Php\PhpPropertyReflection; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\NodeManipulator\LivingCodeManipulator; @@ -117,6 +117,6 @@ private function removeNodeAndKeepComments(Expression $expression): int|Node return $nop; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } } diff --git a/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php b/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php index 8770a819412..764d510034c 100644 --- a/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php +++ b/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php @@ -4,10 +4,10 @@ namespace Rector\DeadCode\Rector\Expression; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Expression; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -56,6 +56,6 @@ public function refactor(Node $node): ?int return null; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } } diff --git a/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php b/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php index 743b6d86879..76394d5a63b 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php @@ -4,8 +4,8 @@ namespace Rector\DeadCode\Rector\For_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Continue_; use PhpParser\Node\Stmt\Do_; @@ -87,7 +87,7 @@ private function isRemovable(Stmt $stmt): bool return false; } - if ($stmt->num instanceof LNumber) { + if ($stmt->num instanceof Int_) { return $stmt->num->value < 2; } diff --git a/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php b/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php index 009cb422d53..eefbdf6064c 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php @@ -82,7 +82,7 @@ public function getNodeTypes(): array /** * @param StmtsAwareInterface $node */ - public function refactor(Node $node): Node|null|int + public function refactor(Node $node): Node|null { if ($node->stmts === null) { return null; @@ -161,7 +161,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter return; } - $exprs = array_filter([$for->expr, $for->valueVar, $for->valueVar]); + $exprs = [$for->expr, $for->valueVar, $for->valueVar]; $variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class); foreach ($variables as $variable) { if ($this->stmtsManipulator->isVariableUsedInNextStmt( diff --git a/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php b/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php index f19ac68c331..e0e4118b4f5 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php @@ -4,12 +4,12 @@ namespace Rector\DeadCode\Rector\For_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -66,6 +66,6 @@ public function refactor(Node $node): ?int return null; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } } diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index 7d2866981b3..fbce2eb3ad5 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\If_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; -use PhpParser\NodeTraverser; use PHPStan\Type\IntersectionType; use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer; use Rector\NodeAnalyzer\ExprAnalyzer; @@ -117,7 +117,7 @@ public function refactor(Node $node): int|null|array|If_ } if ($node->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $node->stmts; diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index a0f97fa4ae9..3c1dc9dd9f5 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\If_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; @@ -115,11 +115,11 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu } if ($if->cond !== $instanceof) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } if ($if->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } // unwrap stmts diff --git a/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php index e305a11e023..eedafc0cd1a 100644 --- a/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\If_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Instanceof_; @@ -18,7 +19,6 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use Rector\NodeManipulator\IfManipulator; use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver; use Rector\Rector\AbstractRector; @@ -105,7 +105,7 @@ public function refactor(Node $node): ?Class_ ): int|null|array { // avoid loop ifs if ($node instanceof While_ || $node instanceof Foreach_ || $node instanceof For_ || $node instanceof Do_) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof If_) { @@ -174,11 +174,11 @@ private function refactorStmtAndInstanceof(Class_ $class, If_ $if, Instanceof_ $ } if ($if->cond !== $instanceof) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } if ($if->stmts === []) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $if->stmts; @@ -200,7 +200,7 @@ private function isSkippedPropertyFetch(Class_ $class, PropertyFetch|StaticPrope return true; } - return $property->type === null; + return !$property->type instanceof Node; } private function isInPropertyPromotedParams(Class_ $class, PropertyFetch|StaticPropertyFetch $propertyFetch): bool diff --git a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php index 0f200bfb639..20144ce88dd 100644 --- a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php +++ b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php @@ -88,9 +88,9 @@ public function getNodeTypes(): array /** * @param If_|StmtsAwareInterface $node - * @return Stmt[]|Foreach_|StmtsAwareInterface|null + * @return Foreach_|StmtsAwareInterface|null */ - public function refactor(Node $node): array|Node|null + public function refactor(Node $node): Node|null { if ($node instanceof If_) { $scope = ScopeFetcher::fetch($node); @@ -220,7 +220,7 @@ private function refactorIf(If_ $if, Scope $scope): ?Foreach_ private function shouldSkipForeachExpr(Expr $foreachExpr, Scope $scope): bool { - if ($foreachExpr instanceof ArrayDimFetch && $foreachExpr->dim !== null) { + if ($foreachExpr instanceof ArrayDimFetch && $foreachExpr->dim instanceof Expr) { $exprType = $this->nodeTypeResolver->getNativeType($foreachExpr->var); $dimType = $this->nodeTypeResolver->getNativeType($foreachExpr->dim); if (! $exprType->hasOffsetValueType($dimType)->yes()) { diff --git a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php index 5ba812d4253..64d1904a8a5 100644 --- a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php +++ b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php @@ -4,11 +4,11 @@ namespace Rector\DeadCode\Rector\If_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; -use PhpParser\NodeTraverser; use Rector\DeadCode\ConditionEvaluator; use Rector\DeadCode\ConditionResolver; use Rector\DeadCode\Contract\ConditionInterface; @@ -110,7 +110,7 @@ private function refactorIsNotMatch(If_ $if): array|int { // no else → just remove the node if (! $if->else instanceof Else_) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } // else is always used diff --git a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php index d2cce19d0f4..ed910877e8d 100644 --- a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php +++ b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php @@ -20,7 +20,6 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Static_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\Node\Stmt\Throw_; use PhpParser\Node\Stmt\While_; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; @@ -48,7 +47,6 @@ final class RemoveNonExistingVarAnnotationRector extends AbstractRector Echo_::class, Return_::class, Expression::class, - Throw_::class, If_::class, While_::class, Switch_::class, diff --git a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php index 20cdc527bda..08cb45681ba 100644 --- a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php +++ b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\Property; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Class_; @@ -11,7 +12,6 @@ use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\TraitUse; -use PhpParser\NodeTraverser; use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; @@ -170,7 +170,7 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi } if ($node instanceof Expression) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } $node->expr = $node->expr->expr; diff --git a/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php b/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php index 6bdeb87a507..147c76205f8 100644 --- a/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php +++ b/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php @@ -4,13 +4,14 @@ namespace Rector\DeadCode\Rector\TryCatch; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Finally_; use PhpParser\Node\Stmt\Nop; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\TryCatch; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -61,9 +62,9 @@ public function getNodeTypes(): array /** * @param TryCatch $node - * @return Stmt[]|null|TryCatch|int + * @return Stmt[]|null|int */ - public function refactor(Node $node): array|null|TryCatch|int + public function refactor(Node $node): array|null|int { $isEmptyFinallyStmts = ! $node->finally instanceof Finally_ || $this->isEmpty($node->finally->stmts); @@ -73,7 +74,7 @@ public function refactor(Node $node): array|null|TryCatch|int } if ($this->isEmpty($node->stmts)) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } if (count($node->catches) !== 1) { @@ -86,11 +87,11 @@ public function refactor(Node $node): array|null|TryCatch|int } $onlyCatchStmt = $onlyCatch->stmts[0]; - if (! $onlyCatchStmt instanceof Throw_) { + if (! ($onlyCatchStmt instanceof Expression && $onlyCatchStmt->expr instanceof Throw_)) { return null; } - if (! $this->nodeComparator->areNodesEqual($onlyCatch->var, $onlyCatchStmt->expr)) { + if (! $this->nodeComparator->areNodesEqual($onlyCatch->var, $onlyCatchStmt->expr->expr)) { return null; } diff --git a/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php b/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php index a29b21826c6..9e114ab26ff 100644 --- a/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php +++ b/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php @@ -96,6 +96,10 @@ public function refactor(Node $node): ?Node return null; } + foreach ($nestedIfsWithOnlyNonReturn as $nestedIfWithOnlyNonReturn) { + $nestedIfWithOnlyNonReturn->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + return $this->processNestedIfsWithNonBreaking($node, $nestedIfsWithOnlyNonReturn); } diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index 47bd23e81dc..638df65250f 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -12,7 +12,7 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -173,7 +173,7 @@ private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool } return ! ($lastStmt instanceof Return_ - || $lastStmt instanceof Throw_ + || ($lastStmt instanceof Expression && $lastStmt->expr instanceof Throw_) || $lastStmt instanceof Continue_ || ($lastStmt instanceof Expression && $lastStmt->expr instanceof Exit_)); } diff --git a/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php b/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php index ced80413395..fb5d8208752 100644 --- a/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php +++ b/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php @@ -4,6 +4,7 @@ namespace Rector\Naming\ExpectedNameResolver; +use PhpParser\Node; use PhpParser\Node\Param; use PHPStan\Type\ObjectType; use Rector\Naming\Naming\PropertyNaming; @@ -23,7 +24,7 @@ public function __construct( public function resolve(Param $param): ?string { // nothing to verify - if ($param->type === null) { + if (!$param->type instanceof Node) { return null; } diff --git a/rules/Naming/Naming/OverridenExistingNamesResolver.php b/rules/Naming/Naming/OverridenExistingNamesResolver.php index a90b43fb0cf..57f776a2655 100644 --- a/rules/Naming/Naming/OverridenExistingNamesResolver.php +++ b/rules/Naming/Naming/OverridenExistingNamesResolver.php @@ -87,7 +87,6 @@ private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $ $currentlyUsedNames[] = $currentVariableName; } - $currentlyUsedNames = array_values($currentlyUsedNames); $currentlyUsedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($currentlyUsedNames); $this->overridenExistingVariableNamesByClassMethod[$classMethodId] = $currentlyUsedNames; diff --git a/rules/Naming/PropertyRenamer/MatchTypePropertyRenamer.php b/rules/Naming/PropertyRenamer/MatchTypePropertyRenamer.php index 3b880eb919c..259c7964a1b 100644 --- a/rules/Naming/PropertyRenamer/MatchTypePropertyRenamer.php +++ b/rules/Naming/PropertyRenamer/MatchTypePropertyRenamer.php @@ -33,8 +33,8 @@ public function rename(PropertyRename $propertyRename): ?Property return null; } - $onlyPropertyProperty = $propertyRename->getPropertyProperty(); - $onlyPropertyProperty->name = new VarLikeIdentifier($propertyRename->getExpectedName()); + $propertyItem = $propertyRename->getPropertyProperty(); + $propertyItem->name = new VarLikeIdentifier($propertyRename->getExpectedName()); $this->renamePropertyFetchesInClass($propertyRename); return $propertyRename->getProperty(); diff --git a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php index 1f00853d8a3..945af06dd23 100644 --- a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php @@ -4,13 +4,13 @@ namespace Rector\Naming\Rector\Foreach_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use Rector\Naming\Guard\BreakingVariableRenameGuard; use Rector\Naming\Matcher\ForeachMatcher; use Rector\Naming\Naming\ExpectedNameResolver; @@ -99,7 +99,7 @@ public function refactor(Node $node): ?Node $node->stmts, function (Node $subNode) use ($node, &$hasRenamed): ?int { if ($subNode instanceof Class_ || $subNode instanceof Closure || $subNode instanceof Function_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Foreach_) { diff --git a/rules/Naming/ValueObject/PropertyRename.php b/rules/Naming/ValueObject/PropertyRename.php index 59e4b625b33..7baffc3d38c 100644 --- a/rules/Naming/ValueObject/PropertyRename.php +++ b/rules/Naming/ValueObject/PropertyRename.php @@ -6,7 +6,7 @@ use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use Rector\Validation\RectorAssert; final readonly class PropertyRename @@ -17,7 +17,7 @@ public function __construct( private string $currentName, private ClassLike $classLike, private string $classLikeName, - private PropertyProperty $propertyProperty + private PropertyItem $propertyItem ) { // name must be valid RectorAssert::propertyName($currentName); @@ -59,8 +59,8 @@ public function getClassLikeName(): string return $this->classLikeName; } - public function getPropertyProperty(): PropertyProperty + public function getPropertyProperty(): PropertyItem { - return $this->propertyProperty; + return $this->propertyItem; } } diff --git a/rules/Naming/VariableRenamer.php b/rules/Naming/VariableRenamer.php index 6ce7c4147a4..9e45bdf7e6e 100644 --- a/rules/Naming/VariableRenamer.php +++ b/rules/Naming/VariableRenamer.php @@ -4,13 +4,13 @@ namespace Rector\Naming; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; use PhpParser\Node\Param; use PhpParser\Node\Stmt; -use PhpParser\NodeTraverser; use PHPStan\Analyser\MutatingScope; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; @@ -58,7 +58,7 @@ function (Node $node) use ( ): int|null|Variable { // skip param names if ($node instanceof Param) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($assign instanceof Assign && $node === $assign) { diff --git a/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php b/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php index 80dffd208e9..7b4f700c232 100644 --- a/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php +++ b/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php @@ -4,11 +4,12 @@ namespace Rector\Php52\Rector\Switch_; +use PhpParser\Node\Scalar\Int_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Class_; @@ -19,7 +20,6 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use PHPStan\Type\Constant\ConstantIntegerType; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\Value\ValueResolver; @@ -113,12 +113,12 @@ private function processContinueStatement(Stmt|StmtsAwareInterface $stmt): void $stmt, function (Node $subNode): null|int|Break_ { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } // continue is belong to loop if ($subNode instanceof Foreach_ || $subNode instanceof While_ || $subNode instanceof Do_ || $subNode instanceof For_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Continue_) { @@ -130,7 +130,7 @@ function (Node $subNode): null|int|Break_ { return new Break_(); } - if ($subNode->num instanceof LNumber) { + if ($subNode->num instanceof Int_) { $continueNumber = $this->valueResolver->getValue($subNode->num); if ($continueNumber <= 1) { $this->hasChanged = true; diff --git a/rules/Php54/Rector/Break_/RemoveZeroBreakContinueRector.php b/rules/Php54/Rector/Break_/RemoveZeroBreakContinueRector.php index 551f1518555..4c77af6030b 100644 --- a/rules/Php54/Rector/Break_/RemoveZeroBreakContinueRector.php +++ b/rules/Php54/Rector/Break_/RemoveZeroBreakContinueRector.php @@ -4,10 +4,10 @@ namespace Rector\Php54\Rector\Break_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Continue_; use PHPStan\Type\Constant\ConstantIntegerType; @@ -89,7 +89,7 @@ public function refactor(Node $node): ?Node return null; } - if ($node->num instanceof LNumber) { + if ($node->num instanceof Int_) { $number = $this->valueResolver->getValue($node->num); if ($number > 1) { return null; @@ -122,7 +122,7 @@ private function processVariableNum(Break_ | Continue_ $stmt, Variable $numVaria } if ($staticType->getValue() > 0) { - $stmt->num = new LNumber($staticType->getValue()); + $stmt->num = new Int_($staticType->getValue()); return $stmt; } } diff --git a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php index 7366491e045..b7e307354a6 100644 --- a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php +++ b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php @@ -4,6 +4,7 @@ namespace Rector\Php55\Rector\String_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\ClassConstFetch; @@ -11,7 +12,6 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassConst; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ReflectionProvider; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; @@ -112,7 +112,7 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int // keep allowed string as condition if ($node instanceof FuncCall) { if ($this->isName($node, 'is_a')) { - return NodeTraverser::DONT_TRAVERSE_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CHILDREN; } return null; diff --git a/rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php b/rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php index 391f8201b84..cc0fd70275e 100644 --- a/rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php +++ b/rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\List_; diff --git a/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php b/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php index c23af94a49e..dfc4fe3f0b3 100644 --- a/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php +++ b/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php @@ -4,6 +4,7 @@ namespace Rector\Php70\Rector\Break_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\FunctionLike; @@ -11,7 +12,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\NodeTraverser; use Rector\NodeNestingScope\ContextAnalyzer; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -95,7 +95,7 @@ public function refactor(Node $node): Return_|null|int $node->cases, static function (Node $subNode): ?int { if ($subNode instanceof Class_ || ($subNode instanceof FunctionLike && ! $subNode instanceof ArrowFunction)) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Break_) { @@ -122,6 +122,6 @@ static function (Node $subNode): ?int { return new Return_(); } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } } diff --git a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php index 84f80501a5a..5cb915b3c35 100644 --- a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php +++ b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php @@ -82,7 +82,7 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactor(Node $node): Class_|int|null + public function refactor(Node $node): Class_|null { $className = $this->getName($node); if (! is_string($className)) { @@ -118,7 +118,7 @@ public function refactor(Node $node): Class_|int|null } if (count($classMethodStmts) === 1) { - $stmt = $psr4ConstructorMethod->stmts[0]; + $stmt = $classMethodStmts[0]; if (! $stmt instanceof Expression) { return null; } diff --git a/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php b/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php index a32a19c645f..501f464e0ca 100644 --- a/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php +++ b/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php @@ -4,6 +4,7 @@ namespace Rector\Php70\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -14,7 +15,6 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\Php70\EregToPcreTransformer; use Rector\Rector\AbstractRector; @@ -154,11 +154,11 @@ private function processSplitLimitArgument(FuncCall $funcCall, string $functionN } // 3rd argument - $limit, 0 → 1 - if (! $funcCall->args[2]->value instanceof LNumber) { + if (! $funcCall->args[2]->value instanceof Int_) { return; } - /** @var LNumber $limitNumberNode */ + /** @var Int_ $limitNumberNode */ $limitNumberNode = $funcCall->args[2]->value; if ($limitNumberNode->value !== 0) { return; @@ -171,7 +171,7 @@ private function createTernaryWithStrlenOfFirstMatch(FuncCall $funcCall): Ternar { $thirdArg = $funcCall->getArgs()[2]; - $arrayDimFetch = new ArrayDimFetch($thirdArg->value, new LNumber(0)); + $arrayDimFetch = new ArrayDimFetch($thirdArg->value, new Int_(0)); $strlenFuncCall = $this->nodeFactory->createFuncCall('strlen', [$arrayDimFetch]); return new Ternary($funcCall, $strlenFuncCall, $this->nodeFactory->createFalse()); diff --git a/rules/Php70/Rector/FuncCall/MultiDirnameRector.php b/rules/Php70/Rector/FuncCall/MultiDirnameRector.php index 54faedadfe6..74291c975a2 100644 --- a/rules/Php70/Rector/FuncCall/MultiDirnameRector.php +++ b/rules/Php70/Rector/FuncCall/MultiDirnameRector.php @@ -4,10 +4,10 @@ namespace Rector\Php70\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -66,7 +66,7 @@ public function refactor(Node $node): ?Node } $node->args[0] = $lastFuncCallNode->args[0]; - $node->args[1] = new Arg(new LNumber($this->nestingLevel)); + $node->args[1] = new Arg(new Int_($this->nestingLevel)); return $node; } @@ -98,11 +98,11 @@ private function matchNestedDirnameFuncCall(FuncCall $funcCall): ?FuncCall // dirname($path, ); if (count($args) === 2) { - if (! $args[1]->value instanceof LNumber) { + if (! $args[1]->value instanceof Int_) { return null; } - /** @var LNumber $levelNumber */ + /** @var Int_ $levelNumber */ $levelNumber = $args[1]->value; $this->nestingLevel += $levelNumber->value; diff --git a/rules/Php70/Rector/FuncCall/RandomFunctionRector.php b/rules/Php70/Rector/FuncCall/RandomFunctionRector.php index 824bb7b6317..898c1ccc538 100644 --- a/rules/Php70/Rector/FuncCall/RandomFunctionRector.php +++ b/rules/Php70/Rector/FuncCall/RandomFunctionRector.php @@ -4,11 +4,11 @@ namespace Rector\Php70\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -68,7 +68,7 @@ public function refactor(Node $node): FuncCall|null if ($newFunctionName === 'random_int') { $args = $node->getArgs(); if ($args === []) { - $node->args[0] = new Arg(new LNumber(0)); + $node->args[0] = new Arg(new Int_(0)); $node->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax')); } elseif (count($args) === 2) { $minValue = $this->valueResolver->getValue($args[0]->value); diff --git a/rules/Php70/Rector/List_/EmptyListRector.php b/rules/Php70/Rector/List_/EmptyListRector.php index 7056c825811..4eda277099d 100644 --- a/rules/Php70/Rector/List_/EmptyListRector.php +++ b/rules/Php70/Rector/List_/EmptyListRector.php @@ -5,7 +5,7 @@ namespace Rector\Php70\Rector\List_; use PhpParser\Node; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Variable; use Rector\Rector\AbstractRector; diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index c9fc03e55e5..de1b1f1f93f 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -4,14 +4,14 @@ namespace Rector\Php70\Rector\MethodCall; +use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Stmt\Class_; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\Php\PhpMethodReflection; use Rector\Enum\ObjectReference; @@ -122,8 +122,8 @@ private function processThisToStatic(Class_ $class, ClassReflection $classReflec $class, $classReflection ): null|StaticCall|int { - if ($subNode instanceof Encapsed) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + if ($subNode instanceof InterpolatedString) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof MethodCall) { diff --git a/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php b/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php index bcbc69297f3..2590a75baf2 100644 --- a/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php +++ b/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php @@ -99,7 +99,7 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal return null; } - if ($isset->vars === null) { + if ($isset->vars === []) { return null; } diff --git a/rules/Php71/Rector/Assign/AssignArrayToStringRector.php b/rules/Php71/Rector/Assign/AssignArrayToStringRector.php index 8243c4278ff..2cb32b54bde 100644 --- a/rules/Php71/Rector/Assign/AssignArrayToStringRector.php +++ b/rules/Php71/Rector/Assign/AssignArrayToStringRector.php @@ -4,6 +4,7 @@ namespace Rector\Php71\Rector\Assign; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Property; -use PhpParser\NodeTraverser; use PHPStan\Type\UnionType; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\PhpParser\NodeFinder\PropertyFetchFinder; @@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node $node->stmts, function (Node $subNode) use (&$hasChanged, $node): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($subNode instanceof Assign) { @@ -195,7 +195,7 @@ private function findSameNamedVariableAssigns( if ($this->isReAssignedAsArray($node, $variableName, $variable)) { $assignedArrayDimFetches = []; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node->var instanceof ArrayDimFetch) { diff --git a/rules/Php71/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php b/rules/Php71/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php index 30320f714ba..04c65a3fb7a 100644 --- a/rules/Php71/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php +++ b/rules/Php71/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php @@ -4,6 +4,8 @@ namespace Rector\Php71\Rector\BinaryOp; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp; @@ -11,8 +13,6 @@ use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\MagicConst\Line; use PhpParser\Node\Scalar\String_; use PHPStan\Type\Constant\ConstantStringType; @@ -105,8 +105,8 @@ public function refactor(Node $node): ?Node $node->left = $this->nodeTypeResolver->getNativeType($node->right) ->isInteger() ->yes() - ? new LNumber(0) - : new DNumber(0); + ? new Int_(0) + : new Float_(0); return $node; } @@ -117,8 +117,8 @@ public function refactor(Node $node): ?Node $node->right = $this->nodeTypeResolver->getNativeType($node->left) ->isInteger() ->yes() - ? new LNumber(0) - : new DNumber(0); + ? new Int_(0) + : new Float_(0); return $node; } diff --git a/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php b/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php index 71d6e02022b..f9d30d5cb69 100644 --- a/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php +++ b/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php @@ -147,8 +147,8 @@ private function resolveMaximumAllowedParameterCount( MethodReflection | FunctionReflection $functionLikeReflection ): int { $parameterCounts = [0]; - foreach ($functionLikeReflection->getVariants() as $parametersAcceptor) { - $parameterCounts[] = count($parametersAcceptor->getParameters()); + foreach ($functionLikeReflection->getVariants() as $variant) { + $parameterCounts[] = count($variant->getParameters()); } return max($parameterCounts); diff --git a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php index 51374dfef34..842fc270d31 100644 --- a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php +++ b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php @@ -4,19 +4,19 @@ namespace Rector\Php72\NodeFactory; +use PhpParser\Node\ClosureUse; +use PhpParser\Node\Scalar\Int_; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\ComplexType; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\ClosureUse; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\NullableType; use PhpParser\Node\Param; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; @@ -106,7 +106,7 @@ public function createAnonymousFunctionFromExpr(Expr $expr): ?Closure $matchesVariable = new Variable('matches'); - return new ArrayDimFetch($matchesVariable, new LNumber((int) $match['number'])); + return new ArrayDimFetch($matchesVariable, new Int_((int) $match['number'])); }); $anonymousFunction->stmts[] = new Return_($stmt); diff --git a/rules/Php72/Rector/Assign/ListEachRector.php b/rules/Php72/Rector/Assign/ListEachRector.php index facd181feb0..50192f9e8a2 100644 --- a/rules/Php72/Rector/Assign/ListEachRector.php +++ b/rules/Php72/Rector/Assign/ListEachRector.php @@ -5,7 +5,7 @@ namespace Rector\Php72\Rector\Assign; use PhpParser\Node; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; diff --git a/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php b/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php index 0a988254b3a..36dc2f12d80 100644 --- a/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php +++ b/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php @@ -4,6 +4,7 @@ namespace Rector\Php72\Rector\FuncCall; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -13,7 +14,6 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Param; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; @@ -154,7 +154,7 @@ private function createParamsFromString(Expr $expr): array */ private function parseStringToBody(Expr $expr): array { - if (! $expr instanceof String_ && ! $expr instanceof Encapsed && ! $expr instanceof Concat) { + if (! $expr instanceof String_ && ! $expr instanceof InterpolatedString && ! $expr instanceof Concat) { // special case of code elsewhere return [$this->createEval($expr)]; } diff --git a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php index 3b7afd83ecf..2113f4d435a 100644 --- a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php +++ b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php @@ -4,13 +4,13 @@ namespace Rector\Php72\Rector\FuncCall; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Stmt\Class_; -use PhpParser\NodeTraverser; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|Ternary { if ($node instanceof Ternary) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof FuncCall) { diff --git a/rules/Php72/Rector/Unset_/UnsetCastRector.php b/rules/Php72/Rector/Unset_/UnsetCastRector.php index 923ba05d6f1..9c943b0ce0e 100644 --- a/rules/Php72/Rector/Unset_/UnsetCastRector.php +++ b/rules/Php72/Rector/Unset_/UnsetCastRector.php @@ -4,12 +4,12 @@ namespace Rector\Php72\Rector\Unset_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Cast\Unset_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Expression; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -67,7 +67,7 @@ public function refactor(Node $node): int|null|Node return null; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $this->nodeFactory->createNull(); diff --git a/rules/Php72/Rector/While_/WhileEachToForeachRector.php b/rules/Php72/Rector/While_/WhileEachToForeachRector.php index 030ae93c260..aeb07c38731 100644 --- a/rules/Php72/Rector/While_/WhileEachToForeachRector.php +++ b/rules/Php72/Rector/While_/WhileEachToForeachRector.php @@ -5,7 +5,7 @@ namespace Rector\Php72\Rector\While_; use PhpParser\Node; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\While_; @@ -114,7 +114,7 @@ public function refactor(Node $node): ?Node $isTrailingCommaLast = true; } - $foreach = new Foreach_($foreachedExpr, $arrayItem, [ + $foreach = new Foreach_($foreachedExpr, $arrayItem->value, [ 'stmts' => $node->stmts, ]); diff --git a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php index 5b6e5b07e1b..9b278ccb44b 100644 --- a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php +++ b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php @@ -4,13 +4,13 @@ namespace Rector\Php73\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; @@ -116,7 +116,7 @@ private function shouldSkipFuncCall(FuncCall $funcCall): bool return true; } - if ($funcCall->args === null) { + if ($funcCall->args === []) { return true; } @@ -157,7 +157,7 @@ private function processJsonDecode(FuncCall $funcCall): ?FuncCall } if (! isset($funcCall->args[2])) { - $funcCall->args[2] = new Arg(new LNumber(512)); + $funcCall->args[2] = new Arg(new Int_(512)); } $this->hasChanged = true; diff --git a/rules/Php73/Rector/FuncCall/SetCookieRector.php b/rules/Php73/Rector/FuncCall/SetCookieRector.php index b0cd83e32f8..27d6ff9b5ac 100644 --- a/rules/Php73/Rector/FuncCall/SetCookieRector.php +++ b/rules/Php73/Rector/FuncCall/SetCookieRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; diff --git a/rules/Php73/Rector/FuncCall/StringifyStrNeedlesRector.php b/rules/Php73/Rector/FuncCall/StringifyStrNeedlesRector.php index 7e4a5f583cd..246c1758d5d 100644 --- a/rules/Php73/Rector/FuncCall/StringifyStrNeedlesRector.php +++ b/rules/Php73/Rector/FuncCall/StringifyStrNeedlesRector.php @@ -4,11 +4,11 @@ namespace Rector\Php73\Rector\FuncCall; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Cast\String_; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\Encapsed; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -91,7 +91,7 @@ public function refactor(Node $node): ?Node return null; } - if ($needleArgValue instanceof Encapsed) { + if ($needleArgValue instanceof InterpolatedString) { return null; } diff --git a/rules/Php74/Guard/MakePropertyTypedGuard.php b/rules/Php74/Guard/MakePropertyTypedGuard.php index 9367349bf28..61ca8af9406 100644 --- a/rules/Php74/Guard/MakePropertyTypedGuard.php +++ b/rules/Php74/Guard/MakePropertyTypedGuard.php @@ -4,6 +4,7 @@ namespace Rector\Php74\Guard; +use PhpParser\Node; use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\ClassReflection; @@ -16,7 +17,7 @@ public function __construct( public function isLegal(Property $property, ClassReflection $classReflection, bool $inlinePublic = true): bool { - if ($property->type !== null) { + if ($property->type instanceof Node) { return false; } diff --git a/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php b/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php index 43ad33101cc..790b4c4008c 100644 --- a/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php +++ b/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\ClosureUse; +use PhpParser\Node\ClosureUse; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Return_; use Rector\PhpParser\Comparing\NodeComparator; diff --git a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php index 848d9638c31..9e3bed6a019 100644 --- a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php +++ b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php @@ -77,9 +77,9 @@ private function isFollowedByCurlyBracket(File $file, ArrayDimFetch $arrayDimFet $oldTokens = $file->getOldTokens(); $endTokenPost = $arrayDimFetch->getEndTokenPos(); - if (isset($oldTokens[$endTokenPost]) && $oldTokens[$endTokenPost] === '}') { - $startTokenPost = $arrayDimFetch->getStartTokenPos(); - return ! (isset($oldTokens[$startTokenPost][1]) && $oldTokens[$startTokenPost][1] === '${'); + if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === '}') { + $startTokenPos = $arrayDimFetch->getStartTokenPos(); + return ! (isset($oldTokens[$startTokenPos]) && (string) $oldTokens[$startTokenPos] === '${'); } return false; diff --git a/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php b/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php index d2f6901d314..08140eb2e42 100644 --- a/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php +++ b/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php @@ -4,10 +4,10 @@ namespace Rector\Php74\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -67,7 +67,7 @@ public function refactor(Node $node): ?Node } $node->args[3] = $node->args[2]; - $node->args[2] = new Arg(new LNumber(0)); + $node->args[2] = new Arg(new Int_(0)); return $node; } diff --git a/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php b/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php index ff396964ac7..21c26767ce5 100644 --- a/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php +++ b/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php @@ -4,13 +4,13 @@ namespace Rector\Php74\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -92,13 +92,13 @@ private function warpInNumberFormatFuncCall(FuncCall $funcCall, Expr $expr): Fun { $roundFuncCall = $this->nodeFactory->createFuncCall( 'round', - [$expr, new LNumber(2), new ConstFetch(new Name('PHP_ROUND_HALF_ODD'))] + [$expr, new Int_(2), new ConstFetch(new Name('PHP_ROUND_HALF_ODD'))] ); $funcCall->name = new Name('number_format'); $funcCall->args = [ new Arg($roundFuncCall), - new Arg(new LNumber(2)), + new Arg(new Int_(2)), new Arg(new String_('.')), new Arg(new String_('')), ]; diff --git a/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php b/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php index fa8f510cd6e..e711d087a69 100644 --- a/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php +++ b/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php @@ -4,9 +4,9 @@ namespace Rector\Php74\Rector\LNumber; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; @@ -95,11 +95,11 @@ public function run() */ public function getNodeTypes(): array { - return [LNumber::class, DNumber::class]; + return [Int_::class, Float_::class]; } /** - * @param LNumber|DNumber $node + * @param Int_|Float_ $node */ public function refactor(Node $node): ?Node { @@ -138,7 +138,7 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::LITERAL_SEPARATOR; } - private function shouldSkip(LNumber | DNumber $node, mixed $rawValue): bool + private function shouldSkip(Int_ | Float_ $node, mixed $rawValue): bool { if (! is_string($rawValue)) { return true; @@ -154,7 +154,7 @@ private function shouldSkip(LNumber | DNumber $node, mixed $rawValue): bool } $kind = $node->getAttribute(AttributeKey::KIND); - if (in_array($kind, [LNumber::KIND_BIN, LNumber::KIND_OCT, LNumber::KIND_HEX], true)) { + if (in_array($kind, [Int_::KIND_BIN, Int_::KIND_OCT, Int_::KIND_HEX], true)) { return true; } diff --git a/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php b/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php index 1a91f990919..bc21fadc7f2 100644 --- a/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php +++ b/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php @@ -95,7 +95,7 @@ public function provideMinPhpVersion(): int private function shouldSkipProperty(Property $property, Class_ $class): bool { - if ($property->type === null) { + if (!$property->type instanceof Node) { return true; } diff --git a/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php b/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php index 6140cfc51d7..2ba0a19ba1a 100644 --- a/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php +++ b/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php @@ -66,7 +66,7 @@ public function refactor(Node $node): ?Node } // re-print with brackets - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $node->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null); return $node; } diff --git a/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php b/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php index 3dddc841d36..a96d9d530ca 100644 --- a/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php +++ b/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php @@ -15,8 +15,8 @@ public function isParenthesized(File $file, Ternary $ternary): bool $startTokenPos = $ternary->getStartTokenPos(); $endTokenPos = $ternary->getEndTokenPos(); - $hasOpenParentheses = isset($oldTokens[$startTokenPos]) && $oldTokens[$startTokenPos] === '('; - $hasCloseParentheses = isset($oldTokens[$endTokenPos]) && $oldTokens[$endTokenPos] === ')'; + $hasOpenParentheses = isset($oldTokens[$startTokenPos]) && (string) $oldTokens[$startTokenPos] === '('; + $hasCloseParentheses = isset($oldTokens[$endTokenPos]) && (string) $oldTokens[$endTokenPos] === ')'; return $hasOpenParentheses || $hasCloseParentheses; } diff --git a/rules/Php80/DocBlock/PropertyPromotionDocBlockMerger.php b/rules/Php80/DocBlock/PropertyPromotionDocBlockMerger.php index c318b7ffd89..97825b8bd10 100644 --- a/rules/Php80/DocBlock/PropertyPromotionDocBlockMerger.php +++ b/rules/Php80/DocBlock/PropertyPromotionDocBlockMerger.php @@ -4,6 +4,7 @@ namespace Rector\Php80\DocBlock; +use PhpParser\Node; use PhpParser\Comment; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; @@ -68,7 +69,7 @@ public function decorateParamWithPropertyPhpDocInfo( $param->setAttribute(AttributeKey::PHP_DOC_INFO, $propertyPhpDocInfo); // make sure the docblock is useful - if ($param->type === null) { + if (!$param->type instanceof Node) { $varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode(); if (! $varTagValueNode instanceof VarTagValueNode) { return; diff --git a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php index 5f1f7a1a890..93eef0e5063 100644 --- a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php +++ b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php @@ -4,13 +4,13 @@ namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeNameResolver\NodeNameResolver; use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface; @@ -116,7 +116,7 @@ private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith $lNumberLength = $strncmpFuncCall->getArgs()[2] ->value; - if (! $lNumberLength instanceof LNumber) { + if (! $lNumberLength instanceof Int_) { return false; } diff --git a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php index 2fda942c273..26af4b21998 100644 --- a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php +++ b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php @@ -4,13 +4,13 @@ namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeNameResolver\NodeNameResolver; use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface; @@ -123,7 +123,7 @@ private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith $lNumberLength = $substrFuncCall->getArgs()[2] ->value; - if (! $lNumberLength instanceof LNumber) { + if (! $lNumberLength instanceof Int_) { return false; } diff --git a/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php b/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php index 9be3d1048e8..3f84b83fd5c 100644 --- a/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php +++ b/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php @@ -9,11 +9,11 @@ use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Match_; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\Node\Stmt\Throw_; use Rector\NodeNameResolver\NodeNameResolver; use Rector\Php80\Enum\MatchKind; use Rector\Php80\ValueObject\CondAndExpr; @@ -74,7 +74,7 @@ public function shouldSkipSwitch(Switch_ $switch, array $condAndExprs, ?Stmt $ne return false; } - return ! $nextStmt instanceof Throw_; + return ! ($nextStmt instanceof Expression && $nextStmt->expr instanceof Throw_); } /** diff --git a/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php b/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php index 5b2fd9fb6a0..25aa6494f26 100644 --- a/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php +++ b/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php @@ -7,7 +7,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Param; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; diff --git a/rules/Php80/NodeFactory/MatchFactory.php b/rules/Php80/NodeFactory/MatchFactory.php index 8376e271db4..785e807df89 100644 --- a/rules/Php80/NodeFactory/MatchFactory.php +++ b/rules/Php80/NodeFactory/MatchFactory.php @@ -10,8 +10,8 @@ use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_ as ThrowsStmt; use Rector\Php80\Enum\MatchKind; use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer; use Rector\Php80\ValueObject\CondAndExpr; @@ -37,8 +37,8 @@ public function createFromCondAndExprs(Expr $condExpr, array $condAndExprs, ?Stm // is default value missing? maybe it can be found in next stmt if (! $this->matchSwitchAnalyzer->hasCondsAndExprDefaultValue($condAndExprs)) { // 1. is followed by throws stmts? - if ($nextStmt instanceof ThrowsStmt) { - $throw = new Throw_($nextStmt->expr); + if ($nextStmt instanceof Expression && $nextStmt->expr instanceof Throw_) { + $throw = $nextStmt->expr; $condAndExprs[] = new CondAndExpr([], $throw, MatchKind::RETURN); $shouldRemoteNextStmt = true; diff --git a/rules/Php80/NodeResolver/SwitchExprsResolver.php b/rules/Php80/NodeResolver/SwitchExprsResolver.php index b7ffa39ca41..878ed6b20df 100644 --- a/rules/Php80/NodeResolver/SwitchExprsResolver.php +++ b/rules/Php80/NodeResolver/SwitchExprsResolver.php @@ -6,12 +6,12 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Case_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\Node\Stmt\Throw_; use Rector\Php80\Enum\MatchKind; use Rector\Php80\ValueObject\CondAndExpr; @@ -73,7 +73,9 @@ public function resolve(Switch_ $switch): array $condExprs[] = $case->cond; } - if ($expr instanceof Return_) { + if ($expr instanceof Throw_) { + $condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::THROW); + } elseif ($expr instanceof Return_) { $returnedExpr = $expr->expr; if (! $returnedExpr instanceof Expr) { return []; @@ -84,9 +86,6 @@ public function resolve(Switch_ $switch): array $condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::ASSIGN); } elseif ($expr instanceof Expr) { $condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::NORMAL); - } elseif ($expr instanceof Throw_) { - $throwExpr = new Expr\Throw_($expr->expr); - $condAndExpr[] = new CondAndExpr($condExprs, $throwExpr, MatchKind::THROW); } else { return []; } diff --git a/rules/Php80/Rector/ClassMethod/AddParamBasedOnParentClassMethodRector.php b/rules/Php80/Rector/ClassMethod/AddParamBasedOnParentClassMethodRector.php index 28afd25ad9f..d1abeb4acca 100644 --- a/rules/Php80/Rector/ClassMethod/AddParamBasedOnParentClassMethodRector.php +++ b/rules/Php80/Rector/ClassMethod/AddParamBasedOnParentClassMethodRector.php @@ -252,7 +252,7 @@ function (Node $subNode) use ($parentClassMethodParam): bool { private function resolveParamType(Param $param): null|Identifier|Name|ComplexType { - if ($param->type === null) { + if (!$param->type instanceof Node) { return null; } diff --git a/rules/Php80/Rector/Class_/StringableForToStringRector.php b/rules/Php80/Rector/Class_/StringableForToStringRector.php index d9a90544bc2..8bc6f02cb9c 100644 --- a/rules/Php80/Rector/Class_/StringableForToStringRector.php +++ b/rules/Php80/Rector/Class_/StringableForToStringRector.php @@ -4,6 +4,7 @@ namespace Rector\Php80\Rector\Class_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Cast\String_ as CastString_; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PhpParser\NodeTraverser; use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; use Rector\NodeAnalyzer\ClassAnalyzer; use Rector\PhpParser\Node\BetterNodeFinder; @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node } // add return type - if ($toStringClassMethod->returnType === null) { + if (!$toStringClassMethod->returnType instanceof Node) { $toStringClassMethod->returnType = new Identifier('string'); $this->hasChanged = true; } @@ -154,7 +154,7 @@ private function processNotStringType(ClassMethod $toStringClassMethod): void $this->traverseNodesWithCallable((array) $toStringClassMethod->stmts, function (Node $subNode): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Return_) { diff --git a/rules/Php80/Rector/Identical/StrEndsWithRector.php b/rules/Php80/Rector/Identical/StrEndsWithRector.php index f3c42615b4b..8191df820dc 100644 --- a/rules/Php80/Rector/Identical/StrEndsWithRector.php +++ b/rules/Php80/Rector/Identical/StrEndsWithRector.php @@ -4,6 +4,7 @@ namespace Rector\Php80\Rector\Identical; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -15,7 +16,6 @@ use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\UnaryMinus; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeAnalyzer\BinaryOpAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -239,7 +239,7 @@ private function isHardCodedLNumberAndString(Expr $substrOffset, Expr $needle): return false; } - if (! $substrOffset->expr instanceof LNumber) { + if (! $substrOffset->expr instanceof Int_) { return false; } diff --git a/rules/Php80/Rector/NotIdentical/StrContainsRector.php b/rules/Php80/Rector/NotIdentical/StrContainsRector.php index 550f10f4f6e..3301112bf30 100644 --- a/rules/Php80/Rector/NotIdentical/StrContainsRector.php +++ b/rules/Php80/Rector/NotIdentical/StrContainsRector.php @@ -4,6 +4,7 @@ namespace Rector\Php80\Rector\NotIdentical; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -14,7 +15,6 @@ use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -159,7 +159,7 @@ private function matchIdenticalOrNotIdenticalToFalse(Identical | NotIdentical | private function isIntegerZero(Expr $expr): bool { - if (! $expr instanceof LNumber) { + if (! $expr instanceof Int_) { return false; } diff --git a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php index adc240d5789..5e9705c5a5a 100644 --- a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php +++ b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php @@ -154,6 +154,14 @@ public function refactor(Node $node): ?Node continue; } + foreach ($match->arms as $arm) { + if ($arm->conds === null) { + continue; + } + + $arm->conds = array_values($arm->conds); + } + $node->stmts[$key] = $isReturn ? new Return_($match) : new Expression($match); $this->mirrorComments($node->stmts[$key], $stmt); $hasChanged = true; diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index ce6b31d4664..989306c7163 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -6,7 +6,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\New_; use PhpParser\Node\Name\FullyQualified; use Rector\NodeAnalyzer\ExprAnalyzer; diff --git a/rules/Php81/NodeFactory/EnumFactory.php b/rules/Php81/NodeFactory/EnumFactory.php index f7c26db7ed7..fe835ea1031 100644 --- a/rules/Php81/NodeFactory/EnumFactory.php +++ b/rules/Php81/NodeFactory/EnumFactory.php @@ -4,12 +4,12 @@ namespace Rector\Php81\NodeFactory; +use PhpParser\Node\Scalar\Int_; use Nette\Utils\Strings; use PhpParser\BuilderFactory; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; @@ -202,11 +202,11 @@ private function collectMappings(array $items, array $mapping): array continue; } - if (! $item->key instanceof LNumber && ! $item->key instanceof String_) { + if (! $item->key instanceof Int_ && ! $item->key instanceof String_) { continue; } - if (! $item->value instanceof LNumber && ! $item->value instanceof String_) { + if (! $item->value instanceof Int_ && ! $item->value instanceof String_) { continue; } diff --git a/rules/Php81/Rector/Array_/FirstClassCallableRector.php b/rules/Php81/Rector/Array_/FirstClassCallableRector.php index 5a6063f2490..cdff4b032d9 100644 --- a/rules/Php81/Rector/Array_/FirstClassCallableRector.php +++ b/rules/Php81/Rector/Array_/FirstClassCallableRector.php @@ -4,6 +4,7 @@ namespace Rector\Php81\Rector\Array_; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ClassConstFetch; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Property; use PhpParser\Node\VariadicPlaceholder; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; @@ -92,7 +92,7 @@ public function getNodeTypes(): array public function refactor(Node $node): int|null|StaticCall|MethodCall { if ($node instanceof Property || $node instanceof ClassConst) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } $scope = ScopeFetcher::fetch($node); diff --git a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php index b8fcd3e5925..4981e958e8a 100644 --- a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php +++ b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php @@ -4,6 +4,8 @@ namespace Rector\Php81\Rector\FuncCall; +use PhpParser\Node\Scalar\InterpolatedString; +use PHPStan\Reflection\Native\ExtendedNativeParameterReflection; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -12,13 +14,11 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\Native\NativeFunctionReflection; -use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Type\ErrorType; use PHPStan\Type\MixedType; @@ -212,7 +212,7 @@ private function processNullToStrictStringOnNodePosition( return null; } - if ($argValue instanceof Encapsed) { + if ($argValue instanceof InterpolatedString) { return null; } @@ -225,7 +225,7 @@ private function processNullToStrictStringOnNodePosition( } $parameter = $parametersAcceptor->getParameters()[$position] ?? null; - if ($parameter instanceof NativeParameterWithPhpDocsReflection && $parameter->getType() instanceof UnionType) { + if ($parameter instanceof ExtendedNativeParameterReflection && $parameter->getType() instanceof UnionType) { $parameterType = $parameter->getType(); if (! $this->isValidUnionType($parameterType)) { return null; diff --git a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php index 7e75cbc9743..f4f66c26cbc 100644 --- a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php +++ b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php @@ -4,6 +4,7 @@ namespace Rector\Php81\Rector\Property; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; @@ -150,7 +150,7 @@ private function refactorProperty(Class_ $class, Property $property, Scope $scop return null; } - if ($property->type === null) { + if (!$property->type instanceof Node) { return null; } @@ -209,7 +209,7 @@ private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $p return null; } - if ($param->type === null) { + if (!$param->type instanceof Node) { return null; } @@ -262,7 +262,7 @@ private function isPromotedPropertyAssigned(Class_ $class, Param $param): bool if ($this->nodeComparator->areNodesEqual($propertyFetch, $node->var)) { $isAssigned = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php index 74c0960d2ac..4ba5028d55e 100644 --- a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php +++ b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php @@ -4,9 +4,9 @@ namespace Rector\Php82\Rector\Encapsed; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\Encapsed; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -42,11 +42,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [Encapsed::class]; + return [InterpolatedString::class]; } /** - * @param Encapsed $node + * @param InterpolatedString $node */ public function refactor(Node $node): ?Node { @@ -64,11 +64,7 @@ public function refactor(Node $node): ?Node continue; } - if (! is_array($oldTokens[$startTokenPos])) { - continue; - } - - if ($oldTokens[$startTokenPos][1] !== '${') { + if ((string) $oldTokens[$startTokenPos] !== '${') { continue; } diff --git a/rules/Php83/Rector/ClassConst/AddTypeToConstRector.php b/rules/Php83/Rector/ClassConst/AddTypeToConstRector.php index 3aa174179d7..7f5f4e768a1 100644 --- a/rules/Php83/Rector/ClassConst/AddTypeToConstRector.php +++ b/rules/Php83/Rector/ClassConst/AddTypeToConstRector.php @@ -4,6 +4,8 @@ namespace Rector\Php83\Rector\ClassConst; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Const_; use PhpParser\Node\Expr; @@ -14,8 +16,6 @@ use PhpParser\Node\Expr\UnaryMinus; use PhpParser\Node\Expr\UnaryPlus; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; @@ -169,11 +169,11 @@ private function findValueType(Expr $expr): ?Identifier return new Identifier('string'); } - if ($expr instanceof LNumber) { + if ($expr instanceof Int_) { return new Identifier('int'); } - if ($expr instanceof DNumber) { + if ($expr instanceof Float_) { return new Identifier('float'); } diff --git a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php index f41c1bfd740..ea4c23698b7 100644 --- a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php +++ b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php @@ -14,7 +14,8 @@ use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; +use PhpParser\Node\Stmt\Expression; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use Rector\NodeAnalyzer\ClassAnalyzer; @@ -225,7 +226,7 @@ private function shouldSkipParentClassMethod(ClassReflection $parentClassReflect return true; } - if ($soleStmt instanceof Throw_) { + if ($soleStmt instanceof Expression && $soleStmt->expr instanceof Throw_) { return true; } } diff --git a/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php b/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php index 534096ed059..dbe07d7e0b2 100644 --- a/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php +++ b/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php @@ -4,11 +4,11 @@ namespace Rector\Php83\Rector\FuncCall; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\Node\InterpolatedStringPart; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\Encapsed; -use PhpParser\Node\Scalar\EncapsedStringPart; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\Rector\AbstractRector; @@ -75,10 +75,10 @@ public function refactor(Node $node): ?Node $firstArg = $args[0]->value; $secondArg = $args[1]->value; - if ($firstArg instanceof String_ && $secondArg instanceof LNumber) { + if ($firstArg instanceof String_ && $secondArg instanceof Int_) { $args[0]->value = new String_($firstArg->value . ':' . $secondArg->value); } elseif ($this->exprAnalyzer->isDynamicExpr($firstArg) && $this->exprAnalyzer->isDynamicExpr($secondArg)) { - $args[0]->value = new Encapsed([$firstArg, new EncapsedStringPart(':'), $secondArg]); + $args[0]->value = new InterpolatedString([$firstArg, new InterpolatedStringPart(':'), $secondArg]); } else { return null; } diff --git a/rules/Privatization/NodeManipulator/VisibilityManipulator.php b/rules/Privatization/NodeManipulator/VisibilityManipulator.php index 0843588aae1..2d7f10d86b8 100644 --- a/rules/Privatization/NodeManipulator/VisibilityManipulator.php +++ b/rules/Privatization/NodeManipulator/VisibilityManipulator.php @@ -4,6 +4,7 @@ namespace Rector\Privatization\NodeManipulator; +use PhpParser\Modifiers; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; @@ -39,7 +40,7 @@ public function makeNonStatic(ClassMethod | Property $node): void return; } - $node->flags -= Class_::MODIFIER_STATIC; + $node->flags -= Modifiers::STATIC; } /** @@ -51,7 +52,7 @@ public function makeNonAbstract(ClassMethod | Class_ $node): void return; } - $node->flags -= Class_::MODIFIER_ABSTRACT; + $node->flags -= Modifiers::ABSTRACT; } /** @@ -71,7 +72,7 @@ public function makeNonFinal(Class_ | ClassMethod $node): void return; } - $node->flags -= Class_::MODIFIER_FINAL; + $node->flags -= Modifiers::FINAL; } public function changeNodeVisibility(ClassMethod | Property | ClassConst $node, int $visibility): void @@ -111,7 +112,7 @@ public function makePrivate(ClassMethod | Property | ClassConst | Param $node): */ public function removeFinal(Class_ | ClassConst $node): void { - $node->flags -= Class_::MODIFIER_FINAL; + $node->flags -= Modifiers::FINAL; } public function makeReadonly(Class_ | Property | Param $node): void @@ -164,16 +165,16 @@ private function removeVisibility(ClassMethod | Property | ClassConst | Param $n } if ($node->isPublic()) { - $node->flags |= Class_::MODIFIER_PUBLIC; - $node->flags -= Class_::MODIFIER_PUBLIC; + $node->flags |= Modifiers::PUBLIC; + $node->flags -= Modifiers::PUBLIC; } if ($node->isProtected()) { - $node->flags -= Class_::MODIFIER_PROTECTED; + $node->flags -= Modifiers::PROTECTED; } if ($node->isPrivate()) { - $node->flags -= Class_::MODIFIER_PRIVATE; + $node->flags -= Modifiers::PRIVATE; } } diff --git a/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php b/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php index ec6464db347..151d9439335 100644 --- a/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php +++ b/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php @@ -4,10 +4,10 @@ namespace Rector\Removing\Rector\FuncCall; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Expression; -use PhpParser\NodeTraverser; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -65,7 +65,7 @@ public function refactor(Node $node): ?int continue; } - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return null; diff --git a/rules/Renaming/NodeManipulator/SwitchManipulator.php b/rules/Renaming/NodeManipulator/SwitchManipulator.php index 913e4faff29..ace241dcb73 100644 --- a/rules/Renaming/NodeManipulator/SwitchManipulator.php +++ b/rules/Renaming/NodeManipulator/SwitchManipulator.php @@ -4,7 +4,7 @@ namespace Rector\Renaming\NodeManipulator; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; @@ -21,12 +21,12 @@ public function removeBreakNodes(array $stmts): array continue; } - if (! $node->num instanceof LNumber || $node->num->value === 1) { + if (! $node->num instanceof Int_ || $node->num->value === 1) { unset($stmts[$key]); continue; } - $node->num = $node->num->value === 2 ? null : new LNumber($node->num->value - 1); + $node->num = $node->num->value === 2 ? null : new Int_($node->num->value - 1); } return $stmts; diff --git a/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php b/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php index 5eb6a0a1d8d..f604551ad43 100644 --- a/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php +++ b/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php @@ -214,7 +214,7 @@ private function processPositionalArg( } // int positions shouldn't have names - if ($arg->name !== null) { + if ($arg->name instanceof Identifier) { return null; } @@ -228,7 +228,7 @@ private function processNamedArg( $args = array_filter($callLike->getArgs(), static function (Arg $arg) use ( $renameFunctionLikeParamWithinCallLikeArg ): bool { - if ($arg->name === null) { + if (!$arg->name instanceof Identifier) { return false; } diff --git a/rules/Strict/NodeFactory/ExactCompareFactory.php b/rules/Strict/NodeFactory/ExactCompareFactory.php index d61ff4c6ce1..4033ede6dbf 100644 --- a/rules/Strict/NodeFactory/ExactCompareFactory.php +++ b/rules/Strict/NodeFactory/ExactCompareFactory.php @@ -4,10 +4,11 @@ namespace Rector\Strict\NodeFactory; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -19,7 +20,6 @@ use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -39,7 +39,7 @@ public function createIdenticalFalsyCompare( Expr $expr, bool $treatAsNonEmpty, bool $isOnlyString = true - ): Identical|BooleanOr|NotIdentical|BooleanNot|Instanceof_|BooleanAnd|null|FuncCall { + ): Identical|BooleanOr|NotIdentical|BooleanNot|null|FuncCall { $result = null; if ($exprType->isString()->yes()) { @@ -49,7 +49,7 @@ public function createIdenticalFalsyCompare( $result = new BooleanOr(new Identical($expr, new String_('')), new Identical($expr, new String_('0'))); } elseif ($exprType->isInteger()->yes()) { - return new Identical($expr, new LNumber(0)); + return new Identical($expr, new Int_(0)); } elseif ($exprType->isBoolean()->yes()) { return new Identical($expr, $this->nodeFactory->createFalse()); } elseif ($exprType->isArray()->yes()) { @@ -78,7 +78,7 @@ public function createNotIdenticalFalsyCompare( Expr $expr, bool $treatAsNotEmpty, bool $isOnlyString = true - ): Identical|Instanceof_|BooleanOr|NotIdentical|BooleanAnd|BooleanNot|null { + ): Identical|Instanceof_|NotIdentical|BooleanAnd|BooleanNot|null { $result = null; if ($exprType->isString()->yes()) { @@ -91,7 +91,7 @@ public function createNotIdenticalFalsyCompare( new NotIdentical($expr, new String_('0')) ); } elseif ($exprType->isInteger()->yes()) { - return new NotIdentical($expr, new LNumber(0)); + return new NotIdentical($expr, new Int_(0)); } elseif ($exprType->isArray()->yes()) { return new NotIdentical($expr, new Array_([])); } elseif (! $exprType instanceof UnionType) { diff --git a/rules/Transform/Rector/ClassMethod/WrapReturnRector.php b/rules/Transform/Rector/ClassMethod/WrapReturnRector.php index 53ff3c7f629..bff72b183d3 100644 --- a/rules/Transform/Rector/ClassMethod/WrapReturnRector.php +++ b/rules/Transform/Rector/ClassMethod/WrapReturnRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; diff --git a/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php index 7041b81d8c9..22b7e7c4797 100644 --- a/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php +++ b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php @@ -6,7 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; diff --git a/rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php b/rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php index 9669cd4c38c..ea1b031ed3a 100644 --- a/rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php +++ b/rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php @@ -4,13 +4,13 @@ namespace Rector\Transform\Rector\Scalar; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; @@ -41,7 +41,7 @@ public function getRuleDefinition(): RuleDefinition SAMPLE , [ new ScalarValueToConstFetch( - new LNumber(10), + new Int_(10), new ClassConstFetch(new FullyQualified('SomeClass'), new Identifier('FOOBAR_INT')), ), ])] @@ -50,11 +50,11 @@ public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array { - return [String_::class, DNumber::class, LNumber::class]; + return [String_::class, Float_::class, Int_::class]; } /** - * @param String_|DNumber|LNumber $node + * @param String_|Float_|Int_ $node */ public function refactor(Node $node): ConstFetch|ClassConstFetch|null { diff --git a/rules/Transform/Rector/String_/StringToClassConstantRector.php b/rules/Transform/Rector/String_/StringToClassConstantRector.php index b3854b34ca8..3584c39fbeb 100644 --- a/rules/Transform/Rector/String_/StringToClassConstantRector.php +++ b/rules/Transform/Rector/String_/StringToClassConstantRector.php @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node ); } - return $node; + return null; } /** diff --git a/rules/Transform/ValueObject/ScalarValueToConstFetch.php b/rules/Transform/ValueObject/ScalarValueToConstFetch.php index 070d4d920c0..a40a2a77236 100644 --- a/rules/Transform/ValueObject/ScalarValueToConstFetch.php +++ b/rules/Transform/ValueObject/ScalarValueToConstFetch.php @@ -4,21 +4,21 @@ namespace Rector\Transform\ValueObject; +use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; final readonly class ScalarValueToConstFetch { public function __construct( - private DNumber|String_|LNumber $scalar, + private Float_|String_|Int_ $scalar, private ConstFetch|ClassConstFetch $constFetch ) { } - public function getScalar(): DNumber|String_|LNumber + public function getScalar(): Float_|String_|Int_ { return $this->scalar; } diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index f518d4bcca3..3b32022cb4b 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\AlreadyAssignDetector; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PhpParser\NodeFinder; -use PhpParser\NodeTraverser; use PHPStan\Type\ObjectType; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeDecorator\StatementDepthAttributeDecorator; @@ -63,7 +63,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b ) use ($propertyName, &$isAssignedInConstructor, $allowConditional): ?int { if ($this->isIfElseAssign($node, $propertyName)) { $isAssignedInConstructor = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $expr = $this->matchAssignExprToPropertyName($node, $propertyName); @@ -77,7 +77,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b // is merged in assign? if ($this->isPropertyUsedInAssign($assign, $propertyName)) { $isAssignedInConstructor = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $isFirstLevelStatement = $assign->getAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT); @@ -86,7 +86,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b if ($isFirstLevelStatement !== true) { if ($allowConditional) { $isAssignedInConstructor = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; @@ -94,7 +94,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b $isAssignedInConstructor = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; }); } diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/NullTypeAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/NullTypeAssignDetector.php index bf7cecce236..751387ccd2a 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/NullTypeAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/NullTypeAssignDetector.php @@ -4,11 +4,11 @@ namespace Rector\TypeDeclaration\AlreadyAssignDetector; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\ClassLike; -use PhpParser\NodeTraverser; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer; @@ -44,7 +44,7 @@ public function detect(ClassLike $classLike, string $propertyName): bool $staticType = $this->nodeTypeResolver->getType($expr); if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($staticType)) { $needsNullType = false; - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } return null; diff --git a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php index 3e58f33e3db..2c06cd69fc6 100644 --- a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php +++ b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\Guard; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Ternary; @@ -11,7 +12,6 @@ use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\If_; -use PhpParser\NodeTraverser; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PhpParser\Node\BetterNodeFinder; @@ -42,7 +42,7 @@ function (Node $subNode) use (&$isLegal, $paramName): ?int { $paramName )) { $isLegal = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if ($subNode instanceof If_ && (bool) $this->betterNodeFinder->findFirst( @@ -53,7 +53,7 @@ function (Node $subNode) use (&$isLegal, $paramName): ?int { ) )) { $isLegal = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if ($subNode instanceof Ternary && (bool) $this->betterNodeFinder->findFirst( @@ -64,7 +64,7 @@ function (Node $subNode) use (&$isLegal, $paramName): ?int { ) )) { $isLegal = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php b/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php index 1c083618a64..139b62ec28b 100644 --- a/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php +++ b/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php @@ -12,7 +12,8 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; +use PhpParser\Node\Stmt\Expression; use PHPStan\Analyser\Scope; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeNestingScope\ValueObject\ControlStructure; @@ -87,7 +88,10 @@ private function hasReturnOrYields(ClassMethod|Function_|Closure $node): bool private function hasNeverNodesOrNeverFuncCalls(ClassMethod|Function_|Closure $node): bool { - $hasNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Throw_::class]); + $hasNeverNodes = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped( + $node, + fn (Node $subNode): bool => $subNode instanceof Expression && $subNode->expr instanceof Throw_ + ); if ($hasNeverNodes) { return true; } diff --git a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php index 75d72ae6d83..b0dcfac33f9 100644 --- a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php +++ b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\NodeManipulator; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Type\MixedType; use PHPStan\Type\TypeCombinator; @@ -88,7 +88,7 @@ private function findCurrentScopeReturn(array $stmts): ?Return_ // skip scope nesting if ($node instanceof Class_ || $node instanceof FunctionLike) { $return = null; - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Return_) { @@ -97,7 +97,7 @@ private function findCurrentScopeReturn(array $stmts): ?Return_ if (! $node->expr instanceof Variable) { $return = null; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $return = $node; @@ -121,12 +121,12 @@ private function shouldSkipParam(Param $param, array $stmts): bool ): int|null { // skip scope nesting if ($node instanceof Class_ || $node instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof AssignRef && $this->nodeNameResolver->isName($node->expr, $paramName)) { $isParamModified = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof Assign) { @@ -142,7 +142,7 @@ private function shouldSkipParam(Param $param, array $stmts): bool } $isParamModified = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; }); return $isParamModified; diff --git a/rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php b/rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php index 547b6e17116..b13c9471dfd 100644 --- a/rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php +++ b/rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php @@ -4,12 +4,12 @@ namespace Rector\TypeDeclaration\PHPStan; +use PhpParser\Node\UseItem; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; @@ -56,6 +56,9 @@ public function narrowToFullyQualifiedOrAliasedObjectType( } $className = ltrim($objectType->getClassName(), '\\'); + if (str_starts_with($objectType->getClassName(), '\\')) { + return new FullyQualifiedObjectType($className); + } if ($this->reflectionProvider->hasClass($className)) { return new FullyQualifiedObjectType($className); @@ -158,6 +161,10 @@ private function processAliasedObject( return new AliasedObjectType($alias, $fullyQualifiedName); } + if (str_starts_with($className, $alias . '\\')) { + return new AliasedObjectType($alias, $fullyQualifiedName . ltrim($className, $alias)); + } + return null; } @@ -212,19 +219,20 @@ private function matchShortenedObjectType( private function matchPartialNamespaceObjectType( string $prefix, ObjectType $objectType, - UseUse $useUse + UseItem $useItem ): ?ShortenedObjectType { + if ($objectType->getClassName() === $useItem->name->getLast()) { + return new ShortenedObjectType($objectType->getClassName(), $prefix . $useItem->name->toString()); + } + // partial namespace - if (! \str_starts_with($objectType->getClassName(), $useUse->name->getLast() . '\\')) { + if (! \str_starts_with($objectType->getClassName(), $useItem->name->getLast() . '\\')) { return null; } $classNameWithoutLastUsePart = Strings::after($objectType->getClassName(), '\\', 1); - $connectedClassName = $prefix . $useUse->name->toString() . '\\' . $classNameWithoutLastUsePart; - if (! $this->reflectionProvider->hasClass($connectedClassName)) { - return null; - } + $connectedClassName = $prefix . $useItem->name->toString() . '\\' . $classNameWithoutLastUsePart; if ($objectType->getClassName() === $connectedClassName) { return null; @@ -239,20 +247,20 @@ private function matchPartialNamespaceObjectType( private function matchClassWithLastUseImportPart( string $prefix, ObjectType $objectType, - UseUse $useUse + UseItem $useItem ): ?ObjectType { - if ($useUse->name->getLast() !== $objectType->getClassName()) { + if ($useItem->name->getLast() !== $objectType->getClassName()) { return null; } - if (! $this->reflectionProvider->hasClass($prefix . $useUse->name->toString())) { + if (! $this->reflectionProvider->hasClass($prefix . $useItem->name->toString())) { return null; } - if ($objectType->getClassName() === $prefix . $useUse->name->toString()) { + if ($objectType->getClassName() === $prefix . $useItem->name->toString()) { return new FullyQualifiedObjectType($objectType->getClassName()); } - return new ShortenedObjectType($objectType->getClassName(), $prefix . $useUse->name->toString()); + return new ShortenedObjectType($objectType->getClassName(), $prefix . $useItem->name->toString()); } } diff --git a/rules/TypeDeclaration/PhpDocParser/ParamPhpDocNodeFactory.php b/rules/TypeDeclaration/PhpDocParser/ParamPhpDocNodeFactory.php index 00be64e8d02..8b79ed8ff03 100644 --- a/rules/TypeDeclaration/PhpDocParser/ParamPhpDocNodeFactory.php +++ b/rules/TypeDeclaration/PhpDocParser/ParamPhpDocNodeFactory.php @@ -22,7 +22,8 @@ public function create(TypeNode $typeNode, Param $param): ParamTagValueNode $typeNode, $param->variadic, '$' . $this->nodeNameResolver->getName($param), - '' + '', + false ); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php index 017a7db0af4..e28cda9373d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; @@ -124,7 +124,7 @@ public function refactor(Node $node): null|ClassMethod|Function_ $node->stmts, function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithType): null|int { if ($subNode instanceof Class_ || $subNode instanceof Function_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof FuncCall) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php index 85331b767ad..b2211d2df89 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php @@ -9,7 +9,7 @@ use PhpParser\Node\Attribute; use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php index 9b9b8567bf7..924d31de9bf 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php @@ -151,7 +151,7 @@ private function refactorClassMethodWithTypehintByParameterPosition( private function refactorParameter(Param $param, AddParamTypeDeclaration $addParamTypeDeclaration): void { // already set → no change - if ($param->type !== null) { + if ($param->type instanceof Node) { $currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); if ($this->typeComparator->areTypesEqual($currentParamType, $addParamTypeDeclaration->getParamType())) { return; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php index faf28d8ae55..6cd2f690f43 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php @@ -4,12 +4,12 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use PHPStan\Type\Type; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; @@ -146,7 +146,7 @@ private function resolvePropertyStaticTypesByParamName(ClassMethod $classMethod, ): ?int { if ($node instanceof Class_ || $node instanceof Function_) { // skip anonymous classes and inner function - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Assign) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php index 4bbc6c8fdf7..475ab823cb7 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php @@ -7,7 +7,8 @@ use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; +use PhpParser\Node\Stmt\Expression; use Rector\Rector\AbstractRector; use Rector\Reflection\ClassModifierChecker; use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver; @@ -132,7 +133,7 @@ private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool } $onlyStmt = $classMethod->stmts[0] ?? null; - return $onlyStmt instanceof Throw_; + return $onlyStmt instanceof Expression && $onlyStmt->expr instanceof Throw_; } private function isNotFinalAndEmpty(ClassMethod $classMethod): bool diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php index 9888506775c..62e25daec9e 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -181,8 +181,8 @@ private function isNativeBooleanReturnTypeFuncCall(FuncCall $funcCall): bool return false; } - foreach ($functionReflection->getVariants() as $parametersAcceptorWithPhpDoc) { - if (! $parametersAcceptorWithPhpDoc->getNativeReturnType()->isBoolean()->yes()) { + foreach ($functionReflection->getVariants() as $variant) { + if (! $variant->getNativeReturnType()->isBoolean()->yes()) { return false; } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php index e8f76a2a267..4c3103135fc 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -4,12 +4,12 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\UnaryMinus; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; @@ -161,11 +161,11 @@ private function isAlwaysNumeric(array $returns): bool $epxr = $epxr->expr; } - if (! $epxr instanceof DNumber) { + if (! $epxr instanceof Float_) { $isAlwaysFloat = false; } - if (! $epxr instanceof LNumber) { + if (! $epxr instanceof Int_) { $isAlwaysInt = false; } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php index b5a5c2efaaf..35b6fce8852 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -4,11 +4,11 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr\UnaryMinus; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; @@ -94,11 +94,11 @@ public function refactor(Node $node): ?Node $expr = $expr->expr; } - if (! $expr instanceof DNumber) { + if (! $expr instanceof Float_) { $isAlwaysFloat = false; } - if (! $expr instanceof LNumber) { + if (! $expr instanceof Int_) { $isAlwaysInt = false; } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index 141b56d0560..614faff000f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -144,7 +144,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool private function shouldSkipParam(Param $param, ClassMethod $classMethod): bool { // already has type, skip - if ($param->type !== null) { + if ($param->type instanceof Node) { return true; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php index bb5a765be93..7155208898f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php @@ -108,7 +108,7 @@ public function refactor(Node $node): ?Node continue; } - if ($parentParam->type === null) { + if (!$parentParam->type instanceof Node) { continue; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index 760d5369a4c..af0ce151531 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -155,7 +155,7 @@ private function processAddArrayReturnType( $node->returnType = new Identifier('array'); // add more precise array type if suitable - if ($returnType instanceof ArrayType && $this->shouldAddReturnArrayDocType($returnType)) { + if ($this->shouldAddReturnArrayDocType($returnType)) { $this->changeReturnType($node, $returnType); } @@ -174,7 +174,7 @@ private function shouldSkip(ClassMethod|Function_|Closure $node, Scope $scope): ); } - private function changeReturnType(ClassMethod|Function_|Closure $node, ArrayType $arrayType): void + private function changeReturnType(ClassMethod|Function_|Closure $node, ArrayType|ConstantArrayType $arrayType): void { $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); @@ -263,7 +263,7 @@ private function matchArrayAssignedVariable(array $stmts): array return $variables; } - private function shouldAddReturnArrayDocType(ArrayType $arrayType): bool + private function shouldAddReturnArrayDocType(ArrayType|ConstantArrayType $arrayType): bool { if ($arrayType instanceof ConstantArrayType) { if ($arrayType->getItemType() instanceof NeverType) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index 0051809e3db..0d67b37a2be 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -26,7 +27,6 @@ use PhpParser\Node\Stmt\Echo_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -131,13 +131,13 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function &$isParamAccessedArrayDimFetch, ): int|null { if ($node instanceof Class_ || $node instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($this->shouldStop($node, $param, $paramName)) { // force set to false to avoid too early replaced $isParamAccessedArrayDimFetch = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof ArrayDimFetch) { @@ -161,7 +161,7 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function if ($variableType->isString()->yes()) { // force set to false to avoid too early replaced $isParamAccessedArrayDimFetch = false; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } // skip integer in possibly string type as string can be accessed via int diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php index 5a9794b127b..dc733bbd0df 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -147,12 +147,12 @@ private function resolveVariableConcattedFromParam( ): int|null { // skip nested class and function nodes if ($node instanceof FunctionLike || $node instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof Assign && $node->var instanceof Variable && $this->isName($node->var, $paramName)) { $variableConcatted = null; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $expr = $this->resolveAssignConcatVariable($node, $paramName); diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php index 33fcc53cd3a..d632895a0e1 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php @@ -4,9 +4,9 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -95,7 +95,7 @@ public function refactor(Node $node): ?Node foreach ($returns as $return) { // we need exact string "value" return - if (! $return->expr instanceof String_ && ! $return->expr instanceof Encapsed) { + if (! $return->expr instanceof String_ && ! $return->expr instanceof InterpolatedString) { return null; } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php index d467d0a8f7d..4269ab7f450 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php @@ -4,10 +4,10 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -134,7 +134,7 @@ private function hasAlwaysStringScalarReturn(array $returns): bool { foreach ($returns as $return) { // we need exact string "value" return - if (! $return->expr instanceof String_ && ! $return->expr instanceof Encapsed) { + if (! $return->expr instanceof String_ && ! $return->expr instanceof InterpolatedString) { return false; } } diff --git a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php index bdd7732d48a..3ec0cb6d1ad 100644 --- a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php @@ -15,7 +15,6 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; -use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\Type\ObjectType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; @@ -160,11 +159,6 @@ private function resolveEntityClassnameFromPhpDoc(Class_ $class): ?string return null; } - // we look for generic type class - if (! $extendsTagValueNode->type instanceof GenericTypeNode) { - return null; - } - $genericTypeNode = $extendsTagValueNode->type; if ($genericTypeNode->type->name !== 'EntityRepository') { return null; diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php index a4747b5d166..c983513cafe 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php @@ -91,7 +91,7 @@ public function refactor(Node $node): ?Node default => null, }; - if ($type === null) { + if (!$type instanceof Node) { continue; } @@ -147,14 +147,14 @@ private function processFunctionLike( } // int positions shouldn't have names - if ($arg->name !== null) { + if ($arg->name instanceof Identifier) { return; } } else { $args = array_filter($callLike->getArgs(), static function (Arg $arg) use ( $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration ): bool { - if ($arg->name === null) { + if (!$arg->name instanceof Identifier) { return false; } @@ -190,7 +190,7 @@ private function refactorParameter( $newParameterType = $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType(); // already set → no change - if ($param->type !== null) { + if ($param->type instanceof Node) { $currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); if ($this->typeComparator->areTypesEqual($currentParamType, $newParameterType)) { return; diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php index fdb1a6c0aee..9cd0bfe6f6c 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\Rector\FunctionLike; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; @@ -138,17 +138,17 @@ private function findCurrentScopeYieldNodes(FunctionLike $functionLike): array ) use (&$yieldNodes): ?int { // skip anonymous class and inner function if ($node instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } // skip nested scope if ($node instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof Stmt && ! $node instanceof Expression) { $yieldNodes = []; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if (! $node instanceof Yield_ && ! $node instanceof YieldFrom) { diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index d131efa6cbc..8b3e476d806 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -4,12 +4,12 @@ namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; +use PhpParser\Node\DeclareItem; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Declare_; -use PhpParser\Node\Stmt\DeclareDeclare; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; @@ -94,10 +94,10 @@ public function beforeTraverse(array $nodes): ?array return null; } - $declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1)); - $strictTypesDeclare = new Declare_([$declareDeclare]); + $declareItem = new DeclareItem(new Identifier('strict_types'), new Int_(1)); + $strictTypesDeclare = new Declare_([$declareItem]); - $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine()); + $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getStartLine()); $this->file->addRectorClassWithLine($rectorWithLineChange); if ($rootStmt instanceof FileWithoutNamespace) { diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php index 390b479ce3e..75fde24c449 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php @@ -4,12 +4,12 @@ namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; +use PhpParser\Node\DeclareItem; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Declare_; -use PhpParser\Node\Stmt\DeclareDeclare; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; @@ -98,7 +98,7 @@ public function beforeTraverse(array $nodes): ?array $strictTypesDeclare = $this->creteStrictTypesDeclare(); - $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine()); + $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getStartLine()); $this->file->addRectorClassWithLine($rectorWithLineChange); return \array_merge([$strictTypesDeclare, new Nop()], $nodes); @@ -129,7 +129,7 @@ public function configure(array $configuration): void private function creteStrictTypesDeclare(): Declare_ { - $declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1)); - return new Declare_([$declareDeclare]); + $declareItem = new DeclareItem(new Identifier('strict_types'), new Int_(1)); + return new Declare_([$declareItem]); } } diff --git a/rules/TypeDeclaration/TypeAnalyzer/PropertyTypeDefaultValueAnalyzer.php b/rules/TypeDeclaration/TypeAnalyzer/PropertyTypeDefaultValueAnalyzer.php index 2404aa10923..12a7f3cae45 100644 --- a/rules/TypeDeclaration/TypeAnalyzer/PropertyTypeDefaultValueAnalyzer.php +++ b/rules/TypeDeclaration/TypeAnalyzer/PropertyTypeDefaultValueAnalyzer.php @@ -5,7 +5,7 @@ namespace Rector\TypeDeclaration\TypeAnalyzer; use PhpParser\Node\Expr; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use PHPStan\Type\Type; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -16,14 +16,14 @@ public function __construct( ) { } - public function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType): bool + public function doesConflictWithDefaultValue(PropertyItem $propertyItem, Type $propertyType): bool { - if (! $propertyProperty->default instanceof Expr) { + if (! $propertyItem->default instanceof Expr) { return false; } // the defaults can be in conflict - $defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default); + $defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyItem->default); if ($defaultType->isArray()->yes() && $propertyType->isArray()->yes()) { return false; } diff --git a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php index a46be2bb2b2..ff3ed4399bd 100644 --- a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php +++ b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php @@ -4,6 +4,9 @@ namespace Rector\TypeDeclaration\TypeAnalyzer; +use PHPStan\Reflection\ExtendedParametersAcceptor; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -15,15 +18,12 @@ use PhpParser\Node\Name; use PhpParser\Node\NullableType; use PhpParser\Node\Scalar; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\Native\NativeFunctionReflection; use PHPStan\Reflection\Native\NativeMethodReflection; -use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Type\MixedType; use PHPStan\Type\StaticType; use PHPStan\Type\Type; @@ -66,12 +66,7 @@ public function collectStrictReturnTypes(array $returns, Scope $scope): array $returnNode = $this->resolveMethodCallReturnNode($returnedExpr); } elseif ($returnedExpr instanceof ClassConstFetch) { $returnNode = $this->resolveConstFetchReturnNode($returnedExpr, $scope); - } elseif ( - $returnedExpr instanceof Array_ - || $returnedExpr instanceof String_ - || $returnedExpr instanceof LNumber - || $returnedExpr instanceof DNumber - ) { + } elseif ($returnedExpr instanceof Array_ || $returnedExpr instanceof String_ || $returnedExpr instanceof Int_ || $returnedExpr instanceof Float_) { $returnNode = $this->resolveLiteralReturnNode($returnedExpr, $scope); } else { return []; @@ -125,7 +120,7 @@ public function resolveMethodCallReturnType(MethodCall | StaticCall | FuncCall $ if ($methodReflection instanceof NativeFunctionReflection || $methodReflection instanceof NativeMethodReflection) { $returnType = $parametersAcceptorWithPhpDocs->getReturnType(); - } elseif ($parametersAcceptorWithPhpDocs instanceof ParametersAcceptorWithPhpDocs) { + } elseif ($parametersAcceptorWithPhpDocs instanceof ExtendedParametersAcceptor) { // native return type is needed, as docblock can be false $returnType = $parametersAcceptorWithPhpDocs->getNativeReturnType(); } else { diff --git a/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php b/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php index f3c01065105..51e9b79e820 100644 --- a/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\TypeInferer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -13,7 +14,6 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Property; -use PhpParser\NodeTraverser; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; use PHPStan\Type\NullType; @@ -165,7 +165,7 @@ private function hasAssignDynamicPropertyValue(ClassLike $classLike, string $pro $assignVar = $node->var; if (! $assignVar->name instanceof Identifier) { $hasAssignDynamicPropertyValue = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; @@ -199,7 +199,7 @@ private function getAssignedExprTypes(ClassLike $classLike, string $propertyName if ($this->exprAnalyzer->isNonTypedFromParam($node->expr)) { $assignedExprTypes = []; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } $assignedExprTypes[] = $this->resolveExprStaticTypeIncludingDimFetch($node); diff --git a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php index 52f4bc64239..355071bacea 100644 --- a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; @@ -131,7 +131,7 @@ private function resolveFromParamType(Param $param, ClassMethod $classMethod, st private function resolveParamTypeToPHPStanType(Param $param): Type { - if ($param->type === null) { + if (!$param->type instanceof Node) { return new MixedType(); } @@ -174,7 +174,7 @@ function (Node $node) use ($propertyName, &$paramStaticType): ?int { $paramStaticType = $this->nodeTypeResolver->getType($node); - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } ); @@ -199,7 +199,7 @@ private function isParamNullable(Param $param): bool private function resolveFullyQualifiedOrAliasedObjectType(Param $param): ?Type { - if ($param->type === null) { + if (!$param->type instanceof Node) { return null; } @@ -235,7 +235,7 @@ private function resolveTypeFromParam( Property $property, Class_ $class ): Type { - if ($param->type === null) { + if (!$param->type instanceof Node) { return new MixedType(); } diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index 3a50a4359dd..fb00daeaa9c 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\TypeInferer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; @@ -27,10 +28,9 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Stmt\TryCatch; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use PHPStan\Reflection\ClassReflection; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PhpParser\Node\BetterNodeFinder; @@ -122,12 +122,12 @@ private function isFoundLoopControl(Do_|While_ $node): bool $node->stmts, static function (Node $subNode) use (&$isFoundLoopControl) { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($subNode instanceof Break_ || $subNode instanceof Continue_ || $subNode instanceof Goto_) { $isFoundLoopControl = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } } ); diff --git a/rules/TypeDeclaration/TypeNormalizer.php b/rules/TypeDeclaration/TypeNormalizer.php index 7cbfaef8e4a..8f5cb4c083e 100644 --- a/rules/TypeDeclaration/TypeNormalizer.php +++ b/rules/TypeDeclaration/TypeNormalizer.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration; +use PHPStan\Type\IntersectionType; use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\MixedType; @@ -39,10 +40,15 @@ public function __construct( */ public function normalizeArrayOfUnionToUnionArray(Type $type, int $arrayNesting = 1): Type { - if (! $type instanceof ArrayType) { + if (! $type->isArray()->yes()) { return $type; } + if ($type instanceof UnionType || $type instanceof IntersectionType) { + return $type; + } + + /** @var ArrayType|ConstantArrayType $type */ if ($type instanceof ConstantArrayType && $arrayNesting === 1) { return $type; } diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index 42a7b858e84..86de3b85cc7 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -111,7 +111,11 @@ public function processFile(File $file, Configuration $configuration): FileProce private function parseFileAndDecorateNodes(File $file): ?SystemError { try { - $this->parseFileNodes($file); + try { + $this->parseFileNodes($file); + } catch (ParserErrorsException) { + $this->parseFileNodes($file, false); + } } catch (ShouldNotHappenException $shouldNotHappenException) { throw $shouldNotHappenException; } catch (AnalysedCodeException $analysedCodeException) { @@ -157,19 +161,19 @@ private function printFile(File $file, Configuration $configuration, string $fil * Handle new line or space before getOriginalFileContent()); - if ($ltrimOriginalFileContent === $newContent) { + $originalFileContent = $file->getOriginalFileContent(); + if ($originalFileContent === $newContent) { return; } // handle space before rectorParser->parseFileContentToStmtsAndTokens($file->getOriginalFileContent()); + $stmtsAndTokens = $this->rectorParser->parseFileContentToStmtsAndTokens( + $file->getOriginalFileContent(), + $forNewestSupportedVersion + ); $oldStmts = $stmtsAndTokens->getStmts(); $oldTokens = $stmtsAndTokens->getTokens(); diff --git a/src/BetterPhpDocParser/PhpDocNodeVisitor/CallableTypePhpDocNodeVisitor.php b/src/BetterPhpDocParser/PhpDocNodeVisitor/CallableTypePhpDocNodeVisitor.php index 5b8a8e1e40b..cc06ea37372 100644 --- a/src/BetterPhpDocParser/PhpDocNodeVisitor/CallableTypePhpDocNodeVisitor.php +++ b/src/BetterPhpDocParser/PhpDocNodeVisitor/CallableTypePhpDocNodeVisitor.php @@ -31,7 +31,8 @@ public function enterNode(Node $node): ?Node $spacingAwareCallableTypeNode = new SpacingAwareCallableTypeNode( $node->identifier, $node->parameters, - $node->returnType + $node->returnType, + [] ); $this->attributeMirrorer->mirror($node, $spacingAwareCallableTypeNode); diff --git a/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php b/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php index d6bed6bbf97..a4898bb2fce 100644 --- a/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php +++ b/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php @@ -17,6 +17,7 @@ use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface; use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory; use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator; @@ -46,6 +47,7 @@ final class BetterPhpDocParser extends PhpDocParser * @param PhpDocNodeDecoratorInterface[] $phpDocNodeDecorators */ public function __construct( + ParserConfig $parserConfig, TypeParser $typeParser, ConstExprParser $constExprParser, private readonly TokenIteratorFactory $tokenIteratorFactory, @@ -53,23 +55,12 @@ public function __construct( private readonly PrivatesAccessor $privatesAccessor, ) { parent::__construct( + // ParserConfig + $parserConfig, // TypeParser $typeParser, // ConstExprParser $constExprParser, - // requireWhitespaceBeforeDescription - false, - // preserveTypeAliasesWithInvalidTypes - false, - // usedAttributes - [ - 'lines' => true, - 'indexes' => true, - ], - // parseDoctrineAnnotations - false, - // textBetweenTagsBelongsToDescription, default to false, exists since 1.23.0 - true ); } diff --git a/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php b/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php index 609db53a52d..2d387add00b 100644 --- a/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php +++ b/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php @@ -4,9 +4,12 @@ namespace Rector\BetterPhpDocParser\PhpDocParser; +use PHPStan\Type\ObjectType; +use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineTagValueNode; use Nette\Utils\Strings; use PhpParser\Node; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; +use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; @@ -21,6 +24,10 @@ use Rector\BetterPhpDocParser\ValueObject\DoctrineAnnotation\SilentKeyMap; use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; use Rector\BetterPhpDocParser\ValueObject\StartAndEnd; +use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType; +use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; +use Rector\TypeDeclaration\PHPStan\ObjectTypeSpecifier; use Rector\Util\StringUtils; use Webmozart\Assert\Assert; @@ -50,11 +57,18 @@ */ private const NEWLINE_ANNOTATION_FQCN_REGEX = '#\r?\n@\\\\#'; + /** + * @var string + * @see https://regex101.com/r/3zXEh7/1 + */ + private const STAR_COMMENT_REGEX = '#^\s*\*#ms'; + public function __construct( private ClassAnnotationMatcher $classAnnotationMatcher, private StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser, private TokenIteratorFactory $tokenIteratorFactory, - private AttributeMirrorer $attributeMirrorer + private AttributeMirrorer $attributeMirrorer, + private ObjectTypeSpecifier $objectTypeSpecifier ) { } @@ -201,6 +215,24 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes( continue; } + // single quoted got invalid tag, keep process + if ($phpDocChildNode->value instanceof InvalidTagValueNode) { + $name = ltrim($phpDocChildNode->name, '@'); + + $values = $phpDocChildNode->value->value; + $this->processDoctrine($currentPhpNode, $name, $phpDocChildNode, $phpDocNode, $key, $values); + } + + // needs stable correct detection of full class name + if ($phpDocChildNode->value instanceof DoctrineTagValueNode) { + $name = ltrim($phpDocChildNode->name, '@'); + + $values = implode(', ', $phpDocChildNode->value->annotation->arguments); + $this->processDoctrine($currentPhpNode, $name, $phpDocChildNode, $phpDocNode, $key, $values); + + continue; + } + if (! $phpDocChildNode->value instanceof GenericTagValueNode) { $this->processDescriptionAsSpacelessPhpDoctagNode( $phpDocNode, @@ -290,6 +322,50 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes( } } + private function processDoctrine(Node $currentPhpNode, string $name, PhpDocTagNode $phpDocTagNode, PhpDocNode $phpDocNode, mixed $key, string $values): void + { + $type = $this->objectTypeSpecifier->narrowToFullyQualifiedOrAliasedObjectType( + $currentPhpNode, + new ObjectType($name), + $currentPhpNode->getAttribute(AttributeKey::SCOPE) + ); + + $fullyQualifiedAnnotationClass = null; + + if ($type instanceof ShortenedObjectType || $type instanceof AliasedObjectType) { + $fullyQualifiedAnnotationClass = $type->getFullyQualifiedName(); + } elseif ($type instanceof ObjectType) { + $fullyQualifiedAnnotationClass = $type->getClassName(); + } + + if ($fullyQualifiedAnnotationClass === null) { + return; + } + + if ($values !== '') { + $values = Strings::replace($values, self::STAR_COMMENT_REGEX); + $values = str_starts_with($values, '(') ? str_replace("'", '"', $values) : '(' . $values . ')'; + + if ($phpDocTagNode->value instanceof DoctrineTagValueNode && $phpDocTagNode->value->description !== '') { + $values .= $phpDocTagNode->value->description; + } + } + + $genericTagValueNode = new GenericTagValueNode($values); + $startAndEnd = $phpDocTagNode->getAttribute(PhpDocAttributeKey::START_AND_END); + $genericTagValueNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd); + + $spacelessPhpDocTagNode = $this->createSpacelessPhpDocTagNode( + '@' . $name, + $genericTagValueNode, + $fullyQualifiedAnnotationClass, + $currentPhpNode + ); + + $this->attributeMirrorer->mirror($phpDocTagNode, $spacelessPhpDocTagNode); + $phpDocNode->children[$key] = $spacelessPhpDocTagNode; + } + private function processDescriptionAsSpacelessPhpDoctagNode( PhpDocNode $phpDocNode, PhpDocTagNode $phpDocTagNode, diff --git a/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php b/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php index 214c711d8dd..f1f4a76c7d2 100644 --- a/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php +++ b/src/Comments/NodeTraverser/CommentRemovingNodeTraverser.php @@ -11,8 +11,8 @@ final class CommentRemovingNodeTraverser extends NodeTraverser { public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor) { - $this->addVisitor($commentRemovingNodeVisitor); - parent::__construct(); + + $this->addVisitor($commentRemovingNodeVisitor); } } diff --git a/src/Config/RectorConfig.php b/src/Config/RectorConfig.php index 1a72c84be65..d7bc29326b4 100644 --- a/src/Config/RectorConfig.php +++ b/src/Config/RectorConfig.php @@ -31,7 +31,7 @@ final class RectorConfig extends Container { /** - * @var array, mixed[]>> + * @var array, mixed[]> */ private array $ruleConfigurations = []; diff --git a/src/CustomRules/SimpleNodeDumper.php b/src/CustomRules/SimpleNodeDumper.php index 5bdc37e68f4..d5a9917487b 100644 --- a/src/CustomRules/SimpleNodeDumper.php +++ b/src/CustomRules/SimpleNodeDumper.php @@ -4,16 +4,16 @@ namespace Rector\CustomRules; +use PhpParser\Modifiers; +use PhpParser\Node\UseItem; use PhpParser\Node; use PhpParser\Node\Expr\Include_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Scalar; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; /** * Inspired by @see \PhpParser\NodeDumper @@ -83,31 +83,31 @@ private static function isStringList(array $items): bool private static function dumpFlags(mixed $flags): string { $strs = []; - if (($flags & Class_::MODIFIER_PUBLIC) !== 0) { + if (($flags & Modifiers::PUBLIC) !== 0) { $strs[] = 'MODIFIER_PUBLIC'; } - if (($flags & Class_::MODIFIER_PROTECTED) !== 0) { + if (($flags & Modifiers::PROTECTED) !== 0) { $strs[] = 'MODIFIER_PROTECTED'; } - if (($flags & Class_::MODIFIER_PRIVATE) !== 0) { + if (($flags & Modifiers::PRIVATE) !== 0) { $strs[] = 'MODIFIER_PRIVATE'; } - if (($flags & Class_::MODIFIER_ABSTRACT) !== 0) { + if (($flags & Modifiers::ABSTRACT) !== 0) { $strs[] = 'MODIFIER_ABSTRACT'; } - if (($flags & Class_::MODIFIER_STATIC) !== 0) { + if (($flags & Modifiers::STATIC) !== 0) { $strs[] = 'MODIFIER_STATIC'; } - if (($flags & Class_::MODIFIER_FINAL) !== 0) { + if (($flags & Modifiers::FINAL) !== 0) { $strs[] = 'MODIFIER_FINAL'; } - if (($flags & Class_::MODIFIER_READONLY) !== 0) { + if (($flags & Modifiers::READONLY) !== 0) { $strs[] = 'MODIFIER_READONLY'; } @@ -186,7 +186,7 @@ private static function dumpSingleNode(Node $node): string } elseif ($key === 'type' && $node instanceof Include_) { $result .= self::dumpIncludeType($value); } elseif ($key === 'type' - && ($node instanceof Use_ || $node instanceof UseUse || $node instanceof GroupUse)) { + && ($node instanceof Use_ || $node instanceof UseItem || $node instanceof GroupUse)) { $result .= self::dumpUseType($value); } elseif (is_string($value)) { $result .= '"' . $value . '"'; diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index d3323b1fa8c..2e1e20b24ec 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -14,6 +14,7 @@ use PHPStan\PhpDoc\TypeNodeResolver; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; use PHPStan\Reflection\ReflectionProvider; use Rector\Application\ChangedNodeScopeRefresher; use Rector\Application\FileProcessor; @@ -94,6 +95,7 @@ use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\ContextNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\GlobalVariableNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\NameNodeVisitor; +use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\ReprintNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StaticVariableNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StmtKeyNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver; @@ -129,6 +131,7 @@ use Rector\PHPStanStaticTypeMapper\TypeMapper\ClosureTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ConditionalTypeForParameterMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ConditionalTypeMapper; +use Rector\PHPStanStaticTypeMapper\TypeMapper\ConstantArrayTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\FloatTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\GenericClassStringTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\HasMethodTypeMapper; @@ -235,6 +238,7 @@ final class LazyContainerFactory NameNodeVisitor::class, StaticVariableNodeVisitor::class, StmtKeyNodeVisitor::class, + ReprintNodeVisitor::class, ]; /** @@ -265,6 +269,7 @@ final class LazyContainerFactory AccessoryNonEmptyStringTypeMapper::class, AccessoryNonFalsyStringTypeMapper::class, AccessoryNumericStringTypeMapper::class, + ConstantArrayTypeMapper::class, ArrayTypeMapper::class, BooleanTypeMapper::class, CallableTypeMapper::class, @@ -666,6 +671,14 @@ static function ( ->needs('$phpDocNodeVisitors') ->giveTagged(BasePhpDocNodeVisitorInterface::class); + $rectorConfig->singleton( + ParserConfig::class, + static fn (Container $container): ParserConfig => new ParserConfig([ + 'lines' => true, + 'indexes' => true, + ]) + ); + return $rectorConfig; } diff --git a/src/NodeAnalyzer/CompactFuncCallAnalyzer.php b/src/NodeAnalyzer/CompactFuncCallAnalyzer.php index 4f0ccf0a5ab..384efb069da 100644 --- a/src/NodeAnalyzer/CompactFuncCallAnalyzer.php +++ b/src/NodeAnalyzer/CompactFuncCallAnalyzer.php @@ -6,7 +6,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; diff --git a/src/NodeAnalyzer/ExprAnalyzer.php b/src/NodeAnalyzer/ExprAnalyzer.php index 8c68a0fc7db..e9c04977b4c 100644 --- a/src/NodeAnalyzer/ExprAnalyzer.php +++ b/src/NodeAnalyzer/ExprAnalyzer.php @@ -4,9 +4,11 @@ namespace Rector\NodeAnalyzer; +use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\UnaryMinus; @@ -15,8 +17,6 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Scalar; -use PhpParser\Node\Scalar\Encapsed; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Type\MixedType; @@ -71,7 +71,7 @@ public function isDynamicExpr(Expr $expr): bool if ($expr instanceof Scalar) { // string interpolation is true, otherwise false - return $expr instanceof Encapsed; + return $expr instanceof InterpolatedString; } return ! $this->isAllowedConstFetchOrClassConstFetch($expr); @@ -128,7 +128,7 @@ private function isAllowedArrayKey(?Expr $expr): bool return true; } - return $expr instanceof LNumber; + return $expr instanceof Int_; } private function isAllowedArrayValue(Expr $expr): bool diff --git a/src/NodeAnalyzer/ParamAnalyzer.php b/src/NodeAnalyzer/ParamAnalyzer.php index 718fbf799fc..748c9675a9e 100644 --- a/src/NodeAnalyzer/ParamAnalyzer.php +++ b/src/NodeAnalyzer/ParamAnalyzer.php @@ -4,6 +4,7 @@ namespace Rector\NodeAnalyzer; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\CallLike; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeTraverser; use Rector\NodeManipulator\FuncCallManipulator; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -48,7 +48,7 @@ public function isParamUsedInClassMethod(ClassMethod $classMethod, Param $param) $param ): ?int { if ($isParamUsed) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if ($this->isUsedAsArg($node, $param)) { @@ -57,7 +57,7 @@ public function isParamUsedInClassMethod(ClassMethod $classMethod, Param $param) // skip nested anonymous class if ($node instanceof Class_ || $node instanceof Function_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($node instanceof Variable && $this->nodeComparator->areNodesEqual($node, $param->var)) { @@ -98,7 +98,7 @@ public function isNullable(Param $param): bool return false; } - if ($param->type === null) { + if (!$param->type instanceof Node) { return false; } diff --git a/src/NodeAnalyzer/TerminatedNodeAnalyzer.php b/src/NodeAnalyzer/TerminatedNodeAnalyzer.php index a8dc7c4ecff..1be143dd053 100644 --- a/src/NodeAnalyzer/TerminatedNodeAnalyzer.php +++ b/src/NodeAnalyzer/TerminatedNodeAnalyzer.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Exit_; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\ClassLike; @@ -23,7 +24,6 @@ use PhpParser\Node\Stmt\Nop; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PhpParser\Node\Stmt\Throw_; use PhpParser\Node\Stmt\TryCatch; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; @@ -33,12 +33,7 @@ final class TerminatedNodeAnalyzer /** * @var array> */ - private const TERMINATED_NODES = [Return_::class, Throw_::class]; - - /** - * @var array> - */ - private const TERMINABLE_NODES = [Throw_::class, Return_::class, Break_::class, Continue_::class]; + private const TERMINABLE_NODES = [Return_::class, Break_::class, Continue_::class]; /** * @var array> @@ -82,7 +77,7 @@ private function isTerminatedNode(Node $previousNode, Node $currentStmt): bool return true; } - if ($previousNode instanceof Expression && $previousNode->expr instanceof Exit_) { + if ($previousNode instanceof Expression && ($previousNode->expr instanceof Exit_ || $previousNode->expr instanceof Throw_)) { return true; } @@ -176,9 +171,9 @@ private function isTerminatedInLastStmts(array $stmts, Node $node): bool } if ($lastNode instanceof Expression) { - return $lastNode->expr instanceof Exit_; + return $lastNode->expr instanceof Exit_ || $lastNode->expr instanceof Throw_; } - return in_array($lastNode::class, self::TERMINATED_NODES, true); + return $lastNode instanceof Return_; } } diff --git a/src/NodeAnalyzer/VariadicAnalyzer.php b/src/NodeAnalyzer/VariadicAnalyzer.php index b120be23fc5..0ce71370199 100644 --- a/src/NodeAnalyzer/VariadicAnalyzer.php +++ b/src/NodeAnalyzer/VariadicAnalyzer.php @@ -30,9 +30,9 @@ public function hasVariadicParameters(FuncCall | StaticCall | MethodCall $call): private function hasVariadicVariant(MethodReflection | FunctionReflection $functionLikeReflection): bool { - foreach ($functionLikeReflection->getVariants() as $parametersAcceptor) { + foreach ($functionLikeReflection->getVariants() as $variant) { // can be any number of arguments → nothing to limit here - if ($parametersAcceptor->isVariadic()) { + if ($variant->isVariadic()) { return true; } } diff --git a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php index d879ae92af1..32b26d510a0 100644 --- a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php +++ b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php @@ -6,7 +6,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Scalar\MagicConst\Class_; use PhpParser\Node\Scalar\String_; @@ -174,11 +174,11 @@ private function resolveClassContextType( } $extendedMethodReflection = $classReflection->getMethod(MethodName::CONSTRUCT, $scope); - $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors( + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors( $extendedMethodReflection->getVariants() ); - foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) { + foreach ($extendedParametersAcceptor->getParameters() as $parameterReflectionWithPhpDoc) { if (! $parameterReflectionWithPhpDoc->getDefaultValue() instanceof Type) { return new MixedType(); } diff --git a/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php b/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php index 51e65646746..eb06ef724ea 100644 --- a/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php +++ b/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php @@ -4,6 +4,7 @@ namespace Rector\NodeManipulator; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -14,7 +15,6 @@ use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\NodeTraverser; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -44,7 +44,7 @@ public function findParamAssignToPropertyName(ClassMethod $classMethod, string $ (array) $classMethod->stmts, function (Node $node) use ($propertyName, &$assignedParamName): ?int { if ($node instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Assign) { @@ -65,7 +65,7 @@ function (Node $node) use ($propertyName, &$assignedParamName): ?int { $assignedParamName = $this->nodeNameResolver->getName($node->expr); - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } ); @@ -104,7 +104,7 @@ public function findAssignsToPropertyName(ClassMethod $classMethod, string $prop (array) $classMethod->stmts, function (Node $node) use ($propertyName, &$assignExprs, $paramNames): ?int { if ($node instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Assign) { diff --git a/src/NodeManipulator/PropertyFetchAssignManipulator.php b/src/NodeManipulator/PropertyFetchAssignManipulator.php index 862fdd0342e..193b0ba6256 100644 --- a/src/NodeManipulator/PropertyFetchAssignManipulator.php +++ b/src/NodeManipulator/PropertyFetchAssignManipulator.php @@ -4,6 +4,7 @@ namespace Rector\NodeManipulator; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\AssignOp; @@ -11,7 +12,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Property; -use PhpParser\NodeTraverser; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -41,7 +41,7 @@ public function isAssignedMultipleTimesInConstructor(Class_ $class, Property $pr function (Node $node) use ($propertyName, &$count): ?int { // skip anonymous classes and inner function if ($node instanceof Class_ || $node instanceof Function_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Assign && ! $node instanceof AssignOp) { @@ -55,7 +55,7 @@ function (Node $node) use ($propertyName, &$count): ?int { ++$count; if ($count === 2) { - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/src/NodeManipulator/StmtsManipulator.php b/src/NodeManipulator/StmtsManipulator.php index 1668f6997de..31fb666fb30 100644 --- a/src/NodeManipulator/StmtsManipulator.php +++ b/src/NodeManipulator/StmtsManipulator.php @@ -5,12 +5,14 @@ namespace Rector\NodeManipulator; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PhpParser\Comparing\NodeComparator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -28,12 +30,17 @@ public function __construct( /** * @param Stmt[] $stmts */ - public function getUnwrappedLastStmt(array $stmts): ?Node + public function getUnwrappedLastStmt(array $stmts): null|Expr|Stmt { + if ($stmts === []) { + return null; + } + $lastStmtKey = array_key_last($stmts); $lastStmt = $stmts[$lastStmtKey]; if ($lastStmt instanceof Expression) { + $lastStmt->expr->setAttribute(AttributeKey::COMMENTS, $lastStmt->getAttribute(AttributeKey::COMMENTS)); return $lastStmt->expr; } diff --git a/src/NodeNameResolver/NodeNameResolver.php b/src/NodeNameResolver/NodeNameResolver.php index ce4ce9abc7e..d528d575601 100644 --- a/src/NodeNameResolver/NodeNameResolver.php +++ b/src/NodeNameResolver/NodeNameResolver.php @@ -19,7 +19,7 @@ use PhpParser\Node\Stmt\Const_; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use PhpParser\Node\Stmt\Trait_; use PHPStan\Analyser\Scope; use Rector\CodingStyle\Naming\ClassNaming; @@ -94,7 +94,7 @@ public function isName(Node | array $node, string $name): bool * @return ($node is Param ? string : * ($node is ClassMethod ? string : * ($node is Property ? string : - * ($node is PropertyProperty ? string : + * ($node is PropertyItem ? string : * ($node is Trait_ ? string : * ($node is Interface_ ? string : * ($node is Const_ ? string : diff --git a/src/NodeNameResolver/NodeNameResolver/FunctionNameResolver.php b/src/NodeNameResolver/NodeNameResolver/FunctionNameResolver.php index c4a9249c6f7..db12b62de46 100644 --- a/src/NodeNameResolver/NodeNameResolver/FunctionNameResolver.php +++ b/src/NodeNameResolver/NodeNameResolver/FunctionNameResolver.php @@ -22,7 +22,7 @@ public function getNode(): string /** * @param Function_ $node */ - public function resolve(Node $node, ?Scope $scope): ?string + public function resolve(Node $node, ?Scope $scope): string { $bareName = (string) $node->name; diff --git a/src/NodeNameResolver/NodeNameResolver/NameNameResolver.php b/src/NodeNameResolver/NodeNameResolver/NameNameResolver.php index 4f88f9cdf25..41c93c43227 100644 --- a/src/NodeNameResolver/NodeNameResolver/NameNameResolver.php +++ b/src/NodeNameResolver/NodeNameResolver/NameNameResolver.php @@ -22,7 +22,7 @@ public function getNode(): string /** * @param Name $node */ - public function resolve(Node $node, ?Scope $scope): ?string + public function resolve(Node $node, ?Scope $scope): string { return $node->toString(); } diff --git a/src/NodeTypeResolver/NodeTypeResolver/ScalarTypeResolver.php b/src/NodeTypeResolver/NodeTypeResolver/ScalarTypeResolver.php index 69f2c9c92de..83dafea3562 100644 --- a/src/NodeTypeResolver/NodeTypeResolver/ScalarTypeResolver.php +++ b/src/NodeTypeResolver/NodeTypeResolver/ScalarTypeResolver.php @@ -4,12 +4,12 @@ namespace Rector\NodeTypeResolver\NodeTypeResolver; +use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\Node\InterpolatedStringPart; use PhpParser\Node; use PhpParser\Node\Scalar; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\Encapsed; -use PhpParser\Node\Scalar\EncapsedStringPart; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\MagicConst; use PhpParser\Node\Scalar\String_; use PHPStan\Type\Constant\ConstantFloatType; @@ -35,27 +35,27 @@ public function getNodeClasses(): array public function resolve(Node $node): Type { - if ($node instanceof DNumber) { - return new ConstantFloatType((float) $node->value); + if ($node instanceof Float_) { + return new ConstantFloatType($node->value); } if ($node instanceof String_) { - return new ConstantStringType((string) $node->value); + return new ConstantStringType($node->value); } - if ($node instanceof LNumber) { - return new ConstantIntegerType((int) $node->value); + if ($node instanceof Int_) { + return new ConstantIntegerType($node->value); } if ($node instanceof MagicConst) { return new ConstantStringType($node->getName()); } - if ($node instanceof Encapsed) { + if ($node instanceof InterpolatedString) { return new StringType(); } - if ($node instanceof EncapsedStringPart) { + if ($node instanceof InterpolatedStringPart) { return new ConstantStringType($node->value); } diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php index b711057aba0..59fdc61cd4f 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php @@ -4,11 +4,11 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Return_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; @@ -40,7 +40,7 @@ public function enterNode(Node $node): ?Node $stmts, static function (Node $node): int|null|Node { if ($node instanceof Class_ || $node instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Return_) { diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php index b2118eb00e2..335feffd4cc 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php @@ -74,7 +74,9 @@ private function resolveParamIsByRefAttribute(FunctionLike $functionLike, array foreach ($functionLike->getParams() as $param) { if ($param->byRef && $param->var instanceof Variable && ! $param->var->name instanceof Expr) { $param->var->setAttribute(AttributeKey::IS_BYREF_VAR, true); - $byRefVariableNames[] = $param->var->name; + /** @var string $paramVarName */ + $paramVarName = $param->var->name; + $byRefVariableNames[] = $paramVarName; } } @@ -94,7 +96,10 @@ private function resolveClosureUseIsByRefAttribute(FunctionLike $functionLike, a foreach ($functionLike->uses as $closureUse) { if ($closureUse->byRef && ! $closureUse->var->name instanceof Expr) { $closureUse->var->setAttribute(AttributeKey::IS_BYREF_VAR, true); - $byRefVariableNames[] = $closureUse->var->name; + + /** @var string $closureVarName */ + $closureVarName = $closureUse->var->name; + $byRefVariableNames[] = $closureVarName; } } diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index ff131c0a647..88c74c24ec7 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -4,6 +4,7 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Attribute; @@ -31,7 +32,6 @@ use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\Unset_; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; @@ -154,7 +154,7 @@ private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): v $stmts, static function (Node $subNode): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($subNode instanceof If_ || $subNode instanceof Break_) { diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php index eae8f68e670..816581321c3 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php @@ -4,13 +4,13 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Global_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -67,7 +67,7 @@ private function setIsGlobalVarAttribute(Stmt $stmt, array $globalVariableNames) $stmt, static function (Node $subNode) use ($globalVariableNames): int|null|Variable { if ($subNode instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Variable) { diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ReprintNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ReprintNodeVisitor.php new file mode 100644 index 00000000000..66669be27ab --- /dev/null +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ReprintNodeVisitor.php @@ -0,0 +1,41 @@ +getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) { + return null; + } + + if ($node instanceof BinaryOp && ! $node instanceof Coalesce) { + if ($node->left instanceof BinaryOp && $node->left->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) { + $node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + + if ($node->right instanceof BinaryOp && $node->right->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) { + $node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + + return $node; + } + + if ($node instanceof BooleanNot && $node->expr instanceof BinaryOp && $node->expr->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) { + $node->expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); + } + + return null; + } +} diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php index 8ed624618cd..4d32f4c42ac 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php @@ -4,13 +4,13 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Static_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -67,7 +67,7 @@ private function setIsStaticVarAttribute(Stmt $stmt, array $staticVariableNames) $stmt, static function (Node $subNode) use ($staticVariableNames): int|null|Variable { if ($subNode instanceof Class_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $subNode instanceof Variable) { diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 4d3abef0b80..e4ee6d788eb 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -4,11 +4,12 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope; +use PHPStan\Parser\ParserErrorsException; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\AssignOp; @@ -19,6 +20,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; @@ -211,7 +213,7 @@ public function processNodes( if ($node instanceof Foreach_) { // decorate value as well $node->valueVar->setAttribute(AttributeKey::SCOPE, $mutatingScope); - if ($node->valueVar instanceof Array_) { + if ($node->valueVar instanceof List_) { $this->processArray($node->valueVar, $mutatingScope); } @@ -328,7 +330,13 @@ private function nodeScopeResolverProcessNodes( ): void { try { $this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback); + } catch (ParserErrorsException) { + // nothing we can do more precise here as error parsing from deep internal PHPStan service with service injection we cannot reset + // in the middle of process + // fallback to fill by found scope + RectorNodeScopeResolver::processNodes($stmts, $mutatingScope); } catch (ShouldNotHappenException) { + // internal PHPStan error } } @@ -353,7 +361,7 @@ private function processAssign(Assign|AssignOp $assign, MutatingScope $mutatingS $assign->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope); } - private function processArray(Array_ $array, MutatingScope $mutatingScope): void + private function processArray(List_|Array_ $array, MutatingScope $mutatingScope): void { foreach ($array->items as $arrayItem) { if ($arrayItem instanceof ArrayItem) { diff --git a/src/NodeTypeResolver/PHPStan/Scope/RectorNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/RectorNodeScopeResolver.php new file mode 100644 index 00000000000..8db15073fee --- /dev/null +++ b/src/NodeTypeResolver/PHPStan/Scope/RectorNodeScopeResolver.php @@ -0,0 +1,33 @@ +traverseNodesWithCallable( + $stmts, + function (Node $node) use ($mutatingScope): void { + $node->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + ); + } +} \ No newline at end of file diff --git a/src/NodeTypeResolver/PHPStan/Type/TypeFactory.php b/src/NodeTypeResolver/PHPStan/Type/TypeFactory.php index 46179fa92aa..304267128f4 100644 --- a/src/NodeTypeResolver/PHPStan/Type/TypeFactory.php +++ b/src/NodeTypeResolver/PHPStan/Type/TypeFactory.php @@ -164,7 +164,21 @@ private function createUnionOrSingleType(array $types): Type return $types[0]; } - return new UnionType($types); + $unionType = new UnionType($types); + + if ($unionType->isFloat()->yes()) { + return new IntegerType(); + } + + if ($unionType->isString()->yes()) { + return new StringType(); + } + + if ($unionType->isInteger()->yes()) { + return new IntegerType(); + } + + return $unionType; } private function removeValueFromConstantType(Type $type): Type diff --git a/src/NodeTypeResolver/PHPStan/TypeHasher.php b/src/NodeTypeResolver/PHPStan/TypeHasher.php index f73f6011e4e..412f4f41624 100644 --- a/src/NodeTypeResolver/PHPStan/TypeHasher.php +++ b/src/NodeTypeResolver/PHPStan/TypeHasher.php @@ -5,7 +5,6 @@ namespace Rector\NodeTypeResolver\PHPStan; use PHPStan\Type\ArrayType; -use PHPStan\Type\ConstantType; use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; @@ -42,7 +41,7 @@ public function createTypeHash(Type $type): string return $this->resolveUniqueTypeWithClassNameHash($type); } - if ($type instanceof ConstantType) { + if ($type->isConstantValue()->yes()) { return $type::class; } diff --git a/src/NodeTypeResolver/TypeComparator/ArrayTypeComparator.php b/src/NodeTypeResolver/TypeComparator/ArrayTypeComparator.php index db87360ec63..7bc10a6b9af 100644 --- a/src/NodeTypeResolver/TypeComparator/ArrayTypeComparator.php +++ b/src/NodeTypeResolver/TypeComparator/ArrayTypeComparator.php @@ -13,7 +13,7 @@ */ final readonly class ArrayTypeComparator { - public function isSubtype(ArrayType $checkedType, ArrayType $mainType): bool + public function isSubtype(ArrayType|ConstantArrayType $checkedType, ArrayType|ConstantArrayType $mainType): bool { if (! $checkedType instanceof ConstantArrayType && ! $mainType instanceof ConstantArrayType) { return $mainType->isSuperTypeOf($checkedType) diff --git a/src/NodeTypeResolver/TypeComparator/ScalarTypeComparator.php b/src/NodeTypeResolver/TypeComparator/ScalarTypeComparator.php index e2062368eda..8298871bc4a 100644 --- a/src/NodeTypeResolver/TypeComparator/ScalarTypeComparator.php +++ b/src/NodeTypeResolver/TypeComparator/ScalarTypeComparator.php @@ -67,7 +67,7 @@ public function areDifferentScalarTypes(Type $firstType, Type $secondType): bool return $firstType::class !== $secondType::class; } - if (! $secondType->isClassStringType()->yes()) { + if (! $secondType->isClassString()->yes()) { return $firstType::class !== $secondType::class; } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php index 690b6417716..c5ca221dbe9 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php @@ -4,7 +4,7 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; +use PHPStan\PhpDocParser\Ast\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; @@ -90,7 +90,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param ArrayType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('array'); } @@ -178,7 +178,7 @@ private function createGenericArrayType(ArrayType $arrayType, bool $withKey = fa // @see https://github.com/phpstan/phpdoc-parser/blob/98a088b17966bdf6ee25c8a4b634df313d8aa531/tests/PHPStan/Parser/PhpDocParserTest.php#L2692-L2696 foreach ($genericTypes as $genericType) { - /** @var \PHPStan\PhpDocParser\Ast\Node $genericType */ + /** @var Node $genericType */ $genericType->setAttribute(self::HAS_GENERIC_TYPE_PARENT, $withKey); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php new file mode 100644 index 00000000000..0d993ed34ea --- /dev/null +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php @@ -0,0 +1,47 @@ + + */ +final readonly class ConstantArrayTypeMapper implements TypeMapperInterface +{ + public function __construct( + private PhpVersionProvider $phpVersionProvider + ) { + } + + public function getNodeClass(): string + { + return ConstantArrayType::class; + } + + /** + * @param ConstantArrayType $type + */ + public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode + { + return $type->toPhpDocNode(); + } + + public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + { + if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { + return null; + } + + return new Identifier('array'); + } +} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php index e06899b7294..c9a72e2a9ac 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Accessory\HasOffsetType; @@ -32,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param HasOffsetType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('array'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php index 7e7537e6933..500ccca6690 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Accessory\HasOffsetValueType; @@ -32,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param HasOffsetValueType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('array'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php index fe31d77e73f..9e810fd8067 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\IterableType; @@ -32,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param IterableType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('iterable'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php index ff3fa1284b7..fd1ed0793d9 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Accessory\NonEmptyArrayType; @@ -32,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param NonEmptyArrayType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('array'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php index 62c94f3403c..38b95b500ef 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Accessory\OversizedArrayType; @@ -34,7 +33,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode * @param TypeKind::* $typeKind * @param OversizedArrayType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier { return new Identifier('array'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php index 26b696443c7..9ef8b7020f0 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Name; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Type; @@ -33,7 +32,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param ParentStaticType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Name { return new Name(ObjectReference::PARENT); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/SelfObjectTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/SelfObjectTypeMapper.php index 7c70666507f..738cd579b59 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/SelfObjectTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/SelfObjectTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Name; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\Type; @@ -32,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param SelfObjectType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Name { return new Name('self'); } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php index 11f0ae2d5bf..d59ee36e44a 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php @@ -4,7 +4,6 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper; -use PhpParser\Node; use PhpParser\Node\Name; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\StaticType; @@ -45,7 +44,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param SimpleStaticType|StaticType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Name { if ($type instanceof SelfStaticType) { return new Name(ObjectReference::SELF); diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ThisTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ThisTypeMapper.php index 1f5bb96eeb9..024a37a03fe 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ThisTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ThisTypeMapper.php @@ -32,7 +32,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode /** * @param ThisType $type */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node + public function mapToPhpParserNode(Type $type, string $typeKind): Node { return new Name('self'); } diff --git a/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php b/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php index ce207800dea..746a1c9820f 100644 --- a/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php +++ b/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php @@ -6,7 +6,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; diff --git a/src/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php b/src/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php index e6407d904d3..4e50ce2384a 100644 --- a/src/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php +++ b/src/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php @@ -5,7 +5,7 @@ namespace Rector\PhpAttribute\AnnotationToAttributeMapper; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Scalar\String_; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode; @@ -40,7 +40,7 @@ public function isCandidate(mixed $value): bool /** * @param ArrayItemNode $arrayItemNode */ - public function map($arrayItemNode): Expr + public function map($arrayItemNode): ArrayItem { $valueExpr = $this->annotationToAttributeMapper->map($arrayItemNode->value); diff --git a/src/PhpAttribute/AnnotationToAttributeMapper/CurlyListNodeAnnotationToAttributeMapper.php b/src/PhpAttribute/AnnotationToAttributeMapper/CurlyListNodeAnnotationToAttributeMapper.php index e981e195c1f..0693157caa4 100644 --- a/src/PhpAttribute/AnnotationToAttributeMapper/CurlyListNodeAnnotationToAttributeMapper.php +++ b/src/PhpAttribute/AnnotationToAttributeMapper/CurlyListNodeAnnotationToAttributeMapper.php @@ -4,9 +4,9 @@ namespace Rector\PhpAttribute\AnnotationToAttributeMapper; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\ArrayItem; use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode; use Rector\PhpAttribute\AnnotationToAttributeMapper; use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface; @@ -65,7 +65,7 @@ public function map($value): Array_ continue; } - $valueExpr->key = new LNumber($arrayItemNodeKey); + $valueExpr->key = new Int_($arrayItemNodeKey); $arrayItems[] = $valueExpr; } diff --git a/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php b/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php index d9b206a9174..2348e7297f5 100644 --- a/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php +++ b/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php @@ -4,10 +4,10 @@ namespace Rector\PhpAttribute\AnnotationToAttributeMapper; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface; @@ -41,7 +41,7 @@ public function map($value): Expr // number as string to number if (is_numeric($value) && strlen((string) (int) $value) === strlen($value)) { - return LNumber::fromString($value); + return Int_::fromString($value); } if (str_contains($value, "'") && ! str_contains($value, "\n")) { diff --git a/src/PhpAttribute/AttributeArrayNameInliner.php b/src/PhpAttribute/AttributeArrayNameInliner.php index 0728442b855..b06bff0922a 100644 --- a/src/PhpAttribute/AttributeArrayNameInliner.php +++ b/src/PhpAttribute/AttributeArrayNameInliner.php @@ -4,14 +4,13 @@ namespace Rector\PhpAttribute; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; -use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node\Scalar\String_; -use Rector\Exception\NotImplementedYetException; use Webmozart\Assert\Assert; final class AttributeArrayNameInliner @@ -61,30 +60,17 @@ private function inlineArray(array $args): array { Assert::allIsAOf($args, Arg::class); - $newArgs = []; - foreach ($args as $arg) { - // matching top root array key - if ($arg->value instanceof ArrayItem) { - $arrayItem = $arg->value; - if ($arrayItem->key instanceof LNumber) { - $newArgs[] = new Arg($arrayItem->value); - } elseif ($arrayItem->key instanceof String_) { - $arrayItemString = $arrayItem->key; - $newArgs[] = new Arg($arrayItem->value, false, false, [], new Identifier($arrayItemString->value)); - } elseif (! $arrayItem->key instanceof Expr) { - // silent key - $newArgs[] = new Arg($arrayItem->value); + if ($arg->value instanceof String_ && is_numeric($arg->value->value)) { + // use equal over identical on purpose to verify if it is an integer + if ((float) $arg->value->value == (int) $arg->value->value) { + $arg->value = new Int_((int) $arg->value->value); } else { - throw new NotImplementedYetException(get_debug_type($arrayItem->key)); + $arg->value = new Float_((float) $arg->value->value); } } } - if ($newArgs !== []) { - return $newArgs; - } - return $args; } } diff --git a/src/PhpAttribute/Contract/AnnotationToAttributeMapperInterface.php b/src/PhpAttribute/Contract/AnnotationToAttributeMapperInterface.php index 984ce64bbb3..feb40065a3f 100644 --- a/src/PhpAttribute/Contract/AnnotationToAttributeMapperInterface.php +++ b/src/PhpAttribute/Contract/AnnotationToAttributeMapperInterface.php @@ -4,7 +4,7 @@ namespace Rector\PhpAttribute\Contract; -use PhpParser\Node\Expr; +use PhpParser\Node; /** * @template T as mixed @@ -16,5 +16,5 @@ public function isCandidate(mixed $value): bool; /** * @param T $value */ - public function map(mixed $value): Expr; + public function map(mixed $value): Node; } diff --git a/src/PhpAttribute/NodeFactory/AnnotationToAttributeIntegerValueCaster.php b/src/PhpAttribute/NodeFactory/AnnotationToAttributeIntegerValueCaster.php index a334125a55a..e601d772fe6 100644 --- a/src/PhpAttribute/NodeFactory/AnnotationToAttributeIntegerValueCaster.php +++ b/src/PhpAttribute/NodeFactory/AnnotationToAttributeIntegerValueCaster.php @@ -4,9 +4,10 @@ namespace Rector\PhpAttribute\NodeFactory; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; -use PhpParser\Node\Expr\ArrayItem; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\ArrayItem; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Scalar\String_; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ParameterReflection; @@ -44,11 +45,15 @@ public function castAttributeTypes(AnnotationToAttribute $annotationToAttribute, foreach ($parameterReflections as $parameterReflection) { foreach ($args as $arg) { - if (! $arg->value instanceof ArrayItem) { + if (! $arg->value instanceof Array_) { + continue; + } + + $arrayItem = current($arg->value->items) ?: null; + if (! $arrayItem instanceof ArrayItem) { continue; } - $arrayItem = $arg->value; if (! $arrayItem->key instanceof String_) { continue; } @@ -72,7 +77,7 @@ public function castAttributeTypes(AnnotationToAttribute $annotationToAttribute, continue; } - $arrayItem->value = new LNumber((int) $valueString->value); + $arrayItem->value = new Int_((int) $valueString->value); } } } @@ -103,10 +108,10 @@ private function resolveConstructorParameterReflections(ClassReflection $classRe { $extendedMethodReflection = $classReflection->getConstructor(); - $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors( + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors( $extendedMethodReflection->getVariants() ); - return $parametersAcceptorWithPhpDocs->getParameters(); + return $extendedParametersAcceptor->getParameters(); } } diff --git a/src/PhpAttribute/NodeFactory/NamedArgsFactory.php b/src/PhpAttribute/NodeFactory/NamedArgsFactory.php index 92013eaeffb..5bebae6f284 100644 --- a/src/PhpAttribute/NodeFactory/NamedArgsFactory.php +++ b/src/PhpAttribute/NodeFactory/NamedArgsFactory.php @@ -4,26 +4,16 @@ namespace Rector\PhpAttribute\NodeFactory; -use Nette\Utils\Strings; use PhpParser\BuilderHelpers; use PhpParser\Node\Arg; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; -use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Identifier; -use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; use Rector\NodeTypeResolver\Node\AttributeKey; final class NamedArgsFactory { - /** - * @see https://regex101.com/r/1bJR0J/1 - * @var string - */ - private const CLASS_CONST_REGEX = '#(?\w+)::(?\w+)#'; - /** * @param array $values * @return Arg[] @@ -33,14 +23,20 @@ public function createFromValues(array $values): array $args = []; foreach ($values as $key => $argValue) { - $expr = BuilderHelpers::normalizeValue($argValue); + $name = null; - $this->normalizeArrayWithConstFetchKey($expr); + if ($argValue instanceof ArrayItem) { + if ($argValue->key instanceof String_) { + $name = new Identifier($argValue->key->value); + } - $name = null; + $argValue = $argValue->value; + } + + $expr = BuilderHelpers::normalizeValue($argValue); // for named arguments - if (is_string($key)) { + if (!$name instanceof Identifier && is_string($key)) { $name = new Identifier($key); } @@ -69,36 +65,4 @@ private function normalizeStringDoubleQuote(Expr $expr): void $expr->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED); } - - private function normalizeArrayWithConstFetchKey(Expr $expr): void - { - if (! $expr instanceof Array_) { - return; - } - - foreach ($expr->items as $arrayItem) { - if (! $arrayItem instanceof ArrayItem) { - continue; - } - - if (! $arrayItem->key instanceof String_) { - continue; - } - - $string = $arrayItem->key; - - $match = Strings::match($string->value, self::CLASS_CONST_REGEX); - if ($match === null) { - continue; - } - - /** @var string $class */ - $class = $match['class']; - - /** @var string $constant */ - $constant = $match['constant']; - - $arrayItem->key = new ClassConstFetch(new Name($class), $constant); - } - } } diff --git a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php index e5b631243ae..f15b9d782c9 100644 --- a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php +++ b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php @@ -9,7 +9,7 @@ use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; @@ -31,8 +31,8 @@ public function __construct( private AnnotationToAttributeMapper $annotationToAttributeMapper, private AttributeNameFactory $attributeNameFactory, private NamedArgsFactory $namedArgsFactory, - private AttributeArrayNameInliner $attributeArrayNameInliner, private AnnotationToAttributeIntegerValueCaster $annotationToAttributeIntegerValueCaster, + private AttributeArrayNameInliner $attributeArrayNameInliner ) { } diff --git a/src/PhpAttribute/UseAliasNameMatcher.php b/src/PhpAttribute/UseAliasNameMatcher.php index f7f69660950..1298b3b0dfd 100644 --- a/src/PhpAttribute/UseAliasNameMatcher.php +++ b/src/PhpAttribute/UseAliasNameMatcher.php @@ -4,9 +4,9 @@ namespace Rector\PhpAttribute; +use PhpParser\Node\UseItem; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php80\Contract\ValueObject\AnnotationToAttributeInterface; use Rector\PhpAttribute\ValueObject\UseAliasMetadata; @@ -30,7 +30,7 @@ public function match( foreach ($use->uses as $useUse) { // we need to use original use statement $originalUseUseNode = $useUse->getAttribute(AttributeKey::ORIGINAL_NODE); - if (! $originalUseUseNode instanceof UseUse) { + if (! $originalUseUseNode instanceof UseItem) { continue; } diff --git a/src/PhpAttribute/ValueObject/UseAliasMetadata.php b/src/PhpAttribute/ValueObject/UseAliasMetadata.php index 124ec7a660c..74ae032466d 100644 --- a/src/PhpAttribute/ValueObject/UseAliasMetadata.php +++ b/src/PhpAttribute/ValueObject/UseAliasMetadata.php @@ -4,14 +4,14 @@ namespace Rector\PhpAttribute\ValueObject; -use PhpParser\Node\Stmt\UseUse; +use PhpParser\Node\UseItem; final readonly class UseAliasMetadata { public function __construct( private string $shortAttributeName, private string $useImportName, - private UseUse $useUse + private UseItem $useItem ) { } @@ -25,8 +25,8 @@ public function getUseImportName(): string return $this->useImportName; } - public function getUseUse(): UseUse + public function getUseUse(): UseItem { - return $this->useUse; + return $this->useItem; } } diff --git a/src/PhpDocParser/NodeVisitor/CallableNodeVisitor.php b/src/PhpDocParser/NodeVisitor/CallableNodeVisitor.php index 54b91362507..c23f6ed1dca 100644 --- a/src/PhpDocParser/NodeVisitor/CallableNodeVisitor.php +++ b/src/PhpDocParser/NodeVisitor/CallableNodeVisitor.php @@ -4,11 +4,11 @@ namespace Rector\PhpDocParser\NodeVisitor; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; final class CallableNodeVisitor extends NodeVisitorAbstract @@ -42,7 +42,7 @@ public function enterNode(Node $node): int|Node|null /** @var int|Node|null|Node[] $newNode */ $newNode = $callable($node); - if ($newNode === NodeTraverser::REMOVE_NODE) { + if ($newNode === NodeVisitor::REMOVE_NODE) { $this->nodeIdToRemove = spl_object_id($originalNode); return $originalNode; } @@ -68,7 +68,7 @@ public function leaveNode(Node $node): int|Node|array { if ($this->nodeIdToRemove !== null && $this->nodeIdToRemove === spl_object_id($node)) { $this->nodeIdToRemove = null; - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } if ($this->nodesToReturn === []) { diff --git a/src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php b/src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php index 7d33311ba27..2556f97b8be 100644 --- a/src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php +++ b/src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php @@ -137,7 +137,7 @@ private function traverseNode(Node $node): Node } elseif ($return === self::STOP_TRAVERSAL) { $this->stopTraversal = true; } elseif ($return === self::NODE_REMOVE) { - $subNode = null; + unset($subNode); continue 2; } else { throw new InvalidTraverseException( diff --git a/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php b/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php index 6b73d03bc2d..b02e3aef6e6 100644 --- a/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php +++ b/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php @@ -4,12 +4,13 @@ namespace Rector\PhpDocParser\PhpParser; -use PhpParser\Lexer\Emulative; use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; use PhpParser\ParserFactory; use PHPStan\Parser\CachedParser; use PHPStan\Parser\SimpleParser; +use PHPStan\Parser\VariadicFunctionsVisitor; +use PHPStan\Parser\VariadicMethodsVisitor; /** * Based on PHPStan-based PHP-Parser best practices: @@ -33,17 +34,16 @@ public function create(): SmartPhpParser private function createNativePhpParser(): Parser { $parserFactory = new ParserFactory(); - $lexerEmulative = new Emulative([ - 'usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'], - ]); - - return $parserFactory->create(ParserFactory::PREFER_PHP7, $lexerEmulative); + return $parserFactory->createForNewestSupportedVersion(); } private function createPHPStanParser(Parser $parser): CachedParser { $nameResolver = new NameResolver(); - $simpleParser = new SimpleParser($parser, $nameResolver); + $variadicMethodsVisitor = new VariadicMethodsVisitor(); + $variadicFunctionsVisitor = new VariadicFunctionsVisitor(); + + $simpleParser = new SimpleParser($parser, $nameResolver, $variadicMethodsVisitor, $variadicFunctionsVisitor); return new CachedParser($simpleParser, 1024); } diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 9381ae24f86..d69432dc23b 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -4,6 +4,7 @@ namespace Rector\PhpParser\Node; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Variable; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; use PhpParser\NodeFinder; -use PhpParser\NodeTraverser; use Rector\NodeAnalyzer\ClassAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -172,13 +172,13 @@ public function hasInstancesOfInFunctionLikeScoped( (array) $functionLike->stmts, static function (Node $subNode) use ($types, &$isFoundNode): ?int { if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } foreach ($types as $type) { if ($subNode instanceof $type) { $isFoundNode = true; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } } @@ -200,12 +200,12 @@ public function findReturnsScoped(ClassMethod | Function_ | Closure $functionLik (array) $functionLike->stmts, function (Node $subNode) use (&$returns): ?int { if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if ($subNode instanceof Yield_ || $subNode instanceof YieldFrom) { $returns = []; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } if ($subNode instanceof Return_) { @@ -216,8 +216,6 @@ function (Node $subNode) use (&$returns): ?int { } ); - Assert::allIsInstanceOf($returns, Return_::class); - return $returns; } @@ -248,7 +246,7 @@ public function findInstancesOfScoped(array $nodes, string|array $types): array $nodes, static function (Node $subNode) use ($types, &$foundNodes): ?int { if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } foreach ($types as $type) { @@ -304,10 +302,10 @@ function (Node $subNode) use (&$scopedNode, $foundNode, $filter): ?int { if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) { if ($foundNode instanceof $subNode && $subNode === $foundNode) { $scopedNode = $subNode; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $foundNode instanceof $subNode) { @@ -319,7 +317,7 @@ function (Node $subNode) use (&$scopedNode, $foundNode, $filter): ?int { $scopedFoundNode = $this->findFirst($subNode, $filter); if ($scopedFoundNode === $subNode) { $scopedNode = $subNode; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; diff --git a/src/PhpParser/Node/NodeFactory.php b/src/PhpParser/Node/NodeFactory.php index 10cbe21073b..1a91d97dc91 100644 --- a/src/PhpParser/Node/NodeFactory.php +++ b/src/PhpParser/Node/NodeFactory.php @@ -4,6 +4,7 @@ namespace Rector\PhpParser\Node; +use PhpParser\Modifiers; use PhpParser\Builder\Method; use PhpParser\Builder\Param as ParamBuilder; use PhpParser\Builder\Property as PropertyBuilder; @@ -13,7 +14,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; @@ -35,7 +36,6 @@ use PhpParser\Node\Param; use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use PHPStan\Type\Type; @@ -116,6 +116,12 @@ public function createArray(array $items): Array_ */ public function createArgs(array $values): array { + foreach ($values as $key => $value) { + if ($value instanceof ArrayItem) { + $values[$key] = $value->value; + } + } + return $this->builderFactory->args($values); } @@ -283,7 +289,7 @@ public function createPromotedPropertyParam(PropertyMetadata $propertyMetadata): $param = $paramBuilder->getNode(); $propertyFlags = $propertyMetadata->getFlags(); - $param->flags = $propertyFlags !== 0 ? $propertyFlags : Class_::MODIFIER_PRIVATE; + $param->flags = $propertyFlags !== 0 ? $propertyFlags : Modifiers::PRIVATE; return $param; } diff --git a/src/PhpParser/Node/Value/ValueResolver.php b/src/PhpParser/Node/Value/ValueResolver.php index ab914e5fe0d..9f7e8884862 100644 --- a/src/PhpParser/Node/Value/ValueResolver.php +++ b/src/PhpParser/Node/Value/ValueResolver.php @@ -4,6 +4,7 @@ namespace Rector\PhpParser\Node\Value; +use PHPStan\Type\Type; use PhpParser\ConstExprEvaluationException; use PhpParser\ConstExprEvaluator; use PhpParser\Node\Arg; @@ -11,13 +12,15 @@ use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; +use PhpParser\Node\InterpolatedStringPart; use PhpParser\Node\Name; +use PhpParser\Node\Scalar\MagicConst\Class_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\Constant\ConstantArrayType; +use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\ConstantScalarType; -use PHPStan\Type\ConstantType; use Rector\Enum\ObjectReference; use Rector\Exception\ShouldNotHappenException; use Rector\NodeAnalyzer\ConstFetchAnalyzer; @@ -52,7 +55,7 @@ public function isValue(Expr $expr, mixed $value): bool return $this->getValue($expr) === $value; } - public function getValue(Arg|Expr $expr, bool $resolvedClassReference = false): mixed + public function getValue(Arg|Expr|InterpolatedStringPart $expr, bool $resolvedClassReference = false): mixed { if ($expr instanceof Arg) { $expr = $expr->value; @@ -88,11 +91,7 @@ public function getValue(Arg|Expr $expr, bool $resolvedClassReference = false): } $nodeStaticType = $this->nodeTypeResolver->getType($expr); - if ($nodeStaticType instanceof ConstantType) { - return $this->resolveConstantType($nodeStaticType); - } - - return null; + return $this->resolveConstantType($nodeStaticType); } /** @@ -149,7 +148,7 @@ public function areValuesEqual(array $nodes, array $expectedValues): bool return true; } - private function resolveExprValueForConst(Expr $expr): mixed + private function resolveExprValueForConst(Expr|InterpolatedStringPart $expr): mixed { try { $constExprEvaluator = $this->getConstExprEvaluator(); @@ -157,6 +156,17 @@ private function resolveExprValueForConst(Expr $expr): mixed } catch (ConstExprEvaluationException|TypeError) { } + if ($expr instanceof Class_) { + $type = $this->nodeTypeResolver->getNativeType($expr); + if ($type instanceof ConstantStringType) { + return $type->getValue(); + } + } + + if ($expr instanceof InterpolatedStringPart) { + return $expr->value; + } + return null; } @@ -196,7 +206,6 @@ private function extractConstantArrayTypeValue(ConstantArrayType $constantArrayT { $keys = []; foreach ($constantArrayType->getKeyTypes() as $i => $keyType) { - /** @var ConstantScalarType $keyType */ $keys[$i] = $keyType->getValue(); } @@ -309,7 +318,7 @@ private function resolveClassFromSelfStaticParent(ClassConstFetch $classConstFet return $parentClassName; } - private function resolveConstantType(ConstantType $constantType): mixed + private function resolveConstantType(Type $constantType): mixed { if ($constantType instanceof ConstantArrayType) { return $this->extractConstantArrayTypeValue($constantType); diff --git a/src/PhpParser/NodeTransformer.php b/src/PhpParser/NodeTransformer.php index 67eaf38812c..a46d7ba5515 100644 --- a/src/PhpParser/NodeTransformer.php +++ b/src/PhpParser/NodeTransformer.php @@ -8,7 +8,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Yield_; @@ -75,10 +75,6 @@ public function transformArrayToYields(Array_ $array): array $yields = []; foreach ($array->items as $arrayItem) { - if (! $arrayItem instanceof ArrayItem) { - continue; - } - $yield = new Yield_($arrayItem->value, $arrayItem->key); $expression = new Expression($yield); diff --git a/src/PhpParser/Parser/InlineCodeParser.php b/src/PhpParser/Parser/InlineCodeParser.php index fefae00d0e6..b05580682b2 100644 --- a/src/PhpParser/Parser/InlineCodeParser.php +++ b/src/PhpParser/Parser/InlineCodeParser.php @@ -4,11 +4,11 @@ namespace Rector\PhpParser\Parser; +use PhpParser\Node\Scalar\InterpolatedString; use Nette\Utils\FileSystem; use Nette\Utils\Strings; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Concat; -use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; use Rector\PhpParser\Node\Value\ValueResolver; @@ -103,7 +103,7 @@ public function stringify(Expr $expr): string ); } - if ($expr instanceof Encapsed) { + if ($expr instanceof InterpolatedString) { return $this->resolveEncapsedValue($expr); } @@ -126,11 +126,11 @@ private function parseCode(string $code): array return $this->simplePhpParser->parseString($code); } - private function resolveEncapsedValue(Encapsed $encapsed): string + private function resolveEncapsedValue(InterpolatedString $interpolatedString): string { $value = ''; $isRequirePrint = false; - foreach ($encapsed->parts as $part) { + foreach ($interpolatedString->parts as $part) { $partValue = (string) $this->valueResolver->getValue($part); if (str_ends_with($partValue, "'")) { $isRequirePrint = true; @@ -140,7 +140,7 @@ private function resolveEncapsedValue(Encapsed $encapsed): string $value .= $partValue; } - $printedExpr = $isRequirePrint ? $this->betterStandardPrinter->print($encapsed) : $value; + $printedExpr = $isRequirePrint ? $this->betterStandardPrinter->print($interpolatedString) : $value; // remove " $printedExpr = trim($printedExpr, '""'); diff --git a/src/PhpParser/Parser/RectorParser.php b/src/PhpParser/Parser/RectorParser.php index 4f663a67676..359f1d74f99 100644 --- a/src/PhpParser/Parser/RectorParser.php +++ b/src/PhpParser/Parser/RectorParser.php @@ -4,16 +4,18 @@ namespace Rector\PhpParser\Parser; -use PhpParser\Lexer; use PhpParser\Node\Stmt; +use PhpParser\ParserFactory; +use PhpParser\PhpVersion; use PHPStan\Parser\Parser; use Rector\PhpParser\ValueObject\StmtsAndTokens; +use Rector\Util\Reflection\PrivatesAccessor; final readonly class RectorParser { public function __construct( - private Lexer $lexer, private Parser $parser, + private PrivatesAccessor $privatesAccessor ) { } @@ -35,10 +37,29 @@ public function parseString(string $fileContent): array return $this->parser->parseString($fileContent); } - public function parseFileContentToStmtsAndTokens(string $fileContent): StmtsAndTokens + public function parseFileContentToStmtsAndTokens(string $fileContent, bool $forNewestSupportedVersion = true): StmtsAndTokens { - $stmts = $this->parser->parseString($fileContent); - $tokens = $this->lexer->getTokens(); + if (! $forNewestSupportedVersion) { + // don't directly change PHPStan Parser service + // to avoid reuse on next file + $phpstanParser = clone $this->parser; + + $parserFactory = new ParserFactory(); + $parser = $parserFactory->createForVersion(PhpVersion::fromString('7.0')); + $this->privatesAccessor->setPrivateProperty($phpstanParser, 'parser', $parser); + + return $this->resolveStmtsAndTokens($phpstanParser, $fileContent); + } + + return $this->resolveStmtsAndTokens($this->parser, $fileContent); + } + + private function resolveStmtsAndTokens(Parser $parser, string $fileContent): StmtsAndTokens + { + $stmts = $parser->parseString($fileContent); + + $innerParser = $this->privatesAccessor->getPrivateProperty($parser, 'parser'); + $tokens = $innerParser->getTokens(); return new StmtsAndTokens($stmts, $tokens); } diff --git a/src/PhpParser/Parser/SimplePhpParser.php b/src/PhpParser/Parser/SimplePhpParser.php index 4e8c7d81f52..8b51c09a77c 100644 --- a/src/PhpParser/Parser/SimplePhpParser.php +++ b/src/PhpParser/Parser/SimplePhpParser.php @@ -22,7 +22,7 @@ public function __construct() { $parserFactory = new ParserFactory(); - $this->phpParser = $parserFactory->create(ParserFactory::ONLY_PHP7); + $this->phpParser = $parserFactory->createForNewestSupportedVersion(); $this->nodeTraverser = new NodeTraverser(); $this->nodeTraverser->addVisitor(new AssignedToNodeVisitor()); diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 4e44eb1036c..10bf4d9cbef 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -4,6 +4,8 @@ namespace Rector\PhpParser\Printer; +use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; use Nette\Utils\Strings; use PhpParser\Comment; use PhpParser\Node; @@ -18,11 +20,8 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Param; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\EncapsedStringPart; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\Nop; @@ -125,16 +124,21 @@ public function prettyPrintFile(array $stmts): string */ public function pFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace): string { - return $this->pStmts($fileWithoutNamespace->stmts, false); + return $this->pStmts($fileWithoutNamespace->stmts); } - protected function p(Node $node, $parentFormatPreserved = false): string + protected function p( + Node $node, + int $precedence = self::MAX_PRECEDENCE, + int $lhsPrecedence = self::MAX_PRECEDENCE, + bool $parentFormatPreserved = false + ): string { while ($node instanceof AlwaysRememberedExpr) { $node = $node->getExpr(); } - $content = parent::p($node, $parentFormatPreserved); + $content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved); return $node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES) === true ? ('(' . $content . ')') @@ -152,10 +156,10 @@ protected function pAttributeGroup(AttributeGroup $attributeGroup): string return $ret; } - protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string + protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $precedence, int $lhsPrecedence): string { if (! $arrowFunction->hasAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED)) { - return parent::pExpr_ArrowFunction($arrowFunction); + return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } $expr = $arrowFunction->expr; @@ -164,7 +168,7 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string $comments = $expr->getAttribute(AttributeKey::COMMENTS) ?? []; if ($comments === []) { - return parent::pExpr_ArrowFunction($arrowFunction); + return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } $indent = $this->resolveIndentSpaces(); @@ -175,15 +179,17 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string $text .= $commentText . "\n"; } - return $this->pAttrGroups($arrowFunction->attrGroups, true) - . ($arrowFunction->static ? 'static ' : '') + return $this->pPrefixOp( + ArrowFunction::class, + $this->pAttrGroups($arrowFunction->attrGroups, true) + . $this->pStatic($arrowFunction->static) . 'fn' . ($arrowFunction->byRef ? '&' : '') - . '(' . $this->pCommaSeparated($arrowFunction->params) . ')' + . '(' . $this->pMaybeMultiline($arrowFunction->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')' . ($arrowFunction->returnType instanceof Node ? ': ' . $this->p($arrowFunction->returnType) : '') . ' =>' . $text - . $indent - . $this->p($arrowFunction->expr); + . $indent, + $arrowFunction->expr, $precedence, $lhsPrecedence); } /** @@ -228,21 +234,20 @@ protected function outdent(): void /** * @param mixed[] $nodes * @param mixed[] $origNodes - * @param int|null $fixup */ protected function pArray( - array $nodes, + array $nodes, array $origNodes, int &$pos, int $indentAdjustment, - string $parentNodeType, + string $parentNodeClass, string $subNodeName, - $fixup + ?int $fixup ): ?string { // reindex positions for printer $nodes = array_values($nodes); - $content = parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $parentNodeType, $subNodeName, $fixup); + $content = parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $parentNodeClass, $subNodeName, $fixup); if ($content === null) { return $content; } @@ -271,13 +276,13 @@ protected function pSingleQuotedString(string $string): string /** * Emulates 1_000 in PHP 7.3- version */ - protected function pScalar_DNumber(DNumber $dNumber): string + protected function pScalar_Float(Float_ $float): string { - if ($this->shouldPrintNewRawValue($dNumber)) { - return (string) $dNumber->getAttribute(AttributeKey::RAW_VALUE); + if ($this->shouldPrintNewRawValue($float)) { + return (string) $float->getAttribute(AttributeKey::RAW_VALUE); } - return parent::pScalar_DNumber($dNumber); + return parent::pScalar_Float($float); } /** @@ -294,14 +299,14 @@ protected function pExpr_Closure(Closure $closure): string return $closureContent; } - return str_replace(' use(', ' use (', (string) $closureContent); + return str_replace(' use(', ' use (', $closureContent); } /** * Do not add "()" on Expressions * @see https://github.com/rectorphp/rector/pull/401#discussion_r181487199 */ - protected function pExpr_Yield(Yield_ $yield): string + protected function pExpr_Yield(Yield_ $yield, int $precedence, int $lhsPrecedence): string { if (! $yield->value instanceof Expr) { return 'yield'; @@ -344,6 +349,15 @@ protected function pExpr_Array(Array_ $array): string */ protected function pScalar_String(String_ $string): string { + if ($string->getAttribute(AttributeKey::DOC_INDENTATION) === '__REMOVED__') { + $content = parent::pScalar_String($string); + + $lines = explode("\n", $content); + $trimmedLines = array_map('ltrim', $lines); + + return implode("\n", $trimmedLines); + } + $isRegularPattern = (bool) $string->getAttribute(AttributeKey::IS_REGULAR_PATTERN, false); if (! $isRegularPattern) { return parent::pScalar_String($string); @@ -388,21 +402,29 @@ protected function pStmt_Declare(Declare_ $declare): string return Strings::replace($declareString, '#\s+#'); } - protected function pExpr_Ternary(Ternary $ternary): string + protected function pExpr_Ternary(Ternary $ternary, int $precedence, int $lhsPrecedence): string { $kind = $ternary->getAttribute(AttributeKey::KIND); if ($kind === 'wrapped_with_brackets') { - $pExprTernary = parent::pExpr_Ternary($ternary); + $pExprTernary = parent::pExpr_Ternary($ternary, $precedence, $lhsPrecedence); return '(' . $pExprTernary . ')'; } - return parent::pExpr_Ternary($ternary); + return parent::pExpr_Ternary($ternary, $precedence, $lhsPrecedence); } - protected function pScalar_EncapsedStringPart(EncapsedStringPart $encapsedStringPart): string + protected function pScalar_InterpolatedString(InterpolatedString $interpolatedString): string { - // parent throws exception, but we need to compare string - return '`' . $encapsedStringPart->value . '`'; + $content = parent::pScalar_InterpolatedString($interpolatedString); + + if ($interpolatedString->getAttribute(AttributeKey::DOC_INDENTATION) === '__REMOVED__') { + $lines = explode("\n", $content); + $trimmedLines = array_map('ltrim', $lines); + + return implode("\n", $trimmedLines); + } + + return $content; } protected function pCommaSeparated(array $nodes): string @@ -421,40 +443,17 @@ protected function pCommaSeparated(array $nodes): string return $result; } - /** - * Override parent pModifiers to set position of final and abstract modifier early, so instead of - * - * public final const MY_CONSTANT = "Hello world!"; - * - * it should be - * - * final public const MY_CONSTANT = "Hello world!"; - * - * @see https://github.com/rectorphp/rector/issues/6963 - * @see https://github.com/nikic/PHP-Parser/pull/826 - */ - protected function pModifiers(int $modifiers): string - { - return (($modifiers & Class_::MODIFIER_FINAL) !== 0 ? 'final ' : '') - . (($modifiers & Class_::MODIFIER_ABSTRACT) !== 0 ? 'abstract ' : '') - . (($modifiers & Class_::MODIFIER_PUBLIC) !== 0 ? 'public ' : '') - . (($modifiers & Class_::MODIFIER_PROTECTED) !== 0 ? 'protected ' : '') - . (($modifiers & Class_::MODIFIER_PRIVATE) !== 0 ? 'private ' : '') - . (($modifiers & Class_::MODIFIER_STATIC) !== 0 ? 'static ' : '') - . (($modifiers & Class_::MODIFIER_READONLY) !== 0 ? 'readonly ' : ''); - } - /** * Invoke re-print even if only raw value was changed. * That allows PHPStan to use int strict types, while changing the value with literal "_" */ - protected function pScalar_LNumber(LNumber $lNumber): string|int + protected function pScalar_Int(Int_ $int): string { - if ($this->shouldPrintNewRawValue($lNumber)) { - return (string) $lNumber->getAttribute(AttributeKey::RAW_VALUE); + if ($this->shouldPrintNewRawValue($int)) { + return (string) $int->getAttribute(AttributeKey::RAW_VALUE); } - return parent::pScalar_LNumber($lNumber); + return parent::pScalar_Int($int); } protected function pExpr_MethodCall(MethodCall $methodCall): string @@ -511,7 +510,7 @@ private function getIndentCharacter(): string return SimpleParameterProvider::provideStringParameter(Option::INDENT_CHAR, ' '); } - private function shouldPrintNewRawValue(LNumber|DNumber $lNumber): bool + private function shouldPrintNewRawValue(Int_|Float_ $lNumber): bool { return $lNumber->getAttribute(AttributeKey::REPRINT_RAW_VALUE) === true; } diff --git a/src/PostRector/Rector/DocblockNameImportingPostRector.php b/src/PostRector/Rector/DocblockNameImportingPostRector.php index bf6ff748e31..d60bdfea9c4 100644 --- a/src/PostRector/Rector/DocblockNameImportingPostRector.php +++ b/src/PostRector/Rector/DocblockNameImportingPostRector.php @@ -23,7 +23,7 @@ public function __construct( ) { } - public function enterNode(Node $node): Node|int|null + public function enterNode(Node $node): Node|null { if (! $node instanceof Stmt && ! $node instanceof Param) { return null; diff --git a/src/PostRector/Rector/NameImportingPostRector.php b/src/PostRector/Rector/NameImportingPostRector.php index 91c20da59bc..9c51238b31f 100644 --- a/src/PostRector/Rector/NameImportingPostRector.php +++ b/src/PostRector/Rector/NameImportingPostRector.php @@ -27,13 +27,16 @@ public function __construct( ) { } - public function beforeTraverse(array $nodes) + /** + * @return Stmt[] + */ + public function beforeTraverse(array $nodes): array { $this->currentUses = $this->useImportsResolver->resolve(); return $nodes; } - public function enterNode(Node $node): Node|int|null + public function enterNode(Node $node): Node|null { if (! $node instanceof FullyQualified) { return null; diff --git a/src/PostRector/Rector/UnusedImportRemovingPostRector.php b/src/PostRector/Rector/UnusedImportRemovingPostRector.php index a17e69879fc..dfa92cfc353 100644 --- a/src/PostRector/Rector/UnusedImportRemovingPostRector.php +++ b/src/PostRector/Rector/UnusedImportRemovingPostRector.php @@ -4,6 +4,8 @@ namespace Rector\PostRector\Rector; +use PhpParser\Node\UseItem; +use PhpParser\NodeVisitor; use Nette\Utils\Strings; use PhpParser\Comment; use PhpParser\Comment\Doc; @@ -13,8 +15,6 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; -use PhpParser\NodeTraverser; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -94,7 +94,7 @@ private function findNonUseImportNames(Namespace_|FileWithoutNamespace $namespac &$names ): int|null|Name { if ($node instanceof Use_) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $node instanceof Name) { @@ -177,11 +177,11 @@ private function resolveUsedPhpAndDocNames(Namespace_|FileWithoutNamespace $name /** * @param string[] $names */ - private function isUseImportUsed(UseUse $useUse, bool $isCaseSensitive, array $names, ?string $namespaceName): bool + private function isUseImportUsed(UseItem $useItem, bool $isCaseSensitive, array $names, ?string $namespaceName): bool { - $comparedName = $useUse->alias instanceof Identifier - ? $useUse->alias->toString() - : $useUse->name->toString(); + $comparedName = $useItem->alias instanceof Identifier + ? $useItem->alias->toString() + : $useItem->name->toString(); if (! $isCaseSensitive) { $comparedName = strtolower($comparedName); diff --git a/src/PostRector/Rector/UseAddingPostRector.php b/src/PostRector/Rector/UseAddingPostRector.php index eb689572c3e..587bb1fb063 100644 --- a/src/PostRector/Rector/UseAddingPostRector.php +++ b/src/PostRector/Rector/UseAddingPostRector.php @@ -4,10 +4,10 @@ namespace Rector\PostRector\Rector; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Namespace_; -use PhpParser\NodeTraverser; use Rector\CodingStyle\Application\UseImportsAdder; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; @@ -80,7 +80,7 @@ public function enterNode(Node $node): int * visitor per execution, using stop traversal here is safe, * ref https://github.com/rectorphp/rector-src/blob/fc1e742fa4d9861ccdc5933f3b53613b8223438d/src/PostRector/Application/PostFileProcessor.php#L59-L61 */ - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } /** diff --git a/src/PostRector/ValueObject/PropertyMetadata.php b/src/PostRector/ValueObject/PropertyMetadata.php index a2d2450402f..07df33b07b4 100644 --- a/src/PostRector/ValueObject/PropertyMetadata.php +++ b/src/PostRector/ValueObject/PropertyMetadata.php @@ -4,7 +4,7 @@ namespace Rector\PostRector\ValueObject; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; use PHPStan\Type\Type; final readonly class PropertyMetadata @@ -12,7 +12,7 @@ public function __construct( private string $name, private ?Type $type, - private int $flags = Class_::MODIFIER_PRIVATE, + private int $flags = Modifiers::PRIVATE, ) { } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 36d4ab5ddc7..e4b526a35ab 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -4,6 +4,7 @@ namespace Rector\Rector; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\Stmt\ClassMethod; @@ -12,9 +13,8 @@ use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Nop; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; use PhpParser\Node\Stmt\Trait_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use PHPStan\Analyser\MutatingScope; use PHPStan\Type\ObjectType; @@ -50,7 +50,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter B) Remove the Node: - return NodeTraverser::REMOVE_NODE; + return \PhpParser\NodeVisitor::REMOVE_NODE; CODE_SAMPLE; protected NodeNameResolver $nodeNameResolver; @@ -143,11 +143,11 @@ final public function enterNode(Node $node): int|Node|null $refactoredNode = $this->refactor($node); // @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing - if ($refactoredNode === NodeTraverser::REMOVE_NODE) { + if ($refactoredNode === NodeVisitor::REMOVE_NODE) { $this->toBeRemovedNodeId = spl_object_id($originalNode); // notify this rule changing code - $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getLine()); + $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine()); $this->file->addRectorClassWithLine($rectorWithLineChange); return $originalNode; @@ -158,7 +158,7 @@ final public function enterNode(Node $node): int|Node|null if (! in_array( $refactoredNode, - [NodeTraverser::DONT_TRAVERSE_CHILDREN, NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN], + [NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN], true )) { // notify this rule changing code @@ -199,7 +199,7 @@ public function leaveNode(Node $node): array|int|Node|null if ($this->toBeRemovedNodeId === $objectId) { $this->toBeRemovedNodeId = null; - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return $this->nodesToReturn[$objectId] ?? $node; @@ -225,7 +225,7 @@ protected function isNames(Node $node, array $names): bool * @return ($node is Node\Param ? string : * ($node is ClassMethod ? string : * ($node is Property ? string : - * ($node is PropertyProperty ? string : + * ($node is PropertyItem ? string : * ($node is Trait_ ? string : * ($node is Interface_ ? string : * ($node is Const_ ? string : @@ -325,7 +325,7 @@ private function postRefactorProcess( /** @var non-empty-array|Node $refactoredNode */ $this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class); - $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getLine()); + $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine()); $this->file->addRectorClassWithLine($rectorWithLineChange); /** @var MutatingScope|null $currentScope */ diff --git a/src/StaticTypeMapper/Naming/NameScopeFactory.php b/src/StaticTypeMapper/Naming/NameScopeFactory.php index d69fedf9224..0921e8841fa 100644 --- a/src/StaticTypeMapper/Naming/NameScopeFactory.php +++ b/src/StaticTypeMapper/Naming/NameScopeFactory.php @@ -4,10 +4,10 @@ namespace Rector\StaticTypeMapper\Naming; +use PhpParser\Node\UseItem; use PhpParser\Node; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use PHPStan\Analyser\NameScope; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; @@ -53,7 +53,7 @@ private function resolveUseNamesByAlias(array $useNodes): array foreach ($useNodes as $useNode) { $prefix = $this->useImportsResolver->resolvePrefix($useNode); foreach ($useNode->uses as $useUse) { - /** @var UseUse $useUse */ + /** @var UseItem $useUse */ $aliasName = $useUse->getAlias() ->name; diff --git a/src/StaticTypeMapper/PhpDocParser/IdentifierPhpDocTypeMapper.php b/src/StaticTypeMapper/PhpDocParser/IdentifierPhpDocTypeMapper.php index f0a43b6ad7c..8016891b8b6 100644 --- a/src/StaticTypeMapper/PhpDocParser/IdentifierPhpDocTypeMapper.php +++ b/src/StaticTypeMapper/PhpDocParser/IdentifierPhpDocTypeMapper.php @@ -95,6 +95,7 @@ public function mapIdentifierTypeNode(IdentifierTypeNode $identifierTypeNode, No return new UnionType($scalarTypes); } + $identifierTypeNode->name = ltrim($identifierTypeNode->name, '@'); $objectType = new ObjectType($identifierTypeNode->name); } diff --git a/src/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php b/src/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php index 9047d45f3da..e54735b09d7 100644 --- a/src/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php +++ b/src/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php @@ -4,9 +4,9 @@ namespace Rector\StaticTypeMapper\ValueObject\Type; +use PhpParser\Node\UseItem; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver; @@ -35,9 +35,9 @@ public function getUseNode(int $useType): Use_ { $name = new Name($this->fullyQualifiedClass); - $useUse = new UseUse($name, $this->getClassName()); + $useItem = new UseItem($name, $this->getClassName()); - $use = new Use_([$useUse]); + $use = new Use_([$useItem]); $use->type = $useType; return $use; diff --git a/src/StaticTypeMapper/ValueObject/Type/FullyQualifiedObjectType.php b/src/StaticTypeMapper/ValueObject/Type/FullyQualifiedObjectType.php index fcd6b59ca33..3405aae976a 100644 --- a/src/StaticTypeMapper/ValueObject/Type/FullyQualifiedObjectType.php +++ b/src/StaticTypeMapper/ValueObject/Type/FullyQualifiedObjectType.php @@ -4,10 +4,10 @@ namespace Rector\StaticTypeMapper\ValueObject\Type; +use PhpParser\Node\UseItem; use Nette\Utils\Strings; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use PHPStan\Type\ObjectType; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -52,9 +52,9 @@ public function getShortNameNode(): Name public function getUseNode(int $useType): Use_ { $name = new Name($this->getClassName()); - $useUse = new UseUse($name); + $useItem = new UseItem($name); - $use = new Use_([$useUse]); + $use = new Use_([$useItem]); $use->type = $useType; return $use; diff --git a/src/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php b/src/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php index fa4f6b0decd..262f657e4a9 100644 --- a/src/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php +++ b/src/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php @@ -4,7 +4,7 @@ namespace Rector\StaticTypeMapper\ValueObject\Type; -use PHPStan\TrinaryLogic; +use PHPStan\Type\IsSuperTypeOfResult; use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\Type; @@ -24,7 +24,7 @@ public function __construct( parent::__construct($shortName, $types); } - public function isSuperTypeOf(Type $type): TrinaryLogic + public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { $genericObjectType = new GenericObjectType($this->fullyQualifiedName, $this->getTypes()); return $genericObjectType->isSuperTypeOf($type); diff --git a/src/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php b/src/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php index 76beff0939b..208bd9ce896 100644 --- a/src/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php +++ b/src/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php @@ -4,7 +4,7 @@ namespace Rector\StaticTypeMapper\ValueObject\Type; -use PHPStan\TrinaryLogic; +use PHPStan\Type\IsSuperTypeOfResult; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; @@ -23,7 +23,7 @@ public function __construct( parent::__construct($shortName); } - public function isSuperTypeOf(Type $type): TrinaryLogic + public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { $fullyQualifiedObjectType = new ObjectType($this->fullyQualifiedName); return $fullyQualifiedObjectType->isSuperTypeOf($type); diff --git a/src/ValueObject/Application/File.php b/src/ValueObject/Application/File.php index fec056fdfcd..89c163db666 100644 --- a/src/ValueObject/Application/File.php +++ b/src/ValueObject/Application/File.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\InlineHTML; use PhpParser\NodeFinder; +use PhpParser\Token; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Exception\ShouldNotHappenException; use Rector\ValueObject\Reporting\FileDiff; @@ -31,7 +32,7 @@ final class File private array $newStmts = []; /** - * @var array + * @var array */ private array $oldTokens = []; @@ -100,7 +101,7 @@ public function getFileDiff(): ?FileDiff /** * @param Stmt[] $newStmts * @param Stmt[] $oldStmts - * @param array $oldTokens + * @param array $oldTokens */ public function hydrateStmtsAndTokens(array $newStmts, array $oldStmts, array $oldTokens): void { @@ -130,7 +131,7 @@ public function getNewStmts(): array } /** - * @return array + * @return array */ public function getOldTokens(): array { diff --git a/src/ValueObject/Visibility.php b/src/ValueObject/Visibility.php index 719cf5a9ae9..85b5458a6d2 100644 --- a/src/ValueObject/Visibility.php +++ b/src/ValueObject/Visibility.php @@ -4,42 +4,42 @@ namespace Rector\ValueObject; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; final class Visibility { /** * @var int */ - public const PUBLIC = Class_::MODIFIER_PUBLIC; + public const PUBLIC = Modifiers::PUBLIC; /** * @var int */ - public const PROTECTED = Class_::MODIFIER_PROTECTED; + public const PROTECTED = Modifiers::PROTECTED; /** * @var int */ - public const PRIVATE = Class_::MODIFIER_PRIVATE; + public const PRIVATE = Modifiers::PRIVATE; /** * @var int */ - public const STATIC = Class_::MODIFIER_STATIC; + public const STATIC = Modifiers::STATIC; /** * @var int */ - public const ABSTRACT = Class_::MODIFIER_ABSTRACT; + public const ABSTRACT = Modifiers::ABSTRACT; /** * @var int */ - public const FINAL = Class_::MODIFIER_FINAL; + public const FINAL = Modifiers::FINAL; /** * @var int */ - public const READONLY = Class_::MODIFIER_READONLY; + public const READONLY = Modifiers::READONLY; } diff --git a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php index f08c6c13354..241f2ab419b 100644 --- a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php +++ b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php @@ -7,7 +7,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\ExtendedFunctionVariant; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\MixedType; use Rector\FileSystem\FilePathHelper; @@ -68,7 +68,7 @@ private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scop $classMethod, $scope ); - if ($parametersAcceptor instanceof FunctionVariantWithPhpDocs && ! $parametersAcceptor->getNativeReturnType() instanceof MixedType) { + if ($parametersAcceptor instanceof ExtendedFunctionVariant && ! $parametersAcceptor->getNativeReturnType() instanceof MixedType) { return false; } diff --git a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php index 1019ef27936..e6bce14c795 100644 --- a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php +++ b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php @@ -6,7 +6,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\ExtendedFunctionVariant; use PHPStan\Type\MixedType; use Rector\NodeAnalyzer\MagicClassMethodAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; @@ -60,7 +60,7 @@ private function isVendorLockedByAncestors(ClassReflection $classReflection, str $parentClassMethodReflection = $ancestorClassReflections->getNativeMethod($methodName); $parametersAcceptor = $parentClassMethodReflection->getVariants()[0]; - if (! $parametersAcceptor instanceof FunctionVariantWithPhpDocs) { + if (! $parametersAcceptor instanceof ExtendedFunctionVariant) { continue; } diff --git a/src/VendorLocker/ParentClassMethodTypeOverrideGuard.php b/src/VendorLocker/ParentClassMethodTypeOverrideGuard.php index bf0a1d8b0a5..a129a27e5ac 100644 --- a/src/VendorLocker/ParentClassMethodTypeOverrideGuard.php +++ b/src/VendorLocker/ParentClassMethodTypeOverrideGuard.php @@ -4,6 +4,7 @@ namespace Rector\VendorLocker; +use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; @@ -50,7 +51,7 @@ public function getParentClassMethod(ClassMethod|MethodReflection $classMethod): public function shouldSkipReturnTypeChange(ClassMethod $classMethod, Type $parentType): bool { - if ($classMethod->returnType === null) { + if (!$classMethod->returnType instanceof Node) { return false; } diff --git a/stubs/Doctrine/Nette/Security/User.php b/stubs/Doctrine/Nette/Security/User.php new file mode 100644 index 00000000000..07b8c544ca1 --- /dev/null +++ b/stubs/Doctrine/Nette/Security/User.php @@ -0,0 +1,14 @@ +resolve(); $this->assertContainsOnlyInstancesOf(InstalledPackage::class, $installedPackages); - $this->assertGreaterThan(80, count($installedPackages)); + $this->assertGreaterThan(77, count($installedPackages)); } } diff --git a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic.php.inc b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic.php.inc index f925056fba3..d5c4e69229d 100644 --- a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic.php.inc +++ b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic.php.inc @@ -29,7 +29,10 @@ use Symfony\Component\Routing\Annotation\Route; class TwoRoutesAfterGeneric { /** - * @OA\Property(type="array", @OA\Items(ref=@Model(type=TestItem::class))) + * @OA\Property( + * type="array", + * @OA\Items(ref=@Model(type=TestItem::class)) + * ) * @Route("/first", methods={"GET"}) * @Route("/second", methods={"GET"}) */ diff --git a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic_with_string_key_numeric.php.inc b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic_with_string_key_numeric.php.inc index 9492f73c826..97fb761a684 100644 --- a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic_with_string_key_numeric.php.inc +++ b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_after_generic_with_string_key_numeric.php.inc @@ -29,7 +29,10 @@ use Symfony\Component\Routing\Annotation\Route; class TwoRoutesAfterGenericWithStringKeyNumeric { /** - * @OA\Property(type="array", '1'=@OA\Items(ref=@Model(type=TestItem::class))) + * @OA\Property( + * type="array", + * '1'=@OA\Items(ref=@Model(type=TestItem::class)) + * ) * @Route("/first", methods={"GET"}) * @Route("/second", methods={"GET"}) */ diff --git a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc index ab7f628422d..a177f80421e 100644 --- a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc +++ b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc @@ -28,6 +28,7 @@ class TwoRoutesWithCommentBefore { /** * Testsssssssssss + * * @Route("/first", methods={"GET"}) * @Route("/second", methods={"GET"}) */ diff --git a/tests/Issues/FqcnAnnotationToAttribute/Fixture/fixture.php.inc b/tests/Issues/FqcnAnnotationToAttribute/Fixture/fixture.php.inc index 95ced7c2337..39ae5355a52 100644 --- a/tests/Issues/FqcnAnnotationToAttribute/Fixture/fixture.php.inc +++ b/tests/Issues/FqcnAnnotationToAttribute/Fixture/fixture.php.inc @@ -30,12 +30,12 @@ class Entity namespace Rector\Tests\Issues\FqcnAnnotationToAttribute\Fixture; #[\Doctrine\ORM\Mapping\Entity] -#[\Doctrine\ORM\Mapping\Index(name: 'name_index', columns: ['name'])] -#[\Doctrine\ORM\Mapping\Index(name: 'surname_index', columns: ['surname'])] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('azureB2cUuid')] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('uuid')] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('email')] #[\Doctrine\ORM\Mapping\Table('user')] +#[\Doctrine\ORM\Mapping\Index(name: 'name_index', columns: ['name'])] +#[\Doctrine\ORM\Mapping\Index(name: 'surname_index', columns: ['surname'])] class Entity { #[\Symfony\Component\Validator\Constraints\NotBlank] diff --git a/tests/Issues/FqcnAnnotationToAttribute/Fixture/single_quote.php.inc b/tests/Issues/FqcnAnnotationToAttribute/Fixture/single_quote.php.inc index 95205f3411a..f1b22a6464a 100644 --- a/tests/Issues/FqcnAnnotationToAttribute/Fixture/single_quote.php.inc +++ b/tests/Issues/FqcnAnnotationToAttribute/Fixture/single_quote.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Issues\FqcnAnnotationToAttribute\Fixture; /** * @\Doctrine\ORM\Mapping\Entity() - * @\Doctrine\ORM\Mapping\Table('user', indexes={ + * @\Doctrine\ORM\Mapping\Table("user", indexes={ * @\Doctrine\ORM\Mapping\Index(name="name_index", columns={"name"}), * @\Doctrine\ORM\Mapping\Index(name="surname_index", columns={"surname"}), * }) @@ -30,12 +30,12 @@ class SingleQuote namespace Rector\Tests\Issues\FqcnAnnotationToAttribute\Fixture; #[\Doctrine\ORM\Mapping\Entity] -#[\Doctrine\ORM\Mapping\Index(name: 'name_index', columns: ['name'])] -#[\Doctrine\ORM\Mapping\Index(name: 'surname_index', columns: ['surname'])] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('azureB2cUuid')] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('uuid')] #[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('email')] #[\Doctrine\ORM\Mapping\Table('user')] +#[\Doctrine\ORM\Mapping\Index(name: 'name_index', columns: ['name'])] +#[\Doctrine\ORM\Mapping\Index(name: 'surname_index', columns: ['surname'])] class SingleQuote { #[\Symfony\Component\Validator\Constraints\NotBlank] diff --git a/tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php b/tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php index 0b83aa0917e..83181c31c1d 100644 --- a/tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php +++ b/tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php @@ -4,10 +4,10 @@ namespace Rector\Tests\Issues\InfiniteLoop\Rector\MethodCall; +use PhpParser\NodeVisitor; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\MethodCall; -use PhpParser\NodeTraverser; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -30,7 +30,7 @@ public function getNodeTypes(): array public function refactor(Node $node): Assign|null|int { if ($node instanceof Assign) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } if (! $this->isName($node->name, 'modify')) { diff --git a/tests/Issues/PlainValueParser/Fixture/fixture.php.inc b/tests/Issues/PlainValueParser/Fixture/fixture.php.inc index 60c5935c1f7..c64045ccbf3 100644 --- a/tests/Issues/PlainValueParser/Fixture/fixture.php.inc +++ b/tests/Issues/PlainValueParser/Fixture/fixture.php.inc @@ -25,9 +25,9 @@ use Rector\Tests\Issues\PlainValueParser\Source\CustomAnnotation; final class SomeFixture { - #[CustomAnnotation(description: 'List of value : - - < b > TRY < /b>: To try - - < b > TEST < /b>: to test ( Default if no parameters given )')] + #[CustomAnnotation(description: 'List of value: + - TRY: To try + - TEST: to test (Default if no parameters given)')] public function test() {} } diff --git a/tests/NodeManipulator/ClassDependencyManipulatorTest.php b/tests/NodeManipulator/ClassDependencyManipulatorTest.php index 5d2af9bf1d1..3bbb8426d36 100644 --- a/tests/NodeManipulator/ClassDependencyManipulatorTest.php +++ b/tests/NodeManipulator/ClassDependencyManipulatorTest.php @@ -4,6 +4,7 @@ namespace Rector\Tests\NodeManipulator; +use PhpParser\Modifiers; use PhpParser\Node\Const_; use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\String_; @@ -11,7 +12,9 @@ use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; -use PhpParser\Node\Stmt\PropertyProperty; +use PhpParser\Node\PropertyItem; +use PhpParser\NodeTraverser; +use PhpParser\NodeVisitor\NameResolver; use PhpParser\PrettyPrinter\Standard; use PHPStan\Type\ObjectType; use Rector\NodeManipulator\ClassDependencyManipulator; @@ -30,10 +33,21 @@ protected function setUp(): void $this->printerStandard = new Standard(); } + private function setNamespacedName(Class_ $class): void + { + $nameResolver = new NameResolver(); + $nodeTraverser = new NodeTraverser(); + + $nodeTraverser->addVisitor($nameResolver); + $nodeTraverser->traverse([$class]); + } + public function testEmptyClass(): void { $someClass = new Class_(new Identifier('EmptyClass')); + $this->setNamespacedName($someClass); + $this->addSingleDependency($someClass); $this->asssertClassEqualsFile($someClass, __DIR__ . '/Fixture/expected_empty_class.php.inc'); } @@ -41,6 +55,9 @@ public function testEmptyClass(): void public function testSingleMethod(): void { $someClass = new Class_(new Identifier('SingleMethodClass')); + + $this->setNamespacedName($someClass); + $someClass->stmts[] = new ClassMethod('firstMethod'); $this->addSingleDependency($someClass); @@ -51,7 +68,10 @@ public function testSingleMethod(): void public function testWithProperty(): void { $someClass = new Class_(new Identifier('ClassWithSingleProperty')); - $someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); + + $this->setNamespacedName($someClass); + + $someClass->stmts[] = new Property(Modifiers::PRIVATE, [new PropertyItem('someProperty')]); $this->addSingleDependency($someClass); @@ -62,7 +82,10 @@ public function testWithProperty(): void public function testWithMethodAndProperty(): void { $someClass = new Class_(new Identifier('ClassWithMethodAndProperty')); - $someClass->stmts[] = new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty('someProperty')]); + + $this->setNamespacedName($someClass); + + $someClass->stmts[] = new Property(Modifiers::PRIVATE, [new PropertyItem('someProperty')]); $someClass->stmts[] = new ClassMethod(new Identifier('someMethod')); $this->addSingleDependency($someClass); @@ -73,9 +96,12 @@ public function testWithMethodAndProperty(): void public function testConstantProperties(): void { $someClass = new Class_(new Identifier('ConstantProperties')); + + $this->setNamespacedName($someClass); + $someClass->stmts[] = new ClassConst([new Const_('SOME_CONST', new String_('value'))]); - $someClass->stmts[] = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('someProperty')]); - $someClass->stmts[] = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('anotherProperty')]); + $someClass->stmts[] = new Property(Modifiers::PUBLIC, [new PropertyItem('someProperty')]); + $someClass->stmts[] = new Property(Modifiers::PUBLIC, [new PropertyItem('anotherProperty')]); $this->addSingleDependency($someClass); diff --git a/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php b/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php index ef2257266a7..62dd2ca2417 100644 --- a/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php +++ b/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php @@ -4,12 +4,12 @@ namespace Rector\Tests\PhpAttribute\AnnotationToAttributeMapper; +use PhpParser\Node\Scalar\Int_; use Iterator; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PHPUnit\Framework\Attributes\DataProvider; use Rector\PhpAttribute\AnnotationToAttributeMapper; @@ -44,7 +44,7 @@ public static function provideData(): Iterator { yield [false, ConstFetch::class]; yield ['false', ConstFetch::class]; - yield ['100', LNumber::class]; + yield ['100', Int_::class]; yield ['hey', String_::class]; yield [['hey'], Array_::class]; } diff --git a/tests/PhpAttribute/UseAliasNameMatcherTest.php b/tests/PhpAttribute/UseAliasNameMatcherTest.php index 525f62f8468..c5f007aef95 100644 --- a/tests/PhpAttribute/UseAliasNameMatcherTest.php +++ b/tests/PhpAttribute/UseAliasNameMatcherTest.php @@ -4,10 +4,10 @@ namespace Rector\Tests\PhpAttribute; +use PhpParser\Node\UseItem; use Iterator; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\Stmt\UseUse; use PHPUnit\Framework\Attributes\DataProvider; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php80\ValueObject\AnnotationToAttribute; @@ -38,10 +38,10 @@ public function test( string $expectedAttributeUseImportName, string $expectedShortAttributeName, ): void { - $useUse = new UseUse(new Name($useImportName), $useAlias); - $useUse->setAttribute(AttributeKey::ORIGINAL_NODE, $useUse); + $useItem = new UseItem(new Name($useImportName), $useAlias); + $useItem->setAttribute(AttributeKey::ORIGINAL_NODE, $useItem); - $uses = [new Use_([$useUse])]; + $uses = [new Use_([$useItem])]; // uses $useAliasMetadata = $this->useAliasNameMatcher->match($uses, $shortAnnotationName, $annotationToAttribute); diff --git a/tests/PhpParser/Node/NodeFactoryTest.php b/tests/PhpParser/Node/NodeFactoryTest.php index a513bb3a8b0..788e9bb7006 100644 --- a/tests/PhpParser/Node/NodeFactoryTest.php +++ b/tests/PhpParser/Node/NodeFactoryTest.php @@ -4,12 +4,12 @@ namespace Rector\Tests\PhpParser\Node; +use PhpParser\Node\Scalar\Int_; use Iterator; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PHPUnit\Framework\Attributes\DataProvider; use Rector\PhpParser\Node\NodeFactory; @@ -40,25 +40,25 @@ public function testCreateArray(array $inputArray, Array_ $expectedArray): void */ public static function provideDataForArray(): Iterator { - $lNumber = new LNumber(1); + $int = new Int_(1); $string = new String_('a'); $trueConstFetch = new ConstFetch(new Name('true')); $falseConstFetch = new ConstFetch(new Name('false')); $nullConstEtch = new ConstFetch(new Name('null')); $array = new Array_(); - $array->items[] = new ArrayItem($lNumber); + $array->items[] = new ArrayItem($int); yield [[1], $array]; $array = new Array_(); - $array->items[] = new ArrayItem($lNumber, $string); + $array->items[] = new ArrayItem($int, $string); yield [[ 'a' => 1, ], $array]; $array = new Array_(); - $array->items[] = new ArrayItem($lNumber); - yield [[$lNumber], $array]; + $array->items[] = new ArrayItem($int); + yield [[$int], $array]; $array = new Array_(); $array->items[] = new ArrayItem($string); diff --git a/tests/StaticTypeMapper/PhpDoc/PhpDocTypeMapperTest.php b/tests/StaticTypeMapper/PhpDoc/PhpDocTypeMapperTest.php index 8f5de66ee04..774e98a1db2 100644 --- a/tests/StaticTypeMapper/PhpDoc/PhpDocTypeMapperTest.php +++ b/tests/StaticTypeMapper/PhpDoc/PhpDocTypeMapperTest.php @@ -10,7 +10,7 @@ use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -use PHPStan\Type\ArrayType; +use PHPStan\Type\Constant\ConstantArrayType; use PHPUnit\Framework\Attributes\DataProvider; use Rector\StaticTypeMapper\Naming\NameScopeFactory; use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper; @@ -42,12 +42,14 @@ public function test(TypeNode $typeNode, string $expectedPHPStanType): void } /** - * @return Iterator[]|ArrayShapeNode[]> + * @return Iterator<(class-string[] | ArrayShapeNode[])> */ public static function provideData(): Iterator { - $arrayShapeNode = new ArrayShapeNode([new ArrayShapeItemNode(null, true, new IdentifierTypeNode('string'))]); + $arrayShapeNode = ArrayShapeNode::createSealed([ + new ArrayShapeItemNode(null, true, new IdentifierTypeNode('string')), + ]); - yield [$arrayShapeNode, ArrayType::class]; + yield [$arrayShapeNode, ConstantArrayType::class]; } }