Skip to content

Commit

Permalink
Update to PHP-Parser 5
Browse files Browse the repository at this point in the history
For instrumentation testing, use the newest supported version.
For actual instrumentation, use the host version.
  • Loading branch information
nikic committed Jan 9, 2024
1 parent 3bef788 commit 1ca47c9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"require": {
"php": ">= 7.4",
"nikic/php-parser": "^4.3",
"nikic/php-parser": "^5.0",
"nikic/include-interceptor": "^0.1.1",
"ulrichsg/getopt-php": "^4.0"
},
Expand Down
4 changes: 3 additions & 1 deletion src/Fuzzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpFuzzer\Mutation\Dictionary;
use PhpFuzzer\Mutation\Mutator;
use PhpFuzzer\Mutation\RNG;
use PhpParser\PhpVersion;

final class Fuzzer {
private Interceptor $interceptor;
Expand Down Expand Up @@ -46,7 +47,8 @@ final class Fuzzer {

public function __construct() {
$this->outputDir = getcwd();
$this->instrumentor = new Instrumentor(FuzzingContext::class);
$this->instrumentor = new Instrumentor(
FuzzingContext::class, PhpVersion::getHostVersion());
$this->rng = new RNG();
$this->config = new Config();
$this->mutator = new Mutator($this->rng, $this->config->dictionary);
Expand Down
13 changes: 4 additions & 9 deletions src/Instrumentation/Instrumentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@

namespace PhpFuzzer\Instrumentation;

use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;

final class Instrumentor {
private Parser $parser;
private NodeTraverser $traverser;
private Context $context;

public function __construct(string $runtimeContextName) {
$this->parser = new Parser\Php7(new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startFilePos', 'endFilePos',
],
]));
public function __construct(string $runtimeContextName, PhpVersion $phpVersion) {
$this->parser = (new ParserFactory())->createForVersion($phpVersion);
$this->traverser = new NodeTraverser();
$this->context = new Context($runtimeContextName);
$this->traverser->addVisitor(new Visitor($this->context));
Expand Down
4 changes: 3 additions & 1 deletion test/Instrumentation/InstrumentorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpFuzzer\Instrumentation\FileInfo;
use PhpFuzzer\Instrumentation\Instrumentor;
use PhpFuzzer\Instrumentation\MutableString;
use PhpParser\PhpVersion;
use PHPUnit\Framework\TestCase;

class InstrumentorTest extends TestCase {
Expand Down Expand Up @@ -135,7 +136,8 @@ public function bar();
}
CODE;

$instrumentor = new Instrumentor('InstrumentationContext');
$instrumentor = new Instrumentor(
'InstrumentationContext', PhpVersion::getNewestSupported());
$fileInfo = new FileInfo();
$output = $instrumentor->instrument($input, $fileInfo);
$this->assertSame($expected, $output);
Expand Down

0 comments on commit 1ca47c9

Please sign in to comment.