|
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 { |
|
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) { |
|
46
|
16 |
|
return new static($name); |
|
47
|
|
|
} |
|
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)) { |
|
62
|
3 |
|
$parameter->setValue($default); |
|
63
|
3 |
|
} else { |
|
64
|
1 |
|
$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) { |
|
100
|
27 |
|
$this->setName($name); |
|
101
|
27 |
|
} |
|
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) { |
|
110
|
6 |
|
$this->passedByReference = (boolean) $bool; |
|
111
|
|
|
|
|
112
|
6 |
|
return $this; |
|
113
|
|
|
} |
|
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() { |
|
121
|
12 |
|
return $this->passedByReference; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns a docblock tag for this parameter |
|
126
|
|
|
* |
|
127
|
|
|
* @return ParamTag |
|
128
|
|
|
*/ |
|
129
|
7 |
|
public function getDocblockTag() { |
|
130
|
7 |
|
return ParamTag::create() |
|
131
|
7 |
|
->setType($this->getType() ?: 'mixed') |
|
132
|
7 |
|
->setVariable($this->getName()) |
|
133
|
7 |
|
->setDescription($this->getTypeDescription()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Alias for setDescription() |
|
138
|
|
|
* |
|
139
|
|
|
* @see #setDescription |
|
140
|
|
|
* @param string $description |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
7 |
|
public function setTypeDescription($description) { |
|
144
|
7 |
|
return $this->setDescription($description); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Alias for getDescription() |
|
149
|
|
|
* |
|
150
|
|
|
* @see #getDescription |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
8 |
|
public function getTypeDescription() { |
|
154
|
8 |
|
return $this->getDescription(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|