1 | <?php |
||
31 | class PhpParameter extends AbstractModel implements ValueInterface { |
||
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 | 19 | public static function create($name = null) { |
|
48 | |||
49 | /** |
||
50 | * Creates a PHP parameter from reflection |
||
51 | * |
||
52 | * @deprecated will be removed in version 0.5 |
||
53 | * @param \ReflectionParameter $ref |
||
54 | * @return PhpParameter |
||
55 | */ |
||
56 | public static function fromReflection(\ReflectionParameter $ref) { |
||
57 | $parameter = new static(); |
||
58 | $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); |
||
59 | |||
60 | if ($ref->isDefaultValueAvailable()) { |
||
61 | $value = $ref->getDefaultValue(); |
||
62 | |||
63 | if (is_string($value) |
||
64 | || is_int($value) |
||
65 | || is_float($value) |
||
66 | || is_bool($value) |
||
67 | || is_null($value) |
||
68 | || ($value instanceof PhpConstant)) { |
||
69 | $parameter->setValue($value); |
||
70 | } else { |
||
71 | $parameter->setExpression($value); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // find type and description in docblock |
||
76 | $docblock = new Docblock($ref->getDeclaringFunction()); |
||
77 | |||
78 | $params = $docblock->getTags('param'); |
||
79 | $tag = $params->find($ref->name, function (ParamTag $t, $name) { |
||
80 | return $t->getVariable() == '$' . $name; |
||
81 | }); |
||
82 | |||
83 | if ($tag !== null) { |
||
84 | $parameter->setType($tag->getType(), $tag->getDescription()); |
||
85 | } |
||
86 | |||
87 | // set type if not found in comment |
||
88 | if ($parameter->getType() === null) { |
||
89 | if ($ref->isArray()) { |
||
90 | $parameter->setType('array'); |
||
91 | } elseif ($class = $ref->getClass()) { |
||
92 | $parameter->setType($class->getName()); |
||
93 | } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { |
||
94 | $parameter->setType('callable'); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $parameter; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Creates a new PHP parameter |
||
103 | * |
||
104 | * @param string $name the parameter name |
||
105 | */ |
||
106 | 32 | public function __construct($name = null) { |
|
109 | |||
110 | /** |
||
111 | * Sets whether this parameter is passed by reference |
||
112 | * |
||
113 | * @param bool $bool `true` if passed by reference and `false` if not |
||
114 | * @return $this |
||
115 | */ |
||
116 | 12 | public function setPassedByReference($bool) { |
|
121 | |||
122 | /** |
||
123 | * Returns whether this parameter is passed by reference |
||
124 | * |
||
125 | * @return bool `true` if passed by reference and `false` if not |
||
126 | */ |
||
127 | 12 | public function isPassedByReference() { |
|
130 | |||
131 | /** |
||
132 | * Returns a docblock tag for this parameter |
||
133 | * |
||
134 | * @return ParamTag |
||
135 | */ |
||
136 | 7 | public function getDocblockTag() { |
|
142 | |||
143 | /** |
||
144 | * Alias for setDescription() |
||
145 | * |
||
146 | * @see #setDescription |
||
147 | * @param string $description |
||
148 | * @return $this |
||
149 | */ |
||
150 | 7 | public function setTypeDescription($description) { |
|
153 | |||
154 | /** |
||
155 | * Alias for getDescription() |
||
156 | * |
||
157 | * @see #getDescription |
||
158 | * @return string |
||
159 | */ |
||
160 | 9 | public function getTypeDescription() { |
|
163 | } |
||
164 |