Skip to content

Add support reference type #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Generate class diagrams for your code in plant UML format.
* Don't require an autoload file
* Use PlantUML format for diagrams

### Installation
`composer global require makeey/php-puml`

### Usage
`php bin/app generate path_to_folder output_path/file.puml `

25 changes: 17 additions & 8 deletions src/Parser/Tokens/FunctionToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class FunctionToken extends AbstractToken
{
/** @var array */
/** @var array */
private static $DEFAULT_VALUE_FOR_TYPES = ['null', 'true', 'false', '[', ']'];
/** @var array */
protected $params;
Expand Down Expand Up @@ -41,22 +41,31 @@ public function params(): array
{
if ($this->params === null) {
$i = 4;

do {
$next = $this->tokens[$this->id + $i];
if ($next[0] === T_STRING || $next[0] === T_ARRAY) {
if (in_array($next[1], self::$DEFAULT_VALUE_FOR_TYPES, true) === false) {
if ($this->tokens[$this->id + $i + 2][1] === "...") {
if (isset($this->tokens[$this->id + $i + 2][1]) && $this->tokens[$this->id + $i + 2][1] === "...") {
$this->params[] = [
'type' => $next[1]."[]",
'type' => $next[1] . "[]",
'variable' => $this->tokens[$this->id + $i + 3][1] ?? "bugs"
];
$i += 3;
} else {
$this->params[] = [
'type' => $next[1],
'variable' => $this->tokens[$this->id + $i + 2][1]
];
$i += 2;
if (isset($this->tokens[$this->id + $i + 2][1])) {
$this->params[] = [
'type' => $next[1],
'variable' => $this->tokens[$this->id + $i + 2][1]
];
$i += 2;
} else {
$this->params[] = [
'type' => $next[1],
'variable' => $this->tokens[$this->id + $i + 2] . $this->tokens[$this->id + $i + 3][1]
];
$i += 3;
}
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Parser/Tokens/FunctionTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public function test(array \$variable = [])
], $functionToken->params());
}
}

public function testParseFunctionWithBooleanParamsWithDefaultValueFalse(): void
{
$tokens = token_get_all(
Expand Down Expand Up @@ -325,4 +326,73 @@ public function test(array \$variable = null, bool \$booleanVar = true)
], $functionToken->params());
}
}


public function testShouldParseReferenceType(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(int &\$b)
{
return ++\$b;
}
}
EOT
);
$functionToken = null;
foreach ($tokens as $id => $value) {
if ($value[0] === T_FUNCTION) {
$functionToken = new FunctionToken($id, $tokens);
}
}
if ($functionToken !== null) {
$this->assertEquals("test", $functionToken->functionName());
$this->assertEquals([
[
'type' => 'int',
'variable' => '&$b'
]
], $functionToken->params());
}


$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(string \$firstParam, int &\$b, string \$thirdParam)
{
return ++\$b;
}
}
EOT
);
$functionToken = null;
foreach ($tokens as $id => $value) {
if ($value[0] === T_FUNCTION) {
$functionToken = new FunctionToken($id, $tokens);
}
}
if ($functionToken !== null) {
$this->assertEquals("test", $functionToken->functionName());
$this->assertEquals([
[
'type' => 'string',
'variable' => '$firstParam'
],
[
'type' => 'int',
'variable' => '&$b'
],
[
'type' => 'string',
'variable' => '$thirdParam'
],
], $functionToken->params());
}
}
}