Skip to content

Commit 7467664

Browse files
authored
Merge pull request #2 from makeey/bug-support-reference-parameters
Add support reference type
2 parents fc66b30 + 4cff5a5 commit 7467664

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Generate class diagrams for your code in plant UML format.
1313
* Don't require an autoload file
1414
* Use PlantUML format for diagrams
1515

16+
### Installation
17+
`composer global require makeey/php-puml`
18+
1619
### Usage
1720
`php bin/app generate path_to_folder output_path/file.puml `
1821

src/Parser/Tokens/FunctionToken.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class FunctionToken extends AbstractToken
66
{
7-
/** @var array */
7+
/** @var array */
88
private static $DEFAULT_VALUE_FOR_TYPES = ['null', 'true', 'false', '[', ']'];
99
/** @var array */
1010
protected $params;
@@ -41,22 +41,31 @@ public function params(): array
4141
{
4242
if ($this->params === null) {
4343
$i = 4;
44+
4445
do {
4546
$next = $this->tokens[$this->id + $i];
4647
if ($next[0] === T_STRING || $next[0] === T_ARRAY) {
4748
if (in_array($next[1], self::$DEFAULT_VALUE_FOR_TYPES, true) === false) {
48-
if ($this->tokens[$this->id + $i + 2][1] === "...") {
49+
if (isset($this->tokens[$this->id + $i + 2][1]) && $this->tokens[$this->id + $i + 2][1] === "...") {
4950
$this->params[] = [
50-
'type' => $next[1]."[]",
51+
'type' => $next[1] . "[]",
5152
'variable' => $this->tokens[$this->id + $i + 3][1] ?? "bugs"
5253
];
5354
$i += 3;
5455
} else {
55-
$this->params[] = [
56-
'type' => $next[1],
57-
'variable' => $this->tokens[$this->id + $i + 2][1]
58-
];
59-
$i += 2;
56+
if (isset($this->tokens[$this->id + $i + 2][1])) {
57+
$this->params[] = [
58+
'type' => $next[1],
59+
'variable' => $this->tokens[$this->id + $i + 2][1]
60+
];
61+
$i += 2;
62+
} else {
63+
$this->params[] = [
64+
'type' => $next[1],
65+
'variable' => $this->tokens[$this->id + $i + 2] . $this->tokens[$this->id + $i + 3][1]
66+
];
67+
$i += 3;
68+
}
6069
}
6170
}
6271
}

tests/Parser/Tokens/FunctionTokenTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ public function test(array \$variable = [])
260260
], $functionToken->params());
261261
}
262262
}
263+
263264
public function testParseFunctionWithBooleanParamsWithDefaultValueFalse(): void
264265
{
265266
$tokens = token_get_all(
@@ -325,4 +326,73 @@ public function test(array \$variable = null, bool \$booleanVar = true)
325326
], $functionToken->params());
326327
}
327328
}
329+
330+
331+
public function testShouldParseReferenceType(): void
332+
{
333+
$tokens = token_get_all(
334+
<<<EOT
335+
<?php
336+
class Foo
337+
{
338+
public function test(int &\$b)
339+
{
340+
return ++\$b;
341+
}
342+
}
343+
EOT
344+
);
345+
$functionToken = null;
346+
foreach ($tokens as $id => $value) {
347+
if ($value[0] === T_FUNCTION) {
348+
$functionToken = new FunctionToken($id, $tokens);
349+
}
350+
}
351+
if ($functionToken !== null) {
352+
$this->assertEquals("test", $functionToken->functionName());
353+
$this->assertEquals([
354+
[
355+
'type' => 'int',
356+
'variable' => '&$b'
357+
]
358+
], $functionToken->params());
359+
}
360+
361+
362+
$tokens = token_get_all(
363+
<<<EOT
364+
<?php
365+
class Foo
366+
{
367+
public function test(string \$firstParam, int &\$b, string \$thirdParam)
368+
{
369+
return ++\$b;
370+
}
371+
}
372+
EOT
373+
);
374+
$functionToken = null;
375+
foreach ($tokens as $id => $value) {
376+
if ($value[0] === T_FUNCTION) {
377+
$functionToken = new FunctionToken($id, $tokens);
378+
}
379+
}
380+
if ($functionToken !== null) {
381+
$this->assertEquals("test", $functionToken->functionName());
382+
$this->assertEquals([
383+
[
384+
'type' => 'string',
385+
'variable' => '$firstParam'
386+
],
387+
[
388+
'type' => 'int',
389+
'variable' => '&$b'
390+
],
391+
[
392+
'type' => 'string',
393+
'variable' => '$thirdParam'
394+
],
395+
], $functionToken->params());
396+
}
397+
}
328398
}

0 commit comments

Comments
 (0)