Skip to content
Merged
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: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
strategy:
matrix:
php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
fail-fast: false

steps:
Expand All @@ -41,4 +41,4 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Tests
run: composer test
run: composer test
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"issues": "https://github.com/php-gettext/PHP-Scanner/issues"
},
"require": {
"php": ">=7.2",
"nikic/php-parser": "^4.2",
"php": ">=7.4",
"nikic/php-parser": "^5",
"gettext/gettext": "^5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.0",
"oscarotero/php-cs-fixer-config": "^1.0",
"friendsofphp/php-cs-fixer": "^2.15"
"oscarotero/php-cs-fixer-config": "^2",
"friendsofphp/php-cs-fixer": "^3"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 12 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="All tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="All tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
13 changes: 6 additions & 7 deletions src/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
namespace Gettext\Scanner;

use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;

class PhpFunctionsScanner implements FunctionsScannerInterface
{
protected $parser;
protected $validFunctions;
protected Parser $parser;
protected ?array $validFunctions;

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

public function scan(string $code, string $filename): array
Expand All @@ -27,15 +26,15 @@ public function scan(string $code, string $filename): array
return [];
}

$traverser = new NodeTraverser();
$visitor = $this->createNodeVisitor($filename);
$traverser->addVisitor($visitor);

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

return $visitor->getFunctions();
}

protected function createNodeVisitor(string $filename): NodeVisitor
protected function createNodeVisitor(string $filename): PhpNodeVisitor
{
return new PhpNodeVisitor($filename, $this->validFunctions);
}
Expand Down
37 changes: 23 additions & 14 deletions src/PhpNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\NodeVisitor;

class PhpNodeVisitor implements NodeVisitor
{
protected $validFunctions;
protected $filename;
protected $functions = [];
protected $bufferComments;
protected ?array $validFunctions;
protected string $filename;
protected array $functions = [];

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

public function __construct(string $filename, array $validFunctions = null)
{
Expand All @@ -40,15 +43,17 @@ public function enterNode(Node $node)
if ($name && ($this->validFunctions === null || in_array($name, $this->validFunctions))) {
$this->functions[] = $this->createFunction($node);
} elseif ($node->getComments()) {
$this->bufferComments = $node;
$this->bufferComments[] = $node;
}
return null;
break;

case 'Stmt_Expression':
case 'Stmt_Echo':
case 'Stmt_Return':
case 'Expr_Print':
case 'Expr_Assign':
$this->bufferComments = $node;
return null;
$this->bufferComments[] = $node;
break;
}

return null;
Expand Down Expand Up @@ -85,13 +90,17 @@ protected function createFunction(Expr $node): ParsedFunction
$function->addComment(static::getComment($comment));
}

if ($this->bufferComments && $this->bufferComments->getStartLine() === $node->getStartLine()) {
foreach ($this->bufferComments->getComments() as $comment) {
$function->addComment(static::getComment($comment));
if ($this->bufferComments) {
foreach ($this->bufferComments as $bufferComment) {
if ($bufferComment->getStartLine() === $node->getStartLine()) {
foreach ($bufferComment->getComments() as $comment) {
$function->addComment(static::getComment($comment));
}
}
}
}

$this->bufferComments = null;
$this->bufferComments = [];

foreach ($node->args as $argument) {
$value = $argument->value;
Expand Down Expand Up @@ -140,8 +149,8 @@ protected static function getValue(Expr $value)

switch ($type) {
case 'Scalar_String':
case 'Scalar_LNumber':
case 'Scalar_DNumber':
case 'Scalar_Int':
case 'Scalar_Float':
return $value->value;
case 'Expr_BinaryOp_Concat':
$values = [];
Expand Down
1 change: 0 additions & 1 deletion src/PhpScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Gettext\Scanner;

use Gettext\Translation;
use Gettext\Translations;

/**
* Class to scan PHP files and get gettext translations
Expand Down
36 changes: 0 additions & 36 deletions tests/PhpFunctionsScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,40 +188,4 @@ public function testPhpFunctionsExtractor()
$this->assertSame($file, $function->getFilename());
$this->assertCount(0, $function->getComments());
}

public function _testPhpFunctionsScannerWithDisabledComments()
{
$scanner = new PhpFunctionsScanner();
$scanner->includeComments(false);
$file = __DIR__.'/assets/functions.php';
$code = file_get_contents($file);
$functions = $scanner->scan($code, $file);

$this->assertCount(11, $functions);

foreach ($functions as $function) {
$this->assertCount(0, $function->getComments());
}
}

public function _testPhpFunctionsScannerWithPrefixedComments()
{
$scanner = new PhpFunctionsScanner();
$scanner->includeComments(['ALLOW:']);
$file = __DIR__.'/assets/functions.php';
$code = file_get_contents($file);
$functions = $scanner->scan($code, $file);

$this->assertCount(11, $functions);

//fn12
$function = $functions[10];
$this->assertCount(1, $function->getComments());

$comments = $function->getComments();
$comment = $comments[0];
$this->assertSame(23, $comment->getLine());
$this->assertSame(23, $comment->getLastLine());
$this->assertSame('ALLOW: Related comment 3', $comment->getComment());
}
}
7 changes: 6 additions & 1 deletion tests/PhpScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public function testPhpCodeScanner()

$scanner->scanFile($file);

list('domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3) = $scanner->getTranslations();
/**
* @var Translations $domain1
* @var Translations $domain2
* @var Translations $domain3
*/
['domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3] = $scanner->getTranslations();

$this->assertCount(6, $domain1);
$this->assertCount(4, $domain2);
Expand Down