Skip to content

Commit

Permalink
simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 2, 2024
1 parent dafc720 commit 0ba5d72
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 53 deletions.
1 change: 0 additions & 1 deletion config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ services:
arguments:
- %type_perfect%

- Rector\TypePerfect\NodeTraverser\SimpleCallableNodeTraverser
- Rector\TypePerfect\NodeFinder\ClassMethodNodeFinder
- Rector\TypePerfect\NodeFinder\MethodCallNodeFinder
- Rector\TypePerfect\NodeFinder\ReturnNodeFinder
Expand Down
22 changes: 15 additions & 7 deletions src/NodeFinder/ReturnNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use Rector\TypePerfect\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\TypePerfect\NodeVisitor\CallableNodeVisitor;

final readonly class ReturnNodeFinder
{
public function __construct(
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}

public function findOnlyReturnsExpr(ClassMethod $classMethod): Expr|null
{
$returns = $this->findReturnsWithValues($classMethod);
Expand All @@ -37,7 +32,7 @@ public function findReturnsWithValues(ClassMethod $classMethod): array
{
$returns = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, static function (
$this->traverseNodesWithCallable((array) $classMethod->stmts, static function (
Node $node
) use (&$returns) {
// skip different scope
Expand All @@ -58,4 +53,17 @@ public function findReturnsWithValues(ClassMethod $classMethod): array

return $returns;
}

/**
* @param callable(Node $node): (int|Node|null) $callable
* @param Node[] $nodes
*/
private function traverseNodesWithCallable(array $nodes, callable $callable): void
{
$nodeTraverser = new NodeTraverser();

$callableNodeVisitor = new CallableNodeVisitor($callable);
$nodeTraverser->addVisitor($callableNodeVisitor);
$nodeTraverser->traverse($nodes);
}
}
39 changes: 0 additions & 39 deletions src/NodeTraverser/SimpleCallableNodeTraverser.php

This file was deleted.

1 change: 0 additions & 1 deletion src/NodeVisitor/CallableNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function __construct(callable $callable)
public function enterNode(Node $node): int|Node|null
{
$originalNode = $node;

$callable = $this->callable;

/** @var int|Node|null $newNode */
Expand Down
17 changes: 12 additions & 5 deletions src/PhpDoc/ApiDocStmtAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ final class ApiDocStmtAnalyzer
{
public function hasApiDoc(ClassMethod $classMethod, ClassReflection $classReflection): bool
{
if ($classReflection->getResolvedPhpDoc() instanceof ResolvedPhpDocBlock) {
$resolvedPhpDoc = $classReflection->getResolvedPhpDoc();
if (str_contains($resolvedPhpDoc->getPhpDocString(), '@api')) {
return true;
}
if ($this->hasClassReflectionApiDoc($classReflection)) {
return true;
}

$docComment = $classMethod->getDocComment();
Expand All @@ -27,4 +24,14 @@ public function hasApiDoc(ClassMethod $classMethod, ClassReflection $classReflec

return str_contains($docComment->getText(), '@api');
}

private function hasClassReflectionApiDoc(ClassReflection $classReflection): bool
{
if (! $classReflection->getResolvedPhpDoc() instanceof ResolvedPhpDocBlock) {
return false;
}

$resolvedPhpDoc = $classReflection->getResolvedPhpDoc();
return str_contains($resolvedPhpDoc->getPhpDocString(), '@api');
}
}

0 comments on commit 0ba5d72

Please sign in to comment.