StructParserPart   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 41
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getStruct() 0 1 ?
A visitStruct() 0 11 2
A visitTraitUse() 0 7 2
A visitNamespace() 0 5 2
A visitUseStatement() 0 6 2
1
<?php
2
namespace gossi\codegen\parser\visitor\parts;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use PhpParser\Node\Stmt\ClassLike;
6
use PhpParser\Node\Stmt\Namespace_;
7
use PhpParser\Node\Stmt\TraitUse;
8
use PhpParser\Node\Stmt\UseUse;
9
10
trait StructParserPart {
11
12
	/**
13
	 * @return AbstractPhpStruct
14
	 */
15
	abstract protected function getStruct();
16
17 15
	public function visitStruct(ClassLike $node) {
18 15
		$struct = $this->getStruct();
19 15
		$struct->setName($node->name->name);
20
21 15
		if (($doc = $node->getDocComment()) !== null) {
22 4
			$struct->setDocblock($doc->getReformattedText());
23 4
			$docblock = $struct->getDocblock();
24 4
			$struct->setDescription($docblock->getShortDescription());
25 4
			$struct->setLongDescription($docblock->getLongDescription());
26
		}
27 15
	}
28
29 2
	public function visitTraitUse(TraitUse $node) {
30 2
		$struct = $this->getStruct();
31
32 2
		foreach ($node->traits as $trait) {
33 2
			$struct->addTrait(implode('\\', $trait->parts));
34
		}
35 2
	}
36
37 9
	public function visitNamespace(Namespace_ $node) {
38 9
		if ($node->name !== null) {
39 9
			$this->getStruct()->setNamespace(implode('\\', $node->name->parts));
40
		}
41 9
	}
42
43 1
	public function visitUseStatement(UseUse $node) {
44 1
		$name = implode('\\', $node->name->parts);
45 1
		$alias = !empty($node->alias) ? $node->alias : null;
46
47 1
		$this->getStruct()->addUseStatement($name, $alias);
0 ignored issues
show
Bug introduced by
It seems like $alias defined by !empty($node->alias) ? $node->alias : null on line 45 can also be of type object<PhpParser\Node\Identifier>; however, gossi\codegen\model\Abst...ruct::addUseStatement() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48 1
	}
49
50
}
51