DocCommentReplacement   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A leaveNode() 0 18 5
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics\Generator\Visitor;
4
5
use PhpParser\Comment\Doc;
6
use PhpParser\Node;
7
use PhpParser\Node\Stmt\ClassMethod;
8
use PhpParser\NodeVisitorAbstract;
9
use Stratadox\PhpGenerics\Generator\Type\TypeMap;
10
use function sprintf;
11
use function str_replace;
12
13
final class DocCommentReplacement extends NodeVisitorAbstract
14
{
15
    /** @var TypeMap */
16
    private $typeMap;
17
    /** @var string[] */
18
    private $annotations;
19
20
    public function __construct(TypeMap $typeMap, string ...$annotations)
21
    {
22
        $this->typeMap = $typeMap;
23
        $this->annotations = $annotations;
24
    }
25
26
    public function leaveNode(Node $node)
27
    {
28
        if (!$node instanceof ClassMethod) {
29
            return;
30
        }
31
        $doc = $node->getDocComment();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $doc is correct as $node->getDocComment() targeting PhpParser\NodeAbstract::getDocComment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
32
        if (!$doc) {
0 ignored issues
show
introduced by
$doc is of type null, thus it always evaluated to false.
Loading history...
33
            return;
34
        }
35
        $search = [];
36
        $replace = [];
37
        foreach ($this->typeMap as $type => $argument) {
38
            foreach ($this->annotations as $annotation) {
39
                $search[] = sprintf($annotation, $type);
40
                $replace[] = sprintf($annotation, $argument);
41
            }
42
        }
43
        $node->setDocComment(new Doc(str_replace($search, $replace, $doc)));
44
    }
45
}
46