Skip to content

Commit

Permalink
Support nikic/php-parser ^5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 22, 2023
1 parent e19555d commit a266e83
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-9.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [9.2.30] - 2023-12-DD

### Changed

* This component is now compatible with `nikic/php-parser` 5.0

## [9.2.29] - 2023-09-19

### Fixed
Expand Down Expand Up @@ -499,6 +505,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

* This component is no longer supported on PHP 7.1

[9.2.30]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.2.29...9.2
[9.2.29]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.2.28...9.2.29
[9.2.28]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.2.27...9.2.28
[9.2.27]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.2.26...9.2.27
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"sort-packages": true
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=7.3",
"ext-dom": "*",
Expand Down
16 changes: 16 additions & 0 deletions src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function enterNode(Node $node): void
$node instanceof Node\Expr\ConstFetch ||
$node instanceof Node\Expr\Match_ ||
$node instanceof Node\Expr\Variable ||
$node instanceof Node\Expr\Throw_ ||
$node instanceof Node\ComplexType ||
$node instanceof Node\Const_ ||
$node instanceof Node\Identifier ||
Expand All @@ -121,12 +122,27 @@ public function enterNode(Node $node): void
return;
}

/*
* nikic/php-parser ^4.18 represents <code>throw</code> statements
* as <code>Stmt\Throw_</code> objects
*/
if ($node instanceof Node\Stmt\Throw_) {
$this->setLineBranch($node->expr->getEndLine(), $node->expr->getEndLine(), ++$this->nextBranch);

return;
}

/*
* nikic/php-parser ^5 represents <code>throw</code> statements
* as <code>Stmt\Expression</code> objects that contain an
* <code>Expr\Throw_</code> object
*/
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
$this->setLineBranch($node->expr->expr->getEndLine(), $node->expr->expr->getEndLine(), ++$this->nextBranch);

return;
}

if ($node instanceof Node\Stmt\Enum_ ||
$node instanceof Node\Stmt\Function_ ||
$node instanceof Node\Stmt\Class_ ||
Expand Down
6 changes: 1 addition & 5 deletions src/StaticAnalysis/ParsingFileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function token_get_all;
use function trim;
use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
Expand Down Expand Up @@ -142,10 +141,7 @@ private function analyse(string $filename): void
$linesOfCode = 1;
}

$parser = (new ParserFactory)->create(
ParserFactory::PREFER_PHP7,
new Lexer
);
$parser = (new ParserFactory)->createForHostVersion();

try {
$nodes = $parser->parse($source);
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function testHandlesFunctionOrMethodWithDisjunctiveNormalFormTypes(): voi

private function findCodeUnits(string $filename): CodeUnitFindingVisitor
{
$nodes = (new ParserFactory)->create(ParserFactory::PREFER_PHP7)->parse(
$nodes = (new ParserFactory)->createForHostVersion()->parse(
file_get_contents($filename)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function file_get_contents;
use function preg_match;
use function strpos;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -62,11 +61,8 @@ public function testExecutableLinesAreGroupedByBranchPhp82(): void

private function doTestSelfDescribingAsset(string $filename): void
{
$source = file_get_contents($filename);
$parser = (new ParserFactory)->create(
ParserFactory::PREFER_PHP7,
new Lexer
);
$source = file_get_contents($filename);
$parser = (new ParserFactory)->createForHostVersion();
$nodes = $parser->parse($source);
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);

Expand Down

0 comments on commit a266e83

Please sign in to comment.