Completed
Push — master ( 5a561d...2aa34a )
by Thomas
05:13
created

MethodParserVisitor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 15
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C visitMethod() 0 60 10
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\parser\visitor;
5
6
use gossi\codegen\model\PhpMethod;
7
use gossi\codegen\model\PhpParameter;
8
use gossi\codegen\parser\PrettyPrinter;
9
use gossi\codegen\parser\visitor\parts\MemberParserPart;
10
use gossi\codegen\parser\visitor\parts\ValueParserPart;
11
use gossi\docblock\tags\ParamTag;
12
use PhpParser\Node\Name;
13
use PhpParser\Node\Stmt\ClassMethod;
14
15
class MethodParserVisitor extends StructParserVisitor {
16
17
	use MemberParserPart;
18 8
	use ValueParserPart;
19 8
20 8
	public function visitMethod(ClassMethod $node) {
21 8
		$m = new PhpMethod($node->name->name);
22 8
		$m->setAbstract($node->isAbstract());
23 8
		$m->setFinal($node->isFinal());
24 8
		$m->setVisibility($this->getVisibility($node));
25
		$m->setStatic($node->isStatic());
26
		$m->setReferenceReturned($node->returnsByRef());
27 8
28 3
		// docblock
29 3
		if (($doc = $node->getDocComment()) !== null) {
30 3
			$m->setDocblock($doc->getReformattedText());
31 3
			$docblock = $m->getDocblock();
32
			$m->setDescription($docblock->getShortDescription());
33
			$m->setLongDescription($docblock->getLongDescription());
34
		}
35 8
36 8
		// params
37
		$params = $m->getDocblock()->getTags('param');
38
		foreach ($node->params as $param) {
39 5
			$name = $param->var ? $param->var->name : $param->name;
40 5
41 5
			$p = new PhpParameter();
42
			$p->setName($name);
43 5
			$p->setPassedByReference($param->byRef);
44 1
45 5
			if (is_string($param->type)) {
46 2
				$p->setType($param->type);
47
			} else if ($param->type instanceof Name) {
48
				$p->setType(implode('\\', $param->type->parts));
49 5
			}
50
51 5
			$this->parseValue($p, $param);
52 2
53 5
			$tag = $params->find($p, function (ParamTag $t, $p) {
54
				return $t->getVariable() == '$' . $p->getName();
55
			});
56
57
			if ($tag !== null) {
58
				$p->setType($tag->getType(), $tag->getDescription());
59
			}
60
61
			$m->addParameter($p);
62
		}
63
64
		// return type and description
65
		$returns = $m->getDocblock()->getTags('return');
66
		if ($returns->size() > 0) {
67
			$return = $returns->get(0);
68
			$m->setType($return->getType(), $return->getDescription());
69
		}
70
71
		// body
72
		$stmts = $node->getStmts();
73
		if (is_array($stmts) && count($stmts)) {
74
			$prettyPrinter = new PrettyPrinter();
75
			$m->setBody($prettyPrinter->prettyPrint($stmts));
76
		}
77
78
		$this->struct->setMethod($m);
79
	}
80
}