MethodParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 65
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
C parse() 31 31 7
A parseMethodArgument() 3 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Padawan\Parser;
4
5
use Padawan\Domain\Project\Node\MethodData;
6
use Padawan\Domain\Project\Node\MethodParam;
7
use Padawan\Domain\Project\Node\Comment;
8
use PhpParser\Node\Stmt\ClassMethod;
9
use PhpParser\Node\Param;
10
use PhpParser\Node\Name;
11
12 View Code Duplication
class MethodParser {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
14
    /**
15
     * Constructs
16
     *
17
     * @param UseParser $useParser
18
     */
19
    public function __construct(
20
        UseParser $useParser,
21
        CommentParser $commentParser,
22
        ParamParser $paramParser
23
    )
24
    {
25
        $this->useParser        = $useParser;
26
        $this->commentParser    = $commentParser;
27
        $this->paramParser      = $paramParser;
28
    }
29
30
    /**
31
     * Parses ClassMethod node to MethodData
32
     *
33
     * @return MethodData
34
     */
35
    public function parse(ClassMethod $node)
36
    {
37
        $method = new MethodData($node->name);
38
        $method->startLine = $node->getAttribute("startLine");
39
        $method->endLine = $node->getAttribute("endLine");
40
        $method->setType($node->type);
41
        $comments = $node->getAttribute("comments");
42
        if (is_array($comments)) {
43
            /** @var Comment */
44
            $comment = $this->commentParser->parse(
45
                $comments[count($comments) - 1]->getText()
46
            );
47
            if ($comment->isInheritDoc()) {
48
                $method->doc = Comment::INHERIT_MARK;
49
            } else {
50
                $method->doc = $comment->getDoc();
51
                $method->return = $comment->getReturn();
52
                foreach ($comment->getVars() as $var) {
53
                    if ($var instanceof MethodParam) {
54
                        $method->addParam($var);
55
                    }
56
                }
57
            }
58
        }
59
        foreach ($node->params AS $child) {
60
            if ($child instanceof Param) {
61
                $method->addParam($this->parseMethodArgument($child));
62
            }
63
        }
64
        return $method;
65
    }
66
    protected function parseMethodArgument(Param $node) {
67
        return $this->paramParser->parse($node);
68
    }
69
70
    /** @var UseParser $useParser */
71
    private $useParser;
72
    /** @property CommentParser $commentParser */
73
    private $commentParser;
74
    /** @var ParamParser */
75
    private $paramParser;
76
}
77