Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
strategy:
matrix:
php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
Expand All @@ -38,6 +40,10 @@ jobs:
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-

- name: Remove tools that need a newer PHP version
if: matrix.php == '7.2' || matrix.php == '7.3'
run: composer remove --dev --no-update friendsofphp/php-cs-fixer oscarotero/php-cs-fixer-config

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"issues": "https://github.com/php-gettext/PHP-Scanner/issues"
},
"require": {
"php": ">=7.4",
"nikic/php-parser": "^5",
"php": ">=7.2",
"nikic/php-parser": "^4.18 || ^5",
"gettext/gettext": "^5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^8.5 || ^9",
"squizlabs/php_codesniffer": "^3.0",
"oscarotero/php-cs-fixer-config": "^2",
"friendsofphp/php-cs-fixer": "^3"
Expand Down
25 changes: 21 additions & 4 deletions src/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@

class PhpFunctionsScanner implements FunctionsScannerInterface
{
protected Parser $parser;
protected ?array $validFunctions;
/** @var Parser */
protected $parser;

/** @var array|null */
protected $validFunctions;

public function __construct(?array $validFunctions = null, ?Parser $parser = null)
{
$this->validFunctions = $validFunctions;
$this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion();
$this->parser = $parser ?: static::createParser();
}

protected static function createParser(): Parser
{
$factory = new ParserFactory();

// nikic/php-parser 5
if (method_exists($factory, 'createForNewestSupportedVersion')) {
return $factory->createForNewestSupportedVersion();
}

// nikic/php-parser 4
return $factory->create(ParserFactory::PREFER_PHP7);
}

public function scan(string $code, string $filename): array
Expand All @@ -28,7 +44,8 @@ public function scan(string $code, string $filename): array

$visitor = $this->createNodeVisitor($filename);

$traverser = new NodeTraverser($visitor);
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$traverser->traverse($ast);

return $visitor->getFunctions();
Expand Down
47 changes: 30 additions & 17 deletions src/PhpNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

class PhpNodeVisitor implements NodeVisitor
{
protected ?array $validFunctions;
protected string $filename;
protected array $functions = [];
/** @var array|null */
protected $validFunctions;

/** @var Comment[] */
protected array $bufferComments = [];
/** @var string */
protected $filename;

/** @var ParsedFunction[] */
protected $functions = [];

/** @var Node[] */
protected $bufferComments = [];

public function __construct(string $filename, ?array $validFunctions = null)
{
Expand Down Expand Up @@ -86,28 +91,33 @@ protected function createFunction(Expr $node): ParsedFunction
$node->getEndLine()
);

foreach ($node->getComments() as $comment) {
$function->addComment(static::getComment($comment));
}
// nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one.
$seen = [];
$addComments = static function (Node $commentedNode) use ($function, &$seen): void {
foreach ($commentedNode->getComments() as $comment) {
$position = $comment->getStartFilePos();

if ($this->bufferComments) {
foreach ($this->bufferComments as $bufferComment) {
if ($bufferComment->getStartLine() === $node->getStartLine()) {
foreach ($bufferComment->getComments() as $comment) {
$function->addComment(static::getComment($comment));
}
if (!isset($seen[$position])) {
$seen[$position] = true;
$function->addComment(static::getComment($comment));
}
}
};

$addComments($node);

foreach ($this->bufferComments as $bufferComment) {
if ($bufferComment->getStartLine() === $node->getStartLine()) {
$addComments($bufferComment);
}
}

$this->bufferComments = [];

foreach ($node->args as $argument) {
$value = $argument->value;

foreach ($argument->getComments() as $comment) {
$function->addComment(static::getComment($comment));
}
$addComments($argument);

$function->addArgument(static::getValue($value));
}
Expand Down Expand Up @@ -151,6 +161,9 @@ protected static function getValue(Expr $value)
case 'Scalar_String':
case 'Scalar_Int':
case 'Scalar_Float':
// nikic/php-parser 4
case 'Scalar_LNumber':
case 'Scalar_DNumber':
return $value->value;
case 'Expr_BinaryOp_Concat':
$values = [];
Expand Down