diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5e2b7046..9a763720 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -60,3 +60,6 @@ parameters: path: 'src/PhpParser/NodeVisitor/ClassAliasStmtAppender.php' - message: '#PhpVersion::fromComponents#' path: 'tests/SpecFramework/SpecScenario.php' + # Fixed in https://github.com/nikic/PHP-Parser/pull/1003 + - message: '#Standard constructor expects array#' + path: 'src/Container.php' diff --git a/specs/misc/nowdoc.php b/specs/misc/nowdoc.php index 21ccbe92..556b8fa4 100644 --- a/specs/misc/nowdoc.php +++ b/specs/misc/nowdoc.php @@ -13,6 +13,7 @@ */ use Humbug\PhpScoper\SpecFramework\Config\Meta; +use Humbug\PhpScoper\SpecFramework\Config\SpecWithConfig; return [ 'meta' => new Meta( @@ -171,4 +172,46 @@ PHP_HEREDOC; PHP, + + // As per the RFC: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes + 'Nowdoc and Heredoc indentation' => SpecWithConfig::create( + phpVersionUsed: 70_200, + spec: <<<'PHP' + parser)) { - $this->phpVersion = $phpVersion; + $this->parserPhpVersion = $phpVersion; $this->parser = $this->createParser($phpVersion); } - $parserVersion = $this->phpVersion; - - $parserMessage = 'Cannot use the existing parser: its PHP version is different than the one requested.'; - - if (null === $parserVersion) { - Assert::null($phpVersion, $parserMessage); - } else { - Assert::notNull($phpVersion, $parserMessage); - Assert::true($parserVersion->equals($phpVersion), $parserMessage); - } + self::checkSamePhpVersion($this->parserPhpVersion, $phpVersion); return $this->parser; } @@ -130,14 +122,33 @@ public function getEnrichedReflectorFactory(): EnrichedReflectorFactory return $this->enrichedReflectorFactory; } - public function getPrinter(): Printer + public function getPrinter(?PhpVersion $phpVersion = null): Printer { if (!isset($this->printer)) { + $this->printerPhpVersion = $phpVersion; $this->printer = new StandardPrinter( - new Standard(), + new Standard([ + 'phpVersion' => $phpVersion, + ]), ); } + self::checkSamePhpVersion($this->printerPhpVersion, $phpVersion); + return $this->printer; } + + private static function checkSamePhpVersion( + ?PhpVersion $versionUsed, + ?PhpVersion $versionRequest, + ): void { + $parserMessage = 'Cannot use the existing parser: its PHP version is different than the one requested.'; + + if (null === $versionUsed) { + Assert::null($versionRequest, $parserMessage); + } else { + Assert::notNull($versionRequest, $parserMessage); + Assert::true($versionUsed->equals($versionRequest), $parserMessage); + } + } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 4e71c61e..a7ca72ca 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -74,6 +74,19 @@ public function test_it_can_get_the_parser_if_the_version_does_not_change( $this->addToAssertionCount(1); } + #[DataProvider('samePhpVersionProvider')] + public function test_it_can_get_the_printer_if_the_version_does_not_change( + ?PhpVersion $version1, + ?PhpVersion $version2, + ): void { + $container = new Container(); + + $container->getPrinter($version1); + $container->getPrinter($version2); + + $this->addToAssertionCount(1); + } + public static function samePhpVersionProvider(): iterable { yield 'no PHP version configured' => [ @@ -108,6 +121,20 @@ public function test_it_cannot_create_two_different_versions_of_the_parser( $container->getParser($version2); } + #[DataProvider('differentPhpVersionProvider')] + public function test_it_cannot_create_two_different_versions_of_the_printer( + ?PhpVersion $version1, + ?PhpVersion $version2, + ): void { + $container = new Container(); + + $container->getPrinter($version1); + + $this->expectException(InvalidArgumentException::class); + + $container->getPrinter($version2); + } + public static function differentPhpVersionProvider(): iterable { $phpVersion = PhpVersion::fromString('7.2'); diff --git a/tests/Scoper/PhpScoperSpecTest.php b/tests/Scoper/PhpScoperSpecTest.php index a7c028cf..3432da2e 100644 --- a/tests/Scoper/PhpScoperSpecTest.php +++ b/tests/Scoper/PhpScoperSpecTest.php @@ -136,7 +136,7 @@ private static function createScoper( $prefix, $symbolsRegistry, ), - $container->getPrinter(), + $container->getPrinter($phpVersionUsed), ); }