1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\model; |
18
|
|
|
|
19
|
|
|
use gossi\codegen\model\parts\NamePart; |
20
|
|
|
use gossi\codegen\model\parts\TypePart; |
21
|
|
|
use gossi\codegen\model\parts\ValuePart; |
22
|
|
|
use gossi\docblock\Docblock; |
23
|
|
|
use gossi\docblock\tags\ParamTag; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Represents a PHP parameter. |
27
|
|
|
* |
28
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
29
|
|
|
* @author Thomas Gossmann |
30
|
|
|
*/ |
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) { |
46
|
19 |
|
return new static($name); |
47
|
|
|
} |
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) { |
107
|
32 |
|
$this->setName($name); |
108
|
32 |
|
} |
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) { |
117
|
12 |
|
$this->passedByReference = (boolean) $bool; |
118
|
|
|
|
119
|
12 |
|
return $this; |
120
|
|
|
} |
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() { |
128
|
12 |
|
return $this->passedByReference; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns a docblock tag for this parameter |
133
|
|
|
* |
134
|
|
|
* @return ParamTag |
135
|
|
|
*/ |
136
|
7 |
|
public function getDocblockTag() { |
137
|
7 |
|
return ParamTag::create() |
138
|
7 |
|
->setType($this->getType()) |
139
|
7 |
|
->setVariable($this->getName()) |
140
|
7 |
|
->setDescription($this->getTypeDescription()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Alias for setDescription() |
145
|
|
|
* |
146
|
|
|
* @see #setDescription |
147
|
|
|
* @param string $description |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
7 |
|
public function setTypeDescription($description) { |
151
|
7 |
|
return $this->setDescription($description); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Alias for getDescription() |
156
|
|
|
* |
157
|
|
|
* @see #getDescription |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
9 |
|
public function getTypeDescription() { |
161
|
9 |
|
return $this->getDescription(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|