Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPStan] Run PHPStan rich parser in tests to know the types + process code with tolerant parses on 1st run #1790

Merged
merged 2 commits into from Feb 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 9 additions & 14 deletions config/phpstan/parser.neon
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
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
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
@@ -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);
}
}