Completed
Pull Request — master (#67)
by
unknown
13:16
created

MethodParserVisitor::parseParams()   B

Complexity

Conditions 9
Paths 33

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9.0036

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 27
cts 28
cp 0.9643
rs 7.6764
c 0
b 0
f 0
cc 9
nc 33
nop 2
crap 9.0036
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\codegen\utils\TypeUtils;
12
use gossi\docblock\tags\ParamTag;
13
use PhpParser\Node\Name;
14
use PhpParser\Node\Stmt\ClassMethod;
15
16
class MethodParserVisitor extends StructParserVisitor {
17
18
	use MemberParserPart;
19
	use ValueParserPart;
20
21 9
	public function visitMethod(ClassMethod $node) {
22 9
		$m = new PhpMethod($node->name->name);
23 9
		$m->setAbstract($node->isAbstract());
24 9
		$m->setFinal($node->isFinal());
25 9
		$m->setVisibility($this->getVisibility($node));
26 9
		$m->setStatic($node->isStatic());
27 9
		$m->setReferenceReturned($node->returnsByRef());
28
29 9
		$this->parseMemberDocblock($m, $node->getDocComment());
30 9
		$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...
31 9
		$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...
32 9
		$this->parseBody($m, $node);
33
34 9
		$this->struct->addMethod($m);
35 9
	}
36
37 9
	private function parseParams(PhpMethod $m, ClassMethod $node) {
38 9
		$params = $m->getDocblock()->getTags('param');
39 9
		foreach ($node->params as $param) {
40 6
			$name = $param->var ? $param->var->name : $param->name;
41
42 6
			$p = new PhpParameter();
43 6
			$p->setName($name);
44 6
			$p->setPassedByReference($param->byRef);
45
46 6
            $type = null;
47 6
			if (is_string($param->type)) {
48
				$type = $param->type;
49 6
			} else if ($param->type instanceof Name) {
50 3
                $type = implode('\\', $param->type->parts);
51 3
                $qualifiedType = TypeUtils::guessQualifiedName($this->struct, $type);
52 3
                if ($type !== $qualifiedType) {
53 2
                    $type = $qualifiedType;
54
                } else {
55 1
                    $type = '\\'.$type;
56
                }
57
            }
58
59 6
			if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60 3
                $p->addType($type);
61
            }
62
63 6
			$this->parseValue($p, $param);
64
65
			$tag = $params->find($p, static function (ParamTag $t, $p) {
66 3
				return $t->getVariable() === '$' . $p->getName();
67 6
			});
68
69 6
			if ($tag !== null) {
70 3
			    $types = TypeUtils::expressionToTypes($tag->getType());
71 3
			    foreach ($types as $type) {
72 3
                    $p->addType(TypeUtils::guessQualifiedName($this->struct, $type));
73
                }
74 3
			    $p->setTypeDescription($tag->getDescription());
75
			}
76
77 6
			$m->addParameter($p);
78
		}
79 9
	}
80
81 9
	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...
82 9
		$returns = $m->getDocblock()->getTags('return');
83 9
		if ($returns->size() > 0) {
84 1
			$return = $returns->get(0);
85 1
			$m->addType($return->getType())->setTypeDescription($return->getDescription());
86
		}
87 9
	}
88
89 9
	private function parseBody(PhpMethod &$m, ClassMethod $node) {
90 9
		$stmts = $node->getStmts();
91 9
		if (is_array($stmts) && count($stmts)) {
92 1
			$prettyPrinter = new PrettyPrinter();
93 1
			$m->setBody($prettyPrinter->prettyPrint($stmts));
94
		}
95 9
	}
96
}
97