Skip to content

Commit 71daa16

Browse files
committed
Merge pull request #1 from marcj/master
Hex Hex
2 parents d68d783 + effa3c8 commit 71daa16

6 files changed

Lines changed: 130 additions & 31 deletions

File tree

src/model/AbstractModel.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ public function getDescription() {
6363

6464
/**
6565
*
66-
* @param string $description
66+
* @param string|array $description
67+
* @return $this
6768
*/
6869
public function setDescription($description) {
70+
if (is_array($description)) {
71+
$description = implode("\n", $description);
72+
}
6973
$this->description = $description;
7074
return $this;
7175
}

src/model/AbstractPhpStruct.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,17 @@ public function setUseStatements(array $useStatements) {
9292

9393
/**
9494
*
95-
* @param string $qualifiedName
96-
* @param string $alias
95+
* @param string $qualifiedName
96+
* @param null|string $alias
97+
* @return $this
9798
*/
9899
public function addUseStatement($qualifiedName, $alias = null) {
99100
if (null === $alias) {
100-
$alias = substr($qualifiedName, strrpos($qualifiedName, '\\') + 1);
101+
if (false === strpos($qualifiedName, '\\')) {
102+
$alias = $qualifiedName;
103+
} else {
104+
$alias = substr($qualifiedName, strrpos($qualifiedName, '\\') + 1);
105+
}
101106
}
102107

103108
$this->useStatements[$alias] = $qualifiedName;
@@ -112,7 +117,40 @@ public function addUseStatement($qualifiedName, $alias = null) {
112117
* @return boolean
113118
*/
114119
public function hasUseStatement($qualifiedName) {
115-
return in_array($qualifiedName, $this->useStatements);
120+
$flipped = array_flip($this->useStatements);
121+
return isset($flipped[$qualifiedName]);
122+
}
123+
124+
public function declareUses()
125+
{
126+
foreach (func_get_args() as $name) {
127+
$this->declareUse($name);
128+
}
129+
}
130+
131+
/**
132+
* @param string $fullClassName
133+
* @param null|string $alias
134+
*
135+
* @return string
136+
*/
137+
public function declareUse($fullClassName, $alias = null)
138+
{
139+
$fullClassName = trim($fullClassName, '\\');
140+
if (!$this->hasUseStatement($fullClassName)) {
141+
$this->addUseStatement($fullClassName, $alias);
142+
}
143+
144+
return $this->getUseAlias($fullClassName);
145+
}
146+
147+
/**
148+
* @param string $qualifiedName
149+
* @return string
150+
*/
151+
public function getUseAlias($qualifiedName) {
152+
$flipped = array_flip($this->useStatements);
153+
return $flipped[$qualifiedName];
116154
}
117155

118156
public function removeUseStatement($qualifiedName) {

src/model/PhpParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getDocblockTag() {
9393
* @see #setDescription
9494
* @param string $description
9595
*/
96-
protected function setTypeDescription($description) {
96+
public function setTypeDescription($description) {
9797
$this->setDescription($description);
9898
}
9999

src/model/parts/ParametersTrait.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,39 @@ trait ParametersTrait {
1313

1414
public function setParameters(array $parameters) {
1515
$this->parameters = array_values($parameters);
16-
16+
1717
return $this;
1818
}
1919

2020
public function addParameter(PhpParameter $parameter) {
2121
$this->parameters[count($this->parameters)] = $parameter;
22-
22+
2323
return $this;
2424
}
2525

2626
/**
27+
* @param string $name
28+
* @param null|string $type
29+
* @param mixed $defaultValue omit the argument to define now default value
2730
*
28-
* @param string|integer $nameOrIndex
31+
* @return $this
32+
*/
33+
public function addSimpleParameter($name, $type = null, $defaultValue = null)
34+
{
35+
$parameter = new PhpParameter($name);
36+
$parameter->setType($type);
37+
38+
if (2 < func_num_args()) {
39+
$parameter->setDefaultValue($defaultValue);
40+
}
41+
42+
$this->addParameter($parameter);
43+
return $this;
44+
}
45+
46+
/**
47+
*
48+
* @param string|integer $nameOrIndex
2949
*
3050
* @return PhpParameter
3151
*/
@@ -34,18 +54,18 @@ public function getParameter($nameOrIndex) {
3454
if (!isset($this->parameters[$nameOrIndex])) {
3555
throw new \InvalidArgumentException(sprintf('There is no parameter at position %d.', $nameOrIndex));
3656
}
37-
57+
3858
return $this->parameters[$nameOrIndex];
3959
}
40-
60+
4161
foreach ($this->parameters as $param) {
4262
assert($param instanceof PhpParameter);
43-
63+
4464
if ($param->getName() === $nameOrIndex) {
4565
return $param;
4666
}
4767
}
48-
68+
4969
throw new \InvalidArgumentException(sprintf('There is no parameter named "%s".', $nameOrIndex));
5070
}
5171

@@ -54,21 +74,21 @@ public function replaceParameter($position, PhpParameter $parameter) {
5474
throw new \InvalidArgumentException(sprintf('The position must be in the range [0, %d].', count($this->parameters)));
5575
}
5676
$this->parameters[$position] = $parameter;
57-
77+
5878
return $this;
5979
}
6080

6181
/**
6282
*
63-
* @param integer $position
83+
* @param integer $position
6484
*/
6585
public function removeParameter($position) {
6686
if (!isset($this->parameters[$position])) {
6787
throw new \InvalidArgumentException(sprintf('There is no parameter at position "%d" does not exist.', $position));
6888
}
6989
unset($this->parameters[$position]);
7090
$this->parameters = array_values($this->parameters);
71-
91+
7292
return $this;
7393
}
7494

src/model/parts/TypeTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ trait TypeTrait {
99

1010
/**
1111
*
12-
* @param string $type
12+
* @param string $type
13+
* @param null|string $description
1314
*/
14-
public function setType($type, $description = '') {
15+
public function setType($type, $description = null) {
1516
$this->type = $type;
16-
$this->setTypeDescription($description);
17+
if (null !== $description) {
18+
$this->setTypeDescription($description);
19+
}
1720

1821
return $this;
1922
}
2023

21-
protected function setTypeDescription($description) {
24+
public function setTypeDescription($description) {
2225
$this->typeDescription = $description;
26+
return $this;
2327
}
2428

2529
public function getType() {

src/visitor/DefaultVisitor.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,20 @@ private function visitUseStatements(AbstractPhpStruct $struct) {
8484
if ($useStatements = $struct->getUseStatements()) {
8585
$this->ensureBlankLine();
8686
foreach ($useStatements as $alias => $namespace) {
87+
if (false === strpos($namespace, '\\')) {
88+
$commonName = $namespace;
89+
} else {
90+
$commonName = substr($namespace, strrpos($namespace, '\\') + 1);
91+
}
92+
93+
if (false === strpos($namespace, '\\') && !$struct->getNamespace()) {
94+
//avoid fatal 'The use statement with non-compound name '$commonName' has no effect'
95+
continue;
96+
}
97+
8798
$this->writer->write('use ' . $namespace);
88-
89-
if (substr($namespace, strrpos($namespace, '\\') + 1) !== $alias) {
99+
100+
if ($commonName !== $alias) {
90101
$this->writer->write(' as ' . $alias);
91102
}
92103

@@ -187,7 +198,7 @@ public function startVisitingStructConstants() {
187198
}
188199

189200
public function visitStructConstant(PhpConstant $constant) {
190-
$this->writer->writeln('const ' . $constant->getName() . ' = ' . var_export($constant->getValue(), true) . ';');
201+
$this->writer->writeln('const ' . $constant->getName() . ' = ' . $this->getPhpExport($constant->getValue()) . ';');
191202
}
192203

193204
public function endVisitingStructConstants() {
@@ -203,12 +214,28 @@ public function visitProperty(PhpProperty $property) {
203214
$this->writer->write($property->getVisibility() . ' ' . ($property->isStatic() ? 'static ' : '') . '$' . $property->getName());
204215

205216
if ($property->hasDefaultValue()) {
206-
$this->writer->write(' = ' . var_export($property->getDefaultValue(), true));
217+
$this->writer->write(' = ' . $this->getPhpExport($property->getDefaultValue()));
207218
}
208219

209220
$this->writer->writeln(';');
210221
}
211222

223+
protected function getPhpExport($value) {
224+
if (is_bool($value)) {
225+
return $value ? 'true' : 'false';
226+
}
227+
228+
if (null === $value) {
229+
return 'null';
230+
}
231+
232+
if (is_array($value)) {
233+
return 'array()';
234+
}
235+
236+
return var_export($value, true);
237+
}
238+
212239
public function endVisitingProperties() {
213240
$this->writer->writeln();
214241
}
@@ -247,7 +274,7 @@ public function visitMethod(PhpMethod $method) {
247274
return;
248275
}
249276

250-
$this->writer->writeln(') {')->indent()->writeln($method->getBody())->outdent()->rtrim()->write("}\n\n");
277+
$this->writer->writeln(') {')->indent()->writeln(trim($method->getBody()))->outdent()->rtrim()->write("}\n\n");
251278
}
252279

253280
public function endVisitingMethods() {
@@ -280,7 +307,7 @@ public function visitFunction(PhpFunction $function) {
280307

281308
$this->writer->write("function {$function->getName()}(");
282309
$this->writeParameters($function->getParameters());
283-
$this->writer->write(") {\n")->indent()->writeln($function->getBody())->outdent()->rtrim()->write('}');
310+
$this->writer->write(") {\n")->indent()->writeln(trim($function->getBody()))->outdent()->rtrim()->write('}');
284311
}
285312

286313
public function getContent() {
@@ -308,11 +335,17 @@ private function writeParameters(array $parameters) {
308335
if ($parameter->hasDefaultValue()) {
309336
$this->writer->write(' = ');
310337
$defaultValue = $parameter->getDefaultValue();
311-
312-
if (is_array($defaultValue) && empty($defaultValue)) {
313-
$this->writer->write('array()');
314-
} else {
315-
$this->writer->write(var_export($defaultValue, true));
338+
339+
switch (true) {
340+
case is_array($defaultValue) && empty($defaultValue):
341+
$this->writer->write('array()');
342+
break;
343+
case ($defaultValue instanceof PhpConstant):
344+
$this->writer->write($defaultValue->getName());
345+
break;
346+
default:
347+
$this->writer->write($this->getPhpExport($defaultValue));
348+
316349
}
317350
}
318351
}

0 commit comments

Comments
 (0)