Skip to content

Commit

Permalink
[Bug] fix parsing boolean params with default value, do not write nam…
Browse files Browse the repository at this point in the history
…espace for the php classes
  • Loading branch information
makeey committed Feb 29, 2020
1 parent d6e08d0 commit ee2d1b8
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 14 deletions.
26 changes: 18 additions & 8 deletions src/Parser/Tokens/FunctionToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class FunctionToken extends AbstractToken
{
/** @var array */
private static $DEFAULT_VALUE_FOR_TYPES = ['null', 'true', 'false', '[', ']'];
/** @var array */
protected $params;
/** @var string */
Expand Down Expand Up @@ -41,13 +43,21 @@ public function params(): array
$i = 4;
do {
$next = $this->tokens[$this->id + $i];
if ($next[0] === T_STRING) {
if ($next[1] !== "null") {
$this->params[] = [
'type' => $next[1],
'variable' => $this->tokens[$this->id + $i + 2][1] ?? "bugs"
];
$i += 2;
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] === "...") {
$this->params[] = [
'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 ($next[0] === T_VARIABLE) {
Expand All @@ -57,7 +67,7 @@ public function params(): array
];
}
$i++;
} while ($next != ")");
} while ($next != ")" && $this->id + $i < count($this->tokens));
if ($this->params === null) {
$this->params = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/TransformMiddleware/NamespaceTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function transform(PhpFile $phpFile): PhpFile

private function modifyEntityName(?string $name, PhpFile $phpFile): ?string
{
if ($name !== null) {
if ($name !== null && $name[0] !== "\\") {
foreach ($phpFile->usedClasses() as $usedClass) {
if ($name === $usedClass['name']) {
return $usedClass['fullName'];
Expand Down
2 changes: 1 addition & 1 deletion src/UML/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function format(UMLDiagram $diagram): string
{
$output = "@startuml " . PHP_EOL;
$output .= $this->formatUmlDiagram($diagram);
$output .= $this->relationFormatter->buildRelations($diagram);
$output .= $this->relationFormatter->format($diagram);
$output .= PHP_EOL . "@enduml";
return $output;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UML/Formatter/RelationFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class RelationFormatter
{
public function buildRelations(UMLDiagram $diagram): string
public function format(UMLDiagram $diagram): string
{
$classes = [];
array_map(function (UMLNamespace $package) use (&$classes): void {
Expand Down
157 changes: 157 additions & 0 deletions tests/Parser/Tokens/FunctionTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,161 @@ public function test(\$foo, int \$baz)
}
}
}

public function testCanParseClassWithVariadic(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(Foo ...\$foes)
{
}
}
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' => 'Foo[]',
'variable' => '$foes'
]
], $functionToken->params());
}
}

public function testParseFunctionWithBooleanParamsWithDefaultValueTrue(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(bool \$variable = true ) : bool
{
return \$variable;
}
}
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' => 'bool',
'variable' => '$variable'
]
], $functionToken->params());
}
}

public function testCanParseFunctionWithArrayParamsWhichEmptyByDefault(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(array \$variable = [])
{
return \$variable;
}
}
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' => 'array',
'variable' => '$variable'
]
], $functionToken->params());
}
}
public function testParseFunctionWithBooleanParamsWithDefaultValueFalse(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(bool \$variable = false) : bool
{
return \$variable;
}
}
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' => 'bool',
'variable' => '$variable'
]
], $functionToken->params());
}
}

public function testCanParseTwoParametersWithDefaultValue(): void
{
$tokens = token_get_all(
<<<EOT
<?php
class Foo
{
public function test(array \$variable = null, bool \$booleanVar = true)
{
return \$variable;
}
}
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' => 'array',
'variable' => '$variable'
],
[
'type' => 'bool',
'variable' => '$booleanVar'
]
], $functionToken->params());
}
}
}
2 changes: 1 addition & 1 deletion tests/UML/Formatter/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Too

$relationFormatterMock = $this->createMock(RelationFormatter::class);
$relationFormatterMock->expects($this->once())
->method('buildRelations')
->method('format')
->with($diagram)
->willReturn("\n");

Expand Down
4 changes: 2 additions & 2 deletions tests/UML/Formatter/RelationFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testBuildEmptyRelations(): void

$relationFormatter = new RelationFormatter();

$this->assertEquals($expectedString, $relationFormatter->buildRelations($diagram));
$this->assertEquals($expectedString, $relationFormatter->format($diagram));
}

public function testBuildRelations(): void
Expand All @@ -48,6 +48,6 @@ public function testBuildRelations(): void

$relationFormatter = new RelationFormatter();

$this->assertEquals($expectedString, $relationFormatter->buildRelations($diagram));
$this->assertEquals($expectedString, $relationFormatter->format($diagram));
}
}

0 comments on commit ee2d1b8

Please sign in to comment.