Completed
Push — master ( 2aa34a...623317 )
by Thomas
01:43
created

MethodParserVisitor::parseParams()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 8.8497
c 0
b 0
f 0
cc 6
nc 13
nop 2
crap 6
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
		$this->parseMemberDocblock($m, $node->getDocComment());
29 3
		$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 3
		$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 3
		$this->parseBody($m, $node);
32
33
		$this->struct->setMethod($m);
34
	}
35 8
36 8
	private function parseParams(PhpMethod $m, ClassMethod $node) {
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
65
	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
		$returns = $m->getDocblock()->getTags('return');
67
		if ($returns->size() > 0) {
68
			$return = $returns->get(0);
69
			$m->setType($return->getType(), $return->getDescription());
70
		}
71
	}
72
73
	private function parseBody(PhpMethod &$m, ClassMethod $node) {
74
		$stmts = $node->getStmts();
75
		if (is_array($stmts) && count($stmts)) {
76
			$prettyPrinter = new PrettyPrinter();
77
			$m->setBody($prettyPrinter->prettyPrint($stmts));
78
		}
79
	}
80
}