From f480f6e2e3ddb8811bd1f575519f163a8fb32653 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 27 Nov 2025 16:12:53 +0100 Subject: [PATCH 1/4] [issue 9492] create reproducer for modified array_map args, as creates changed args on print --- .github/workflows/phpstan_printer_test.yaml | 41 +++++++++++++++++++ composer.json | 2 +- .../Printer/Fixture/some_array_map.php | 5 +++ .../PhpParser/Printer/PHPStanPrinterTest.php | 39 ++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/phpstan_printer_test.yaml create mode 100644 tests/PhpParser/Printer/Fixture/some_array_map.php create mode 100644 tests/PhpParser/Printer/PHPStanPrinterTest.php diff --git a/.github/workflows/phpstan_printer_test.yaml b/.github/workflows/phpstan_printer_test.yaml new file mode 100644 index 00000000000..ff080c000c9 --- /dev/null +++ b/.github/workflows/phpstan_printer_test.yaml @@ -0,0 +1,41 @@ +name: PHPStan Printer Test + +on: + pull_request: null + push: + branches: + - main + + + +env: + # see https://github.com/composer/composer/issues/9368#issuecomment-718112361 + COMPOSER_ROOT_VERSION: "dev-main" + +jobs: + tests: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + php-versions: ['8.2'] + + runs-on: ${{ matrix.os }} + timeout-minutes: 3 + + name: PHP ${{ matrix.php-versions }} tests (${{ matrix.os }}) + steps: + - uses: actions/checkout@v4 + + - + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: none + # to display warning when assert() is called, eg: on direct getArgs() on CallLike + # and check against first class callable strlen(...) + ini-values: zend.assertions=1 + + - uses: "ramsey/composer-install@v3" + + - run: vendor/bin/phpunit tests/PhpParser/Printer/PHPStanPrinterTest.php --colors diff --git a/composer.json b/composer.json index 26cb49af90e..5b1e9140662 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "nikic/php-parser": "^5.6.2", "ondram/ci-detector": "^4.2", "phpstan/phpdoc-parser": "^2.3", - "phpstan/phpstan": "^2.1.32", + "phpstan/phpstan": "2.1.19 as 2.1.32", "react/event-loop": "^1.6", "react/promise": "^3.3", "react/socket": "^1.17", diff --git a/tests/PhpParser/Printer/Fixture/some_array_map.php b/tests/PhpParser/Printer/Fixture/some_array_map.php new file mode 100644 index 00000000000..824ecfc90d8 --- /dev/null +++ b/tests/PhpParser/Printer/Fixture/some_array_map.php @@ -0,0 +1,5 @@ + $value); diff --git a/tests/PhpParser/Printer/PHPStanPrinterTest.php b/tests/PhpParser/Printer/PHPStanPrinterTest.php new file mode 100644 index 00000000000..774a923f9e5 --- /dev/null +++ b/tests/PhpParser/Printer/PHPStanPrinterTest.php @@ -0,0 +1,39 @@ +make(Parser::class); + + $stmts = $phpstanParser->parseFile(__DIR__ . '/Fixture/some_array_map.php'); + + // get private property "parser" + $parserReflectionProperty = new \ReflectionProperty(RichParser::class, 'parser'); + + /** @var \PhpParser\Parser $innerParser */ + $innerParser = $parserReflectionProperty->getValue($phpstanParser); + $tokens = $innerParser->getTokens(); + + $standardPrinter = new Standard([ + 'newline' => "\n", + ]); + $printerContents = $standardPrinter->printFormatPreserving($stmts, $stmts, $tokens); + + $this->assertStringEqualsFile(__DIR__ . '/Fixture/some_array_map.php', $printerContents); + } +} From af3b7fabd9de62cc593526cf05778fda83cfba48 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 28 Nov 2025 08:51:06 +0100 Subject: [PATCH 2/4] bump to phpstan dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5b1e9140662..fd5a23e91d4 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "nikic/php-parser": "^5.6.2", "ondram/ci-detector": "^4.2", "phpstan/phpdoc-parser": "^2.3", - "phpstan/phpstan": "2.1.19 as 2.1.32", + "phpstan/phpstan": "2.1.x-dev", "react/event-loop": "^1.6", "react/promise": "^3.3", "react/socket": "^1.17", From de32119fc7e6b58bb9349a57bcc4b6ba9ae1aa2d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 28 Nov 2025 08:53:47 +0100 Subject: [PATCH 3/4] add test to SortNamedParamRector --- .../Fixture/array_map_named_params.php.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc new file mode 100644 index 00000000000..de77b5f1b0b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc @@ -0,0 +1,17 @@ + $item * 2); + +?> +----- + $item * 2, array: $items); + +?> From 6aceec7227237718e92bf5143a441f2a97e292ed Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 28 Nov 2025 09:13:36 +0100 Subject: [PATCH 4/4] fix newlines --- composer.json | 2 +- tests/PhpParser/Printer/PHPStanPrinterTest.php | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index fd5a23e91d4..26cb49af90e 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "nikic/php-parser": "^5.6.2", "ondram/ci-detector": "^4.2", "phpstan/phpdoc-parser": "^2.3", - "phpstan/phpstan": "2.1.x-dev", + "phpstan/phpstan": "^2.1.32", "react/event-loop": "^1.6", "react/promise": "^3.3", "react/socket": "^1.17", diff --git a/tests/PhpParser/Printer/PHPStanPrinterTest.php b/tests/PhpParser/Printer/PHPStanPrinterTest.php index 774a923f9e5..15a021fa855 100644 --- a/tests/PhpParser/Printer/PHPStanPrinterTest.php +++ b/tests/PhpParser/Printer/PHPStanPrinterTest.php @@ -8,6 +8,7 @@ use PHPStan\Parser\Parser; use PHPStan\Parser\RichParser; use Rector\Testing\PHPUnit\AbstractLazyTestCase; +use ReflectionProperty; /** * Test case for: https://github.com/rectorphp/rector/issues/9492 @@ -23,17 +24,17 @@ public function testAddingCommentOnSomeNodesFail(): void $stmts = $phpstanParser->parseFile(__DIR__ . '/Fixture/some_array_map.php'); // get private property "parser" - $parserReflectionProperty = new \ReflectionProperty(RichParser::class, 'parser'); + $parserReflectionProperty = new ReflectionProperty(RichParser::class, 'parser'); /** @var \PhpParser\Parser $innerParser */ $innerParser = $parserReflectionProperty->getValue($phpstanParser); $tokens = $innerParser->getTokens(); - $standardPrinter = new Standard([ - 'newline' => "\n", - ]); - $printerContents = $standardPrinter->printFormatPreserving($stmts, $stmts, $tokens); + $standard = new Standard([]); + $printerContents = $standard->printFormatPreserving($stmts, $stmts, $tokens); - $this->assertStringEqualsFile(__DIR__ . '/Fixture/some_array_map.php', $printerContents); + $newlineNormalizedContents = str_replace("\r\n", PHP_EOL, $printerContents); + + $this->assertStringEqualsFile(__DIR__ . '/Fixture/some_array_map.php', $newlineNormalizedContents); } }