1 | <?php |
||
31 | class PhpParameter extends AbstractModel { |
||
32 | |||
33 | use NamePart; |
||
34 | use TypePart; |
||
35 | use ValuePart; |
||
36 | |||
37 | private $passedByReference = false; |
||
38 | |||
39 | /** |
||
40 | * Creates a new PHP parameter. |
||
41 | * |
||
42 | * @param string $name the parameter name |
||
43 | * @return static |
||
44 | */ |
||
45 | 16 | public static function create($name = null) { |
|
48 | |||
49 | /** |
||
50 | * Creates a PHP parameter from reflection |
||
51 | * |
||
52 | * @param \ReflectionParameter $ref |
||
53 | * @return PhpParameter |
||
54 | */ |
||
55 | 4 | public static function fromReflection(\ReflectionParameter $ref) { |
|
56 | 4 | $parameter = new static(); |
|
57 | 4 | $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); |
|
58 | |||
59 | 4 | if ($ref->isDefaultValueAvailable()) { |
|
60 | 4 | $default = $ref->getDefaultValue(); |
|
61 | 4 | if (is_string($default) || is_null($default)) { |
|
62 | 4 | $parameter->setValue($default); |
|
63 | 4 | } else { |
|
64 | $parameter->setExpression($default); |
||
65 | } |
||
66 | 4 | } |
|
67 | |||
68 | // find type and description in docblock |
||
69 | 4 | $docblock = new Docblock($ref->getDeclaringFunction()); |
|
70 | |||
71 | 4 | $params = $docblock->getTags('param'); |
|
72 | 4 | $tag = $params->find($ref->name, function (ParamTag $t, $name) { |
|
73 | 4 | return $t->getVariable() == '$' . $name; |
|
74 | 4 | }); |
|
75 | |||
76 | 4 | if ($tag !== null) { |
|
77 | 4 | $parameter->setType($tag->getType(), $tag->getDescription()); |
|
78 | 4 | } |
|
79 | |||
80 | // set type if not found in comment |
||
81 | 4 | if ($parameter->getType() === null) { |
|
82 | if ($ref->isArray()) { |
||
83 | $parameter->setType('array'); |
||
84 | } elseif ($class = $ref->getClass()) { |
||
85 | $parameter->setType($class->getName()); |
||
86 | } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { |
||
87 | $parameter->setType('callable'); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | 4 | return $parameter; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Creates a new PHP parameter |
||
96 | * |
||
97 | * @param string $name the parameter name |
||
98 | */ |
||
99 | 27 | public function __construct($name = null) { |
|
102 | |||
103 | /** |
||
104 | * Sets whether this parameter is passed by reference |
||
105 | * |
||
106 | * @param bool $bool `true` if passed by reference and `false` if not |
||
107 | * @return $this |
||
108 | */ |
||
109 | 6 | public function setPassedByReference($bool) { |
|
114 | |||
115 | /** |
||
116 | * Returns whether this parameter is passed by reference |
||
117 | * |
||
118 | * @return bool `true` if passed by reference and `false` if not |
||
119 | */ |
||
120 | 12 | public function isPassedByReference() { |
|
123 | |||
124 | /** |
||
125 | * Returns a docblock tag for this parameter |
||
126 | * |
||
127 | * @return ParamTag |
||
128 | */ |
||
129 | 7 | public function getDocblockTag() { |
|
135 | |||
136 | /** |
||
137 | * Alias for setDescription() |
||
138 | * |
||
139 | * @see #setDescription |
||
140 | * @param string $description |
||
141 | * @return $this |
||
142 | */ |
||
143 | 7 | public function setTypeDescription($description) { |
|
146 | |||
147 | /** |
||
148 | * Alias for getDescription() |
||
149 | * |
||
150 | * @see #getDescription |
||
151 | * @return string |
||
152 | */ |
||
153 | 8 | public function getTypeDescription() { |
|
156 | } |
||
157 |