From fde7a7a1e6f2d722f8f5f0cf1f77c73f0bbb837c Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 16 Dec 2021 22:51:29 +0100 Subject: [PATCH 1/2] Downgrade PHPUnit after transforming source --- .github/workflows/static-analysis.yml | 8 ++++---- .github/workflows/tests.yml | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 6620abb9cf0..29b2d09c9ac 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -47,14 +47,14 @@ jobs: - name: "Install dependencies" run: "composer install --no-interaction --no-progress" - - name: "Downgrade PHPUnit" - if: matrix.php-version == '7.1' || matrix.php-version == '7.2' - run: "composer require --dev phpunit/phpunit:^7.5.20 brianium/paratest:^4.0 --update-with-dependencies" - - name: "Transform source code" if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1' run: php bin/transform-source.php + - name: "Downgrade PHPUnit" + if: matrix.php-version == '7.1' || matrix.php-version == '7.2' + run: "composer require --dev phpunit/phpunit:^7.5.20 brianium/paratest:^4.0 --update-with-dependencies" + - name: "PHPStan" run: ${{ matrix.script }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e1beee4998a..da2b00ab44f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -87,12 +87,12 @@ jobs: - name: "Install dependencies" run: "composer install --no-interaction --no-progress" - - name: "Downgrade PHPUnit" - run: "composer require --dev phpunit/phpunit:^7.5.20 brianium/paratest:^4.0 --update-with-dependencies" - - name: "Transform source code" run: php bin/transform-source.php + - name: "Downgrade PHPUnit" + run: "composer require --dev phpunit/phpunit:^7.5.20 brianium/paratest:^4.0 --update-with-dependencies" + - name: "Tests" run: "${{ matrix.script }}" From 2614c4c2d78cb042b274161ee359a2ac0799069e Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 16 Dec 2021 20:57:38 +0100 Subject: [PATCH 2/2] Transform sources with Rector --- .github/workflows/compiler-tests.yml | 2 +- .github/workflows/lint.yml | 2 +- .github/workflows/phar-old.yml | 2 +- .github/workflows/phar.yml | 2 +- .github/workflows/static-analysis.yml | 3 +- .github/workflows/tests.yml | 6 +- bin/transform-source.php | 122 ------------------- build/rector-downgrade.php | 18 +++ build/transform-source | 7 ++ composer.json | 1 + composer.lock | 62 +++++++++- patches/PHPStanNodeScopeResolver.patch | 21 ++++ tests/PHPStan/Composer/AutoloadFilesTest.php | 6 +- 13 files changed, 123 insertions(+), 131 deletions(-) delete mode 100755 bin/transform-source.php create mode 100644 build/rector-downgrade.php create mode 100755 build/transform-source create mode 100644 patches/PHPStanNodeScopeResolver.patch diff --git a/.github/workflows/compiler-tests.yml b/.github/workflows/compiler-tests.yml index 047624aa98b..7f2547a040d 100644 --- a/.github/workflows/compiler-tests.yml +++ b/.github/workflows/compiler-tests.yml @@ -32,7 +32,7 @@ jobs: run: "composer install --no-dev --no-interaction --no-progress" - name: "Transform source code" - run: php bin/transform-source.php + run: build/transform-source - name: "Tests" run: | diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6c61cce7a17..5b625c773d1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -46,7 +46,7 @@ jobs: - name: "Transform source code" if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1' - run: php bin/transform-source.php + run: build/transform-source - name: "Lint" run: "make lint" diff --git a/.github/workflows/phar-old.yml b/.github/workflows/phar-old.yml index 6244c0f5b09..9c51546e29c 100644 --- a/.github/workflows/phar-old.yml +++ b/.github/workflows/phar-old.yml @@ -34,7 +34,7 @@ jobs: run: "composer install --no-interaction --no-progress --working-dir=compiler" - name: "Transform source code" - run: php bin/transform-source.php + run: build/transform-source - name: "Compile PHAR" run: php compiler/bin/compile diff --git a/.github/workflows/phar.yml b/.github/workflows/phar.yml index 12c4c5e405b..aca187041c5 100644 --- a/.github/workflows/phar.yml +++ b/.github/workflows/phar.yml @@ -36,7 +36,7 @@ jobs: run: "composer install --no-interaction --no-progress --working-dir=compiler" - name: "Transform source code" - run: php bin/transform-source.php + run: build/transform-source - name: "Compile PHAR" run: php compiler/bin/compile diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 29b2d09c9ac..480644af25f 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -49,7 +49,8 @@ jobs: - name: "Transform source code" if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1' - run: php bin/transform-source.php + shell: bash + run: build/transform-source - name: "Downgrade PHPUnit" if: matrix.php-version == '7.1' || matrix.php-version == '7.2' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index da2b00ab44f..87a81d856cd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,7 +49,8 @@ jobs: - name: "Transform source code" if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1' - run: php bin/transform-source.php + shell: bash + run: build/transform-source - name: "Tests" run: "${{ matrix.script }}" @@ -88,7 +89,8 @@ jobs: run: "composer install --no-interaction --no-progress" - name: "Transform source code" - run: php bin/transform-source.php + shell: bash + run: build/transform-source - name: "Downgrade PHPUnit" run: "composer require --dev phpunit/phpunit:^7.5.20 brianium/paratest:^4.0 --update-with-dependencies" diff --git a/bin/transform-source.php b/bin/transform-source.php deleted file mode 100755 index 8ecbefb0236..00000000000 --- a/bin/transform-source.php +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/env php -type === null) { - return null; - } - $docComment = $node->getDocComment(); - if ($docComment !== null) { - $node->type = null; - return $node; - } - - $node->setDocComment(new \PhpParser\Comment\Doc(sprintf('/** @var %s */', $this->printType($node->type)))); - $node->type = null; - - return $node; - } - - /** - * @param Identifier|Name|NullableType|UnionType $type - * @return string - */ - private function printType($type): string - { - if ($type instanceof NullableType) { - return $this->printType($type->type) . '|null'; - } - - if ($type instanceof UnionType) { - throw new \Exception('UnionType not yet supported'); - } - - if ($type instanceof Name) { - $name = $type->toString(); - if ($type->isFullyQualified()) { - return '\\' . $name; - } - - return $name; - } - - if ($type instanceof Identifier) { - return $type->name; - } - - throw new \Exception('Unsupported type class'); - } - -} - -(function () { - $dir = __DIR__ . '/../src'; - - $lexer = new Lexer\Emulative([ - 'usedAttributes' => [ - 'comments', - 'startLine', 'endLine', - 'startTokenPos', 'endTokenPos', - ], - ]); - $parser = new Parser\Php7($lexer, [ - 'useIdentifierNodes' => true, - 'useConsistentVariableNodes' => true, - 'useExpressionStatements' => true, - 'useNopStatements' => false, - ]); - $nameResolver = new NodeVisitor\NameResolver(null, [ - 'replaceNodes' => false - ]); - - $printer = new PrettyPrinter\Standard(); - - $traverser = new NodeTraverser(); - $traverser->addVisitor(new NodeVisitor\CloningVisitor()); - $traverser->addVisitor($nameResolver); - $traverser->addVisitor(new PhpPatcher($printer)); - - $it = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($dir), - RecursiveIteratorIterator::LEAVES_ONLY - ); - foreach ($it as $file) { - $fileName = $file->getPathname(); - if (!preg_match('/\.php$/', $fileName)) { - continue; - } - - $code = \PHPStan\File\FileReader::read($fileName); - $origStmts = $parser->parse($code); - $newCode = $printer->printFormatPreserving( - $traverser->traverse($origStmts), - $origStmts, - $lexer->getTokens() - ); - - \PHPStan\File\FileWriter::write($fileName, $newCode); - } -})(); diff --git a/build/rector-downgrade.php b/build/rector-downgrade.php new file mode 100644 index 00000000000..a285cee4734 --- /dev/null +++ b/build/rector-downgrade.php @@ -0,0 +1,18 @@ +parameters(); + + $parameters->set(Option::SKIP, [ + 'tests/*/data/*', + 'tests/PHPStan/Analyser/traits/*', + 'tests/PHPStan/Generics/functions.php', + ]); + + $services = $containerConfigurator->services(); + $services->set(DowngradeTypedPropertyRector::class); +}; diff --git a/build/transform-source b/build/transform-source new file mode 100755 index 00000000000..5740ad76de2 --- /dev/null +++ b/build/transform-source @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -o errexit +set -o pipefail +set -o nounset + +docker run --rm -v $(pwd):/app -w /app php:8.0-cli vendor/bin/rector process src tests/PHPStan -c build/rector-downgrade.php diff --git a/composer.json b/composer.json index 58258d82365..8fc89054f9a 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,7 @@ "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-strict-rules": "^1.0", "phpunit/phpunit": "^9.5.4", + "rector/rector": "0.12.5", "vaimo/composer-patches": "^4.22" }, "config": { diff --git a/composer.lock b/composer.lock index d11be561371..6cd783f77c0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f3d8876eb1aa89b253ae3df9ee66e2df", + "content-hash": "91c92e91ba8b3c0e0e13ce933977ee99", "packages": [ { "name": "clue/block-react", @@ -5004,6 +5004,66 @@ ], "time": "2021-03-23T07:16:29+00:00" }, + { + "name": "rector/rector", + "version": "0.12.5", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "e50e40e0fe73b88a46f27e086441e1998ceeca3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/e50e40e0fe73b88a46f27e086441e1998ceeca3c", + "reference": "e50e40e0fe73b88a46f27e086441e1998ceeca3c", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0", + "phpstan/phpstan": "^1.1.1" + }, + "conflict": { + "phpstan/phpdoc-parser": "<1.2", + "rector/rector-cakephp": "*", + "rector/rector-doctrine": "*", + "rector/rector-laravel": "*", + "rector/rector-nette": "*", + "rector/rector-phpoffice": "*", + "rector/rector-phpunit": "*", + "rector/rector-prefixed": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.12-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Prefixed and PHP 7.1 downgraded version of rector/rector", + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.12.5" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2021-11-23T17:38:29+00:00" + }, { "name": "sebastian/cli-parser", "version": "1.0.1", diff --git a/patches/PHPStanNodeScopeResolver.patch b/patches/PHPStanNodeScopeResolver.patch new file mode 100644 index 00000000000..29413cdf882 --- /dev/null +++ b/patches/PHPStanNodeScopeResolver.patch @@ -0,0 +1,21 @@ +@package rector/rector + +--- packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php 2021-12-16 21:10:20.000000000 +0100 ++++ packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php 2021-12-16 21:10:27.000000000 +0100 +@@ -226,12 +226,12 @@ + { + // 1. get PHPStan locator + /** @var ClassReflector $classReflector */ +- $classReflector = $this->privatesAccessor->getPrivateProperty($nodeScopeResolver, 'classReflector'); ++ //$classReflector = $this->privatesAccessor->getPrivateProperty($nodeScopeResolver, 'classReflector'); + /** @var SourceLocator $sourceLocator */ +- $sourceLocator = $this->privatesAccessor->getPrivateProperty($classReflector, 'sourceLocator'); ++ //$sourceLocator = $this->privatesAccessor->getPrivateProperty($classReflector, 'sourceLocator'); + // 2. get Rector locator +- $aggregateSourceLocator = new \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator([$sourceLocator, $this->renamedClassesSourceLocator, $this->parentAttributeSourceLocator]); +- $this->privatesAccessor->setPrivateProperty($classReflector, 'sourceLocator', $aggregateSourceLocator); ++ //$aggregateSourceLocator = new \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator([$sourceLocator, $this->renamedClassesSourceLocator, $this->parentAttributeSourceLocator]); ++ //$this->privatesAccessor->setPrivateProperty($classReflector, 'sourceLocator', $aggregateSourceLocator); + } + private function createDummyClassScopeContext(\PHPStan\Analyser\MutatingScope $mutatingScope) : \PHPStan\Analyser\ScopeContext + { diff --git a/tests/PHPStan/Composer/AutoloadFilesTest.php b/tests/PHPStan/Composer/AutoloadFilesTest.php index 32bf0f3eca3..c14a37123a4 100644 --- a/tests/PHPStan/Composer/AutoloadFilesTest.php +++ b/tests/PHPStan/Composer/AutoloadFilesTest.php @@ -15,6 +15,7 @@ use function sort; use function strlen; use function substr; +use const DIRECTORY_SEPARATOR; use const PHP_VERSION_ID; class AutoloadFilesTest extends TestCase @@ -43,7 +44,10 @@ public function testExpectedFiles(): void } foreach ($json['autoload']['files'] as $file) { - $autoloadFile = substr(dirname($realpath) . '/' . $file, strlen($vendorPath) + 1); + $autoloadFile = substr(dirname($realpath) . DIRECTORY_SEPARATOR . $file, strlen($vendorPath) + 1); + if (strpos($autoloadFile, 'rector' . DIRECTORY_SEPARATOR . 'rector' . DIRECTORY_SEPARATOR) === 0) { + continue; + } $autoloadFiles[] = $fileHelper->normalizePath($autoloadFile); } }