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
|
|
|
$class->addTrait(PhpTrait::fromReflection($trait)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// constants |
76
|
|
|
// TODO: https://github.com/gossi/php-code-generator/issues/19 |
77
|
|
|
foreach ($ref->getConstants() as $name => $value) { |
78
|
|
|
$const = new PhpConstant($name); |
79
|
|
|
|
80
|
|
|
if (is_string($value)) { |
81
|
|
|
$const->setValue($value); |
82
|
|
|
} else { |
83
|
|
|
$const->setExpression($value); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
$class->setConstant($const); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $class; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates a PHP class from file |
93
|
|
|
* |
94
|
|
|
* @param string $filename |
95
|
|
|
* @return PhpClass |
96
|
|
|
*/ |
97
|
6 |
|
public static function fromFile($filename) { |
98
|
6 |
|
$visitor = new PhpClassVisitor(); |
99
|
6 |
|
$parser = new FileParser(); |
100
|
6 |
|
return $parser->parse($visitor, $filename); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Creates a new PHP class |
105
|
|
|
* |
106
|
|
|
* @param string $name the qualified name |
107
|
|
|
*/ |
108
|
42 |
|
public function __construct($name = null) { |
109
|
42 |
|
parent::__construct($name); |
110
|
42 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the parent class name |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
12 |
|
public function getParentClassName() { |
118
|
12 |
|
return $this->parentClassName; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the parent class name |
123
|
|
|
* |
124
|
|
|
* @param string|null $name the new parent |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
1 |
|
public function setParentClassName($name) { |
128
|
1 |
|
$this->parentClassName = $name; |
129
|
|
|
|
130
|
1 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
5 |
|
public function generateDocblock() { |
134
|
5 |
|
parent::generateDocblock(); |
135
|
|
|
|
136
|
5 |
|
foreach ($this->constants as $constant) { |
137
|
3 |
|
$constant->generateDocblock(); |
138
|
|
|
} |
139
|
|
|
|
140
|
5 |
|
foreach ($this->properties as $prop) { |
141
|
3 |
|
$prop->generateDocblock(); |
142
|
|
|
} |
143
|
5 |
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
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: