Skip to content

Commit

Permalink
fix and test magic string resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-therond committed Jan 16, 2025
1 parent eff8976 commit 6d53c47
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/PHPCfg/AstVisitor/MagicStringResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function ($match) {
},
$comment->getText(),
),
$comment->getLine(),
$comment->getFilePos(),
$comment->getStartLine(),
$comment->getStartFilePos(),
);

$node->setDocComment($comment);
Expand Down
63 changes: 63 additions & 0 deletions test/PHPCfg/MagicStringResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg;

use PHPCfg\AstVisitor\NameResolver;
use PHPCfg\AstVisitor\MagicStringResolver;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

class MagicStringResolverTest extends TestCase
{
/** @var Parser */
private $astParser;

protected function setUp(): void
{
$this->astParser = (new ParserFactory())->createForNewestSupportedVersion();
}

public function testIgnoresInvalidParamTypeInDocComment()
{
$doccomment = <<<'DOC'
/**
* @param Foo\foo bar
*/
DOC;

$code = <<<'DOC'
<?php
namespace Foo {
class Test {
/**
* @param foo bar
*/
public function test($foo) {
echo __LINE__;
}
}
}
DOC;

$ast = $this->astParser->parse($code);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new MagicStringResolver());
$traverser->traverse($ast);

$this->assertEquals($doccomment, $ast[0]->stmts[0]->stmts[0]->getDocComment()->getText());
$this->assertEquals(9, $ast[0]->stmts[0]->stmts[0]->stmts[0]->exprs[0]->value);
}
}

0 comments on commit 6d53c47

Please sign in to comment.