Skip to content

Commit

Permalink
add support for nikic/php-parser 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Jun 24, 2024
1 parent 4b668fd commit 1b5421d
Show file tree
Hide file tree
Showing 23 changed files with 277 additions and 51 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"require": {
"php": "^8.1",
"nikic/php-parser": "^4.19.1",
"nikic/php-parser": "^4.19.1|^v5.0.2",
"pestphp/pest-plugin": "^v2.1.1",
"psr/simple-cache": "^3.0"
},
Expand Down
10 changes: 7 additions & 3 deletions src/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Pest\Mutate;

use Pest\Exceptions\ShouldNotHappen;
use Pest\Mutate\Support\PhpParserFactory;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
Expand Down Expand Up @@ -51,8 +51,12 @@ public static function create(
$modifiedSourcePath = self::TMP_FOLDER.DIRECTORY_SEPARATOR.hash('xxh3', $modifiedSource);
file_put_contents($modifiedSourcePath, $modifiedSource);

$orignalAst = (new ParserFactory)->create(ParserFactory::PREFER_PHP7)->parse($file->getContents());
$newlyRenderedOriginalSource = (new Standard())->prettyPrintFile($orignalAst); // @phpstan-ignore-line
$parser = PhpParserFactory::make();
$orignalAst = $parser->parse($file->getContents());

assert($orignalAst !== null);

$newlyRenderedOriginalSource = (new Standard())->prettyPrintFile($orignalAst);

$endLine = $originalNode->getEndLine();

Expand Down
8 changes: 6 additions & 2 deletions src/Mutators/Number/DecrementFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\Float_;

class DecrementFloat extends AbstractMutator
{
Expand All @@ -21,12 +22,15 @@ class DecrementFloat extends AbstractMutator

public static function nodesToHandle(): array
{
return [DNumber::class];
return [
DNumber::class,
Float_::class,
];
}

public static function mutate(Node $node): Node
{
/** @var Node\Scalar\LNumber $node */
/** @var Node\Scalar\LNumber|Node\Scalar\Float_ $node */
$node->value--;

return $node;
Expand Down
8 changes: 6 additions & 2 deletions src/Mutators/Number/DecrementInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\DeclareDeclare;

Expand All @@ -23,7 +24,10 @@ class DecrementInteger extends AbstractMutator

public static function nodesToHandle(): array
{
return [LNumber::class];
return [
LNumber::class,
Int_::class,
];
}

public static function can(Node $node): bool
Expand All @@ -39,7 +43,7 @@ public static function can(Node $node): bool

public static function mutate(Node $node): Node
{
/** @var Node\Scalar\LNumber $node */
/** @var Node\Scalar\LNumber|Node\Scalar\Int_ $node */
$node->value -= $node->getAttribute('parent') instanceof UnaryMinus ? -1 : 1;

return $node;
Expand Down
8 changes: 6 additions & 2 deletions src/Mutators/Number/IncrementFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\Float_;

class IncrementFloat extends AbstractMutator
{
Expand All @@ -21,12 +22,15 @@ class IncrementFloat extends AbstractMutator

public static function nodesToHandle(): array
{
return [DNumber::class];
return [
DNumber::class,
Float_::class,
];
}

public static function mutate(Node $node): Node
{
/** @var Node\Scalar\LNumber $node */
/** @var Node\Scalar\LNumber|Node\Scalar\Float_ $node */
$node->value++;

return $node;
Expand Down
8 changes: 6 additions & 2 deletions src/Mutators/Number/IncrementInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\DeclareDeclare;

Expand All @@ -23,7 +24,10 @@ class IncrementInteger extends AbstractMutator

public static function nodesToHandle(): array
{
return [LNumber::class];
return [
LNumber::class,
Int_::class,
];
}

public static function can(Node $node): bool
Expand All @@ -39,7 +43,7 @@ public static function can(Node $node): bool

public static function mutate(Node $node): Node
{
/** @var Node\Scalar\LNumber $node */
/** @var Node\Scalar\LNumber|Node\Scalar\Int_ $node */
$node->value += $node->getAttribute('parent') instanceof UnaryMinus ? -1 : 1;

return $node;
Expand Down
5 changes: 4 additions & 1 deletion src/Mutators/Removal/RemoveArrayItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class RemoveArrayItem extends AbstractMutator

public static function nodesToHandle(): array
{
return [ArrayItem::class];
return [
ArrayItem::class,
Node\ArrayItem::class,
];
}

public static function mutate(Node $node): int
Expand Down
2 changes: 1 addition & 1 deletion src/Mutators/Return/AlwaysReturnNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function can(Node $node): bool
return false;
}

if ($node->expr instanceof ConstFetch && $node->expr->name->parts[0] === 'null') {
if ($node->expr instanceof ConstFetch && $node->expr->name->getParts()[0] === 'null') {
return false;
}

Expand Down
9 changes: 6 additions & 3 deletions src/Support/MutationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\SplFileInfo;

class MutationGenerator
Expand Down Expand Up @@ -56,7 +55,7 @@ public function generate(
}
}

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$parser = PhpParserFactory::make();

$mutators = $this->filterMutators($mutators, $contents, $parser);

Expand All @@ -82,7 +81,11 @@ public function generate(
trackMutation: $this->trackMutation(...),
));

$modifiedAst = $traverser->traverse($parser->parse($contents)); // @phpstan-ignore-line
$stmts = $parser->parse($contents);

assert($stmts !== null);

$modifiedAst = $traverser->traverse($stmts);

if (! $this->hasMutated()) {
break;
Expand Down
25 changes: 25 additions & 0 deletions src/Support/PhpParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Support;

use PhpParser\Parser;
use PhpParser\ParserFactory;

class PhpParserFactory
{
public static function make(): Parser
{
if (self::version() === 4) {
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7); // @phpstan-ignore-line
}

return (new ParserFactory)->createForNewestSupportedVersion();
}

public static function version(): int
{
return method_exists(ParserFactory::class, 'create') ? 4 : 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
public static function isAdult(int $age): bool
{
return $age > 18;
}
public static function isSenior(int $age) : bool
public static function isSenior(int $age): bool
{
return $age >= 65;
}
public static function isImmature(int $age) : bool
public static function isImmature(int $age): bool
{
return $age < 18;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
public static function isAdult(int $age): bool
{
return $age >= 18;
}
public static function isSenior(int $age) : bool
public static function isSenior(int $age): bool
{
return $age > 65;
}
public static function isImmature(int $age) : bool
public static function isImmature(int $age): bool
{
return $age < 18;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
public static function isAdult(int $age): bool
{
return $age >= 18;
}
public static function isSenior(int $age) : bool
public static function isSenior(int $age): bool
{
return $age >= 65;
}
public static function isImmature(int $age) : bool
public static function isImmature(int $age): bool
{
return $age <= 18;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare (strict_types=1);
namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
{
return $age > 18;
}
public static function isSenior(int $age) : bool
{
return $age >= 65;
}
public static function isImmature(int $age) : bool
{
return $age < 18;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare (strict_types=1);
namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
{
return $age >= 18;
}
public static function isSenior(int $age) : bool
{
return $age > 65;
}
public static function isImmature(int $age) : bool
{
return $age < 18;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare (strict_types=1);
namespace Tests\Fixtures\Classes;

class AgeHelper
{
public static function isAdult(int $age) : bool
{
return $age >= 18;
}
public static function isSenior(int $age) : bool
{
return $age >= 65;
}
public static function isImmature(int $age) : bool
{
return $age <= 18;
}
}
4 changes: 2 additions & 2 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
declare(strict_types=1);

use Pest\Mutate\Factories\NodeTraverserFactory;
use Pest\Mutate\Support\PhpParserFactory;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;

function mutateCode(string $mutator, string $code): string
{
$stmts = (new ParserFactory)->create(ParserFactory::PREFER_PHP7)->parse($code);
$stmts = PhpParserFactory::make()->parse($code);

$mutationCount = 0;

Expand Down
Loading

0 comments on commit 1b5421d

Please sign in to comment.