MethodParserVisitor   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 14
dl 0
loc 66
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A visitMethod() 0 15 1
B parseParams() 0 28 6
A parseType() 0 7 2
A parseBody() 0 7 3
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
	use ValueParserPart;
19
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 8
		$m->setStatic($node->isStatic());
26 8
		$m->setReferenceReturned($node->returnsByRef());
27
28 8
		$this->parseMemberDocblock($m, $node->getDocComment());
29 8
		$this->parseParams($m, $node);
0 ignored issues
show
Documentation introduced by
$m is of type object<gossi\codegen\mod...egen\model\PhpConstant>, but the function expects a object<gossi\codegen\model\PhpMethod>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30 8
		$this->parseType($m, $node);
0 ignored issues
show
Documentation introduced by
$m is of type object<gossi\codegen\mod...egen\model\PhpConstant>, but the function expects a object<gossi\codegen\model\PhpMethod>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31 8
		$this->parseBody($m, $node);
32
33 8
		$this->struct->setMethod($m);
34 8
	}
35
36 8
	private function parseParams(PhpMethod $m, ClassMethod $node) {
37 8
		$params = $m->getDocblock()->getTags('param');
38 8
		foreach ($node->params as $param) {
39 5
			$name = $param->var ? $param->var->name : $param->name;
40
41 5
			$p = new PhpParameter();
42 5
			$p->setName($name);
43 5
			$p->setPassedByReference($param->byRef);
44
45 5
			if (is_string($param->type)) {
46
				$p->setType($param->type);
47 5
			} else if ($param->type instanceof Name) {
48 2
				$p->setType(implode('\\', $param->type->parts));
49
			}
50
51 5
			$this->parseValue($p, $param);
52
53
			$tag = $params->find($p, function (ParamTag $t, $p) {
54 2
				return $t->getVariable() == '$' . $p->getName();
55 5
			});
56
57 5
			if ($tag !== null) {
58 2
				$p->setType($tag->getType(), $tag->getDescription());
59
			}
60
61 5
			$m->addParameter($p);
62
		}
63 8
	}
64
65 8
	private function parseType(PhpMethod &$m, ClassMethod $node) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66 8
		$returns = $m->getDocblock()->getTags('return');
67 8
		if ($returns->size() > 0) {
68 1
			$return = $returns->get(0);
69 1
			$m->setType($return->getType(), $return->getDescription());
70
		}
71 8
	}
72
73 8
	private function parseBody(PhpMethod &$m, ClassMethod $node) {
74 8
		$stmts = $node->getStmts();
75 8
		if (is_array($stmts) && count($stmts)) {
76 1
			$prettyPrinter = new PrettyPrinter();
77 1
			$m->setBody($prettyPrinter->prettyPrint($stmts));
78
		}
79 8
	}
80
}
81