Completed
Pull Request — master (#30)
by
unknown
03:52
created

PhpClass::setParentClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace gossi\codegen\model;
3
4
use gossi\codegen\model\parts\AbstractPart;
5
use gossi\codegen\model\parts\ConstantsPart;
6
use gossi\codegen\model\parts\FinalPart;
7
use gossi\codegen\model\parts\InterfacesPart;
8
use gossi\codegen\model\parts\PropertiesPart;
9
use gossi\codegen\model\parts\TraitsPart;
10
use gossi\codegen\parser\FileParser;
11
use gossi\codegen\parser\visitor\PhpClassVisitor;
12
use gossi\codegen\utils\ReflectionUtils;
13
use gossi\docblock\Docblock;
14
15
/**
16
 * Represents a PHP class.
17
 *
18
 * @author Thomas Gossmann
19
 */
20
class PhpClass extends AbstractPhpStruct implements GenerateableInterface, TraitsInterface, ConstantsInterface {
21
22
	use AbstractPart;
23
	use ConstantsPart;
24
	use FinalPart;
25
	use InterfacesPart;
26
	use PropertiesPart;
27
	use TraitsPart;
28
29
	/** @var string */
30
	private $parentClassName;
31
32
	/**
33
	 * Creates a PHP class from reflection
34
	 *
35
	 * @param \ReflectionClass $ref
36
	 * @return PhpClass
37
	 */
38 3
	public static function fromReflection(\ReflectionClass $ref) {
39
40
		/**@var PhpClass $class */
41 3
		$class = new static();
42
43 3
		$class->setQualifiedName($ref->name)
44 3
			->setAbstract($ref->isAbstract())
45 3
			->setFinal($ref->isFinal())
46 3
			->setParentClassName($ref->getParentClass()->name)
47
			->setUseStatements(ReflectionUtils::getUseStatements($ref));
48
49
		if ($ref->getDocComment()) {
50
			$docblock = new Docblock($ref);
51
			$class->setDocblock($docblock);
52
			$class->setDescription($docblock->getShortDescription());
53
			$class->setLongDescription($docblock->getLongDescription());
54
		}
55
56
		// methods
57
		foreach ($ref->getMethods() as $method) {
58
			if ($method->class == $ref->name) {
59
				$class->setMethod(static::createMethod($method));
60
			}
61
		}
62
63
		// properties
64
		foreach ($ref->getProperties() as $property) {
65
			if ($property->class == $ref->name) {
66
				$class->setProperty(static::createProperty($property));
67
			}
68
		}
69
70
		// traits
71
		foreach ($ref->getTraits() as $trait) {
72
			if ($trait->class == $ref->name) {
0 ignored issues
show
Bug introduced by
The property class does not seem to exist in ReflectionClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
				$class->addTrait(PhpTrait::fromReflection($trait));
74
			}
75 1
		}
76
77
		// constants
78
		// TODO: https://github.com/gossi/php-code-generator/issues/19
79
		foreach ($ref->getConstants() as $name => $value) {
80
			$const = new PhpConstant($name);
81
82
			if (is_string($value)) {
83
				$const->setValue($value);
84
			} else {
85
				$const->setExpression($value);
0 ignored issues
show
Documentation introduced by
$value is of type integer|double|boolean, but the function expects a string.

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...
86
			}
87
			$class->setConstant($const);
88
		}
89
90
		return $class;
91
	}
92
93
	/**
94
	 * Creates a PHP class from file
95
	 *
96
	 * @param string $filename
97
	 * @return PhpClass
98
	 */
99 6
	public static function fromFile($filename) {
100 6
		$visitor = new PhpClassVisitor();
101 6
		$parser = new FileParser();
102 6
		return $parser->parse($visitor, $filename);
103
	}
104
105
	/**
106
	 * Creates a new PHP class
107
	 *
108
	 * @param string $name the qualified name
109
	 */
110 42
	public function __construct($name = null) {
111 42
		parent::__construct($name);
112 42
	}
113
114
	/**
115
	 * Returns the parent class name
116
	 *
117
	 * @return string
118
	 */
119 12
	public function getParentClassName() {
120 12
		return $this->parentClassName;
121
	}
122
123
	/**
124
	 * Sets the parent class name
125
	 *
126
	 * @param string|null $name the new parent
127
	 * @return $this
128
	 */
129 1
	public function setParentClassName($name) {
130 1
		$this->parentClassName = $name;
131
132 1
		return $this;
133
	}
134
135 5
	public function generateDocblock() {
136 5
		parent::generateDocblock();
137
138 5
		foreach ($this->constants as $constant) {
139 3
			$constant->generateDocblock();
140 5
		}
141
142 5
		foreach ($this->properties as $prop) {
143 3
			$prop->generateDocblock();
144 5
		}
145 5
	}
146
147
}
148