Completed
Push — master ( 9086d9...713b15 )
by Thomas
07:07
created

PhpClass::fromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
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 3
		$class = new static();
40 3
		$class->setQualifiedName($ref->name)
41 3
			->setAbstract($ref->isAbstract())
42 3
			->setFinal($ref->isFinal())
43 3
			->setUseStatements(ReflectionUtils::getUseStatements($ref));
44
45 3
		if ($ref->getDocComment()) {
46 2
			$docblock = new Docblock($ref);
47 2
			$class->setDocblock($docblock);
48 2
			$class->setDescription($docblock->getShortDescription());
49 2
			$class->setLongDescription($docblock->getLongDescription());
50 2
		}
51
52
		// methods
53 3
		foreach ($ref->getMethods() as $method) {
54 3
			$class->setMethod(static::createMethod($method));
55 3
		}
56
57
		// properties
58 3
		foreach ($ref->getProperties() as $property) {
59 3
			$class->setProperty(static::createProperty($property));
60 3
		}
61
62
		// traits
63 3
		foreach ($ref->getTraits() as $trait) {
64 1
			$class->addTrait(PhpTrait::fromReflection($trait));
65 3
		}
66
67
		// constants
68
		// TODO: https://github.com/gossi/php-code-generator/issues/19
69 3
		foreach ($ref->getConstants() as $name => $value) {
70 1
			$const = new PhpConstant($name);
71
72 1
			if (is_string($value)) {
73 2
				$const->setValue($value);
74 1
			} else {
75 1
				$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...
76
			}
77 1
			$class->setConstant($const);
78 3
		}
79
80 3
		return $class;
81
	}
82
83
	/**
84
	 * Creates a PHP class from file
85
	 *
86
	 * @param string $filename
87
	 * @return PhpClass
88
	 */
89 6
	public static function fromFile($filename) {
90 6
		$visitor = new PhpClassVisitor();
91 6
		$parser = new FileParser();
92 6
		return $parser->parse($visitor, $filename);
93
	}
94
95
	/**
96
	 * Creates a new PHP class
97
	 *
98
	 * @param string $name the qualified name
99
	 */
100 42
	public function __construct($name = null) {
101 42
		parent::__construct($name);
102 42
	}
103
104
	/**
105
	 * Returns the parent class name
106
	 *
107
	 * @return string
108
	 */
109 12
	public function getParentClassName() {
110 12
		return $this->parentClassName;
111
	}
112
113
	/**
114
	 * Sets the parent class name
115
	 *
116
	 * @param string|null $name the new parent
117
	 * @return $this
118
	 */
119 1
	public function setParentClassName($name) {
120 1
		$this->parentClassName = $name;
121
122 1
		return $this;
123
	}
124
125 5
	public function generateDocblock() {
126 5
		parent::generateDocblock();
127
128 5
		foreach ($this->constants as $constant) {
129 3
			$constant->generateDocblock();
130 5
		}
131
132 5
		foreach ($this->properties as $prop) {
133 3
			$prop->generateDocblock();
134 5
		}
135 5
	}
136
137
}
138