Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

MethodParserVisitor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 13
dl 0
loc 66
ccs 38
cts 38
cp 1
rs 10

1 Method

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