Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/phpstan_printer_test.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;

$items = [1, 2, 3];
$result = array_map(array: $items, callback: fn ($item) => $item * 2);

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;

$items = [1, 2, 3];
$result = array_map(callback: fn ($item) => $item * 2, array: $items);

?>
5 changes: 5 additions & 0 deletions tests/PhpParser/Printer/Fixture/some_array_map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\PhpParser\Printer\Fixture;

$result = array_map(array: [1, 2, 3], callback: fn(int $value) => $value);
40 changes: 40 additions & 0 deletions tests/PhpParser/Printer/PHPStanPrinterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpParser\Printer;

use PhpParser\PrettyPrinter\Standard;
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
* Most likely caused by https://github.com/phpstan/phpstan-src/pull/3763
*/
final class PHPStanPrinterTest extends AbstractLazyTestCase
{
public function testAddingCommentOnSomeNodesFail(): void
{
/** @var RichParser $phpstanParser */
$phpstanParser = $this->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();

$standard = new Standard([]);
$printerContents = $standard->printFormatPreserving($stmts, $stmts, $tokens);

$newlineNormalizedContents = str_replace("\r\n", PHP_EOL, $printerContents);

$this->assertStringEqualsFile(__DIR__ . '/Fixture/some_array_map.php', $newlineNormalizedContents);
}
}
Loading