Code Duplication    Length = 53-65 lines in 2 locations

src/Padawan/Parser/MethodParser.php 1 location

@@ 12-76 (lines=65) @@
9
use PhpParser\Node\Param;
10
use PhpParser\Node\Name;
11
12
class MethodParser {
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

src/Padawan/Parser/Transformer/FunctionTransformer.php 1 location

@@ 13-65 (lines=53) @@
10
use Padawan\Parser\ParamParser;
11
use Padawan\Parser\UseParser;
12
13
class FunctionTransformer
14
{
15
    public function __construct(
16
        CommentParser $commentParser,
17
        ParamParser $paramParser,
18
        UseParser $useParser
19
    ) {
20
        $this->commentParser = $commentParser;
21
        $this->paramParser = $paramParser;
22
        $this->useParser = $useParser;
23
    }
24
    public function tranform(Function_ $node)
25
    {
26
        $function = new FunctionData($node->name);
27
        $function->startLine = $node->getAttribute("startLine");
28
        $function->endLine = $node->getAttribute("endLine");
29
        $this->parseComments($function, $node->getAttribute("comments"));
30
        foreach ($node->params AS $child) {
31
            if ($child instanceof Param) {
32
                $function->addArgument($this->tranformArgument($child));
33
            }
34
        }
35
        return $function;
36
    }
37
    protected function parseComments(FunctionData $function, $comments)
38
    {
39
        if (is_array($comments)) {
40
            /** @var Comment */
41
            $comment = $this->commentParser->parse(
42
                $comments[count($comments) - 1]->getText()
43
            );
44
            if ($comment->isInheritDoc()) {
45
                $function->doc = Comment::INHERIT_MARK;
46
            } else {
47
                $function->doc = $comment->getDoc();
48
                $function->return = $comment->getReturn();
49
                foreach ($comment->getVars() as $var) {
50
                    if ($var instanceof MethodParam) {
51
                        $function->addParam($var);
52
                    }
53
                }
54
            }
55
        }
56
    }
57
    protected function tranformArgument(Param $node)
58
    {
59
        return $this->paramParser->parse($node);
60
    }
61
62
    private $paramParser;
63
    private $commentParser;
64
    private $useParser;
65
}
66