Skip to content

Commit

Permalink
[PHPStan] Run PHPStan rich parser in tests to know the types + proces…
Browse files Browse the repository at this point in the history
…s code with tolerant parses on 1st run (#1790)
  • Loading branch information
TomasVotruba committed Feb 9, 2022
1 parent 94ef798 commit e9fb346
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
23 changes: 9 additions & 14 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@ services:
arguments:
originalParser: @rectorParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
autowired: false
autowired: no

cachedRectorSimpleParser:
class: PHPStan\Parser\CachedParser
rectorSimpleParser:
class: PHPStan\Parser\CleaningParser
arguments:
originalParser: @rectorSimpleParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
autowired: false
wrappedParser: @currentPhpVersionSimpleDirectParser
autowired: no

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

rectorParser:
class: PHPStan\Parser\RichParser
arguments:
parser: @currentPhpVersionPhpParser
lexer: @currentPhpVersionLexer
autowired: no

rectorSimpleParser:
class: PHPStan\Parser\SimpleParser
arguments:
parser: @currentPhpVersionPhpParser
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,6 @@ 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: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -73,6 +74,11 @@
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
43 changes: 43 additions & 0 deletions src/PhpParser/Parser/RectorPathRoutingParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Rector\Core\PhpParser\Parser;

use PhpParser\Node\Stmt;
use PHPStan\File\FileHelper;
use PHPStan\Parser\Parser;
use PHPStan\Parser\PathRoutingParser;

/**
* @api Used in PHPStan internals for parsing nodes:
* 1) with types for tests:
* 2) removing unsupported PHP-version code on real run
*/
final class RectorPathRoutingParser extends PathRoutingParser
{
private readonly Parser $currentPhpVersionRichParser;

public function __construct(
FileHelper $fileHelper,
Parser $currentPhpVersionRichParser,
Parser $currentPhpVersionSimpleParser,
Parser $php8Parser
) {
$this->currentPhpVersionRichParser = $currentPhpVersionRichParser;
parent::__construct($fileHelper, $currentPhpVersionRichParser, $currentPhpVersionSimpleParser, $php8Parser);
}

/**
* @return Stmt[]
*/
public function parseFile(string $file): array
{
// for tests, always parse nodes with directly rich parser to be aware of types
if (defined('PHPUNIT_COMPOSER_INSTALL')) {
return $this->currentPhpVersionRichParser->parseFile($file);
}

return parent::parseFile($file);
}
}

0 comments on commit e9fb346

Please sign in to comment.