Completed
Pull Request — master (#30)
by
unknown
05:11
created

PhpClass::generateDocblock()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 0
crap 3
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->getFileName() == $ref->getFileName()) {
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
73
			// remove Property from traits to avoid error
74
			foreach ($trait->getProperties() as $property) {
75
				/**@var \ReflectionProperty $property */
76
				$name = $property->getName();
77
				if ($class->hasProperty($name)) {
78
					$class->removeProperty($name);
79
				}
80
			}
81
			
82
			$class->addTrait(PhpTrait::fromReflection($trait));
83
		}
84
85
		// constants
86
		// TODO: https://github.com/gossi/php-code-generator/issues/19
87
		foreach ($ref->getConstants() as $name => $value) {
88
			$const = new PhpConstant($name);
89
90
			if (is_string($value)) {
91
				$const->setValue($value);
92
			} else {
93
				$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...
94
			}
95
			$class->setConstant($const);
96
		}
97
98
		return $class;
99
	}
100
101
	/**
102
	 * Creates a PHP class from file
103
	 *
104
	 * @param string $filename
105
	 * @return PhpClass
106
	 */
107 6
	public static function fromFile($filename) {
108 6
		$visitor = new PhpClassVisitor();
109 6
		$parser = new FileParser();
110 6
		return $parser->parse($visitor, $filename);
111
	}
112
113
	/**
114
	 * Creates a new PHP class
115
	 *
116
	 * @param string $name the qualified name
117
	 */
118 42
	public function __construct($name = null) {
119 42
		parent::__construct($name);
120 42
	}
121
122
	/**
123
	 * Returns the parent class name
124
	 *
125
	 * @return string
126
	 */
127 12
	public function getParentClassName() {
128 12
		return $this->parentClassName;
129
	}
130
131
	/**
132
	 * Sets the parent class name
133
	 *
134
	 * @param string|null $name the new parent
135
	 * @return $this
136
	 */
137 1
	public function setParentClassName($name) {
138 1
		$this->parentClassName = $name;
139
140 1
		return $this;
141
	}
142
143 5
	public function generateDocblock() {
144 5
		parent::generateDocblock();
145
146 5
		foreach ($this->constants as $constant) {
147 3
			$constant->generateDocblock();
148 5
		}
149
150 5
		foreach ($this->properties as $prop) {
151 3
			$prop->generateDocblock();
152 5
		}
153 5
	}
154
155
}
156