Skip to content

Commit

Permalink
Fix PHPStan parser for different PHP version parsing (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 9, 2022
1 parent dbadefc commit bc0eac3
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 108 deletions.
44 changes: 44 additions & 0 deletions .github/disabled-workflows/e2e_php74.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow runs system tests: Use the Rector application from the source
# checkout to process "fixture" projects in e2e/ directory
# to see if those can be processed successfully
name: End to End tests on PHP 7.4

on:
pull_request:
branches:
- main
push:
branches:
- main

env:
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
COMPOSER_ROOT_VERSION: "dev-main"

jobs:
end_to_end:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
directory:
- 'e2e/php74-parse-static'

name: End to end test - ${{ matrix.directory }}

steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none

# run in e2e subdir
-
run: composer install --ansi
working-directory: ${{ matrix.directory }}

# run e2e test - there should be no reports
- run: vendor/bin/rector --ansi --dry-run
working-directory: ${{ matrix.directory }}
27 changes: 27 additions & 0 deletions .github/workflows/downgrade_phpstan_parser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ref https://github.com/rectorphp/rector/issues/6970
name: Downgrade PHPStan Parser

on:
pull_request: null

env:
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
COMPOSER_ROOT_VERSION: "dev-main"

jobs:
downgrade_phpstan_parser:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

# see https://github.com/shivammathur/setup-php
-
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none

- uses: "ramsey/composer-install@v1"

- run: bin/rector p vendor/clue/ndjson-react/src/Decoder.php --config tests/config/downgrade-phpstan-parser.php --dry-run
20 changes: 4 additions & 16 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,15 @@ services:
arguments:
originalParser: @rectorParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
autowired: no

cachedRectorSimpleParser:
class: PHPStan\Parser\CachedParser
arguments:
originalParser: @rectorSimpleParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%

rectorSimpleParser:
class: PHPStan\Parser\CleaningParser
arguments:
wrappedParser: @currentPhpVersionSimpleDirectParser
autowired: no
autowired: false

pathRoutingParser:
class: Rector\Core\PhpParser\Parser\RectorPathRoutingParser
class: PHPStan\Parser\PathRoutingParser
arguments:
currentPhpVersionRichParser: @cachedRectorParser
currentPhpVersionSimpleParser: @cachedRectorSimpleParser
currentPhpVersionSimpleParser: @cachedRectorParser
php8Parser: @php8Parser
autowired: no
autowired: false

rectorParser:
class: PHPStan\Parser\RichParser
Expand Down
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
__DIR__ . '/../src/PhpParser/ValueObject',
__DIR__ . '/../src/functions',
__DIR__ . '/../src/constants.php',

]);

$services->alias(Application::class, ConsoleApplication::class);
Expand Down
11 changes: 11 additions & 0 deletions e2e/php74-parse-static/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"require": {
"php": "7.4.*",
"api-platform/core": "^2.6",
"symfony/framework-bundle": "^5.4",
"symfony/http-client": "^5.4"
},
"require-dev": {
"rector/rector": "dev-main"
}
}
19 changes: 19 additions & 0 deletions e2e/php74-parse-static/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PARALLEL, true);
$parameters->set(Option::PATHS, [
__DIR__ . '/tests',
]);

$services = $containerConfigurator->services();
$services->set(DowngradeJsonDecodeNullAssociativeArgRector::class);
};
15 changes: 15 additions & 0 deletions e2e/php74-parse-static/tests/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;

class ApiTest extends ApiTestCase
{
protected function setUp(): void
{
parent::setUp();
static::createClient();
}
}
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,3 @@ parameters:
-
message: '#Array with keys is not allowed\. Use value object to pass data instead#'
path: packages/PhpAttribute/Printer/PhpAttributeGroupFactory.php

# on purpose to fix tests + old PHP version parsing code and keep compatbility
- '#Extending PHPStan\\Parser\\PathRoutingParser is not covered by backward compatibility promise\. The class might change in a minor PHPStan version#'
6 changes: 0 additions & 6 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Rector\Core\Configuration\Option;
use Rector\Nette\Set\NetteSetList;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
Expand Down Expand Up @@ -74,11 +73,6 @@
MyCLabsClassToEnumRector::class,
SpatieEnumClassToEnumRector::class,

// on purpose with private property parent - @todo fix later
ClassPropertyAssignToConstructorPromotionRector::class => [
__DIR__ . '/src/PhpParser/Parser/RectorPathRoutingParser.php',
],

// test paths
'*/tests/**/Fixture/*',
'*/rules-tests/**/Fixture/*',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class ArrayAssoc
{
public function run($data, array $items)
{
$data = \json_decode($data, $items[0]);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class ArrayAssoc
{
public function run($data, array $items)
{
$data = \json_decode($data, $items[0] === null ?: $items[0]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

/**
* @see \Clue\React\NDJson\Decoder
*/
final class ClueJsonDecode
{
private $assoc;
private $depth;
private $options;

public function run($data)
{
if ($this->options === 0) {
$data = \json_decode($data, $this->assoc, $this->depth);
} else {
$data = \json_decode($data, $this->assoc, $this->depth, $this->options);
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

/**
* @see \Clue\React\NDJson\Decoder
*/
final class ClueJsonDecode
{
private $assoc;
private $depth;
private $options;

public function run($data)
{
if ($this->options === 0) {
$data = \json_decode($data, $this->assoc === null ?: $this->assoc, $this->depth);
} else {
$data = \json_decode($data, $this->assoc === null ?: $this->assoc, $this->depth, $this->options);
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class PossiblyNull
{
function run(string $json, ?bool $associative)
{
if ($associative === null) {
$associative = true;
}
$value = json_decode($json, $associative);
$value = json_decode($json, $associative === null ?: $associative);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class SkipAlreadyDowngraded
{
public function run(string $json, $associative)
{
$value = json_decode($json, $associative === null ?: $associative);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class SkipClueJsonDecodeFilledInConstructor
{
private $assoc;
private $depth;

/**
* @param bool $assoc
*/
public function __construct($assoc = false)
{
$this->assoc = $assoc;
}

public function run($data)
{
$data = \json_decode($data, $this->assoc, $this->depth);
}
}

0 comments on commit bc0eac3

Please sign in to comment.