Skip to content

Commit

Permalink
add own RectorPathRoutingParser that handles test with rich parser by…
Browse files Browse the repository at this point in the history
… default to let PHPStan know the types
  • Loading branch information
TomasVotruba committed Feb 9, 2022
1 parent d9697ea commit 95b5090
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/phpstan/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ services:
autowired: no

pathRoutingParser:
class: PHPStan\Parser\PathRoutingParser
class: Rector\Core\PhpParser\Parser\RectorPathRoutingParser
# class: PHPStan\Parser\PathRoutingParser
arguments:
currentPhpVersionRichParser: @cachedRectorParser
currentPhpVersionSimpleParser: @rectorSimpleParser
Expand Down
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 95b5090

Please sign in to comment.