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 f364011
Show file tree
Hide file tree
Showing 3 changed files with 42 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#'
37 changes: 37 additions & 0 deletions src/PhpParser/Parser/RectorPathRoutingParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Core\PhpParser\Parser;

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 Parser $currentPhpVersionRichParser;

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

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 f364011

Please sign in to comment.