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

MethodParserVisitor::visitMethod()   C

Complexity

Conditions 10
Paths 104

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 6.9793
c 0
b 0
f 0
cc 10
nc 104
nop 1
crap 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}