Skip to content

Commit

Permalink
Added NativeDecliner
Browse files Browse the repository at this point in the history
  • Loading branch information
DeGraciaMathieu committed Nov 15, 2024
1 parent 6977ee9 commit 208f6e9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
51 changes: 51 additions & 0 deletions app/Infrastructure/Analyze/Adapters/Jerowork/NativeDecliner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Infrastructure\Analyze\Adapters\Jerowork;

use PhpParser\Node;
use PhpParser\Node\Name;
use Jerowork\ClassDependenciesParser\ClassDependencies;
use Jerowork\ClassDependenciesParser\PhpParser\NodeVisitor\InlineFqnParser\Decliner\InlineFqnDecliner;

final class NativeDecliner implements InlineFqnDecliner
{
private array $primitiveTypes = [
'string',
'int',
'float',
'bool',
'array',
'object',
'null',
'void',
'mixed',
'never',
'callable',
'iterable',
'false',
'true',
'self',
'parent',
'static',
'PHP_EOL',
];

public function shouldDecline(Node $parent, Name $name, ClassDependencies $classDependencies): bool
{
$fqn = $name->toString();

return $this->isNativePrimitiveType($fqn) || $this->isNativePhpClass($fqn);
}

private function isNativePrimitiveType(string $fqn): bool
{
return in_array(strtolower($fqn), $this->primitiveTypes, true);
}

private function isNativePhpClass(string $fqn): bool
{
return function_exists($fqn) ||
class_exists($fqn, false) ||
interface_exists($fqn, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use App\Infrastructure\Analyze\Adapters\Jerowork\Visitors\DetectClassTypeVisitor;
use App\Infrastructure\Analyze\Adapters\Jerowork\NativeDecliner;
use Jerowork\ClassDependenciesParser\PhpParser\NodeVisitor\ParseClassFqnNodeVisitor;
use Jerowork\ClassDependenciesParser\PhpParser\NodeVisitor\ParseInlineFqnNodeVisitor;
use Jerowork\ClassDependenciesParser\PhpParser\NodeVisitor\ParseImportedFqnNodeVisitor;
Expand Down Expand Up @@ -33,6 +33,7 @@ public function createTraverser(array &$collectors): NodeTraverserInterface
new NamespaceDecliner(),
new ImportedFqnDecliner(),
new PhpNativeAccessorDecliner(),
new NativeDecliner(),
],
[
new FullyQualifiedNameProcessor(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@
]
);
});

it('exclude native PHP dependencies', function () {

$analyzerServiceAdapter = app(AnalyzerServiceAdapter::class);

$dependencies = $analyzerServiceAdapter->getDependencies(new FileStub('Native.php'));

expect($dependencies->hasNoDependencies())->toBeTrue();
});
15 changes: 15 additions & 0 deletions tests/Unit/Infrastructure/Services/Stubs/Native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Infrastructure\Services\Stubs;

class Native
{
public function foo(): null
{
array_map(function () {
return null;
}, []);

return null;
}
}

0 comments on commit 208f6e9

Please sign in to comment.