Completed
Pull Request — master (#67)
by
unknown
02:21
created

PhpClass::getParentClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\model;
5
6
use gossi\codegen\model\parts\AbstractPart;
7
use gossi\codegen\model\parts\ConstantsPart;
8
use gossi\codegen\model\parts\FinalPart;
9
use gossi\codegen\model\parts\InterfacesPart;
10
use gossi\codegen\model\parts\PropertiesPart;
11
use gossi\codegen\model\parts\TraitsPart;
12
use gossi\codegen\parser\FileParser;
13
use gossi\codegen\parser\visitor\ClassParserVisitor;
14
use gossi\codegen\parser\visitor\ConstantParserVisitor;
15
use gossi\codegen\parser\visitor\MethodParserVisitor;
16
use gossi\codegen\parser\visitor\PropertyParserVisitor;
17
18
/**
19
 * Represents a PHP class.
20
 *
21
 * @author Thomas Gossmann
22
 */
23
class PhpClass extends AbstractPhpStruct implements GenerateableInterface, TraitsInterface, ConstantsInterface, PropertiesInterface, PhpTypeInterface {
24
25
	use AbstractPart;
26
	use ConstantsPart;
27
	use FinalPart;
28
	use InterfacesPart;
29
	use PropertiesPart;
30
	use TraitsPart;
31
32
	/** @var string */
33
	private $parentClassName;
34
35
	/**
36
	 * Creates a PHP class from file
37
	 *
38
	 * @param string $filename
39
	 * @return PhpClass
40
	 */
41 13
	public static function fromFile(string $filename): self {
42 13
		$class = new self();
43 13
		$parser = new FileParser($filename);
44 13
		$parser->addVisitor(new ClassParserVisitor($class));
45 13
		$parser->addVisitor(new MethodParserVisitor($class));
46 13
		$parser->addVisitor(new ConstantParserVisitor($class));
47 13
		$parser->addVisitor(new PropertyParserVisitor($class));
48 13
		$parser->parse();
49
50 13
		return $class;
51
	}
52
53
	/**
54
	 * Creates a new PHP class
55
	 *
56
	 * @param string $name the qualified name
57
	 */
58 47
	public function __construct($name = null) {
59 47
		parent::__construct($name);
60 47
		$this->initProperties();
61 47
		$this->initConstants();
62 47
		$this->initInterfaces();
63 47
	}
64
65
	/**
66
	 * Returns the parent class name
67
	 *
68
	 * @return string
69
	 */
70 16
	public function getParentClassName(): ?string {
71 16
		return $this->parentClassName;
72
	}
73
74
    public function getParentClass(): PhpClass {
75
	    return class_exists($this->parentClassName) ?
76
            self::fromName($this->parentClassName) : PhpClass::create($this->parentClassName);
77
    }
78
79
	/**
80
	 * Sets the parent class name
81
	 *
82
	 * @param PhpClass|string|null $name the new parent
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
83
	 * @return $this
84
	 */
85 6
	public function setParentClassName($parent) {
86 6
		if ($parent instanceof PhpClass) {
87
		    $this->addUseStatement($parent->getQualifiedName());
88
		    $parent = $parent->getName();
89
        }
90 6
        $this->parentClassName = $parent;
91
92 6
        return $this;
93
	}
94
95 5
	public function generateDocblock(): void {
96 5
		parent::generateDocblock();
97
98 5
		foreach ($this->constants as $constant) {
99 2
			$constant->generateDocblock();
100
		}
101
102 5
		foreach ($this->properties as $prop) {
103 3
			$prop->generateDocblock();
104
		}
105 5
	}
106
107
}
108