Skip to content

Commit

Permalink
Add support for square brackets and default parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Oct 12, 2018
1 parent c63030a commit c72683a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
22 changes: 18 additions & 4 deletions source/macros.yay
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ $(macro :recursion) {
$(chain(
buffer("class"),
ns() as className,
buffer("<"),
either(
buffer("<"),
buffer("[")
),
ls(
either(
token(T_ARRAY),
ns()
) as type,
buffer(",")
) as types,
buffer(">"),
either(
buffer(">"),
buffer("]")
),
buffer("{"),
optional(
repeat(
Expand Down Expand Up @@ -178,6 +184,8 @@ $(macro :recursion) {
$(functionArgumentType)
})
$(functionArgumentName)
$(functionArgumentEquals)
$(functionArgumentValue)
}) })
) $(functionReturn ... {
$(functionReturnTypeRemove ! {
Expand Down Expand Up @@ -231,15 +239,21 @@ $(macro :recursion) {
$(chain(
buffer("new"),
ns() as className,
buffer("<"),
either(
buffer("<"),
buffer("[")
),
ls(
either(
token(T_ARRAY),
ns()
) as type,
token(",")
) as types,
buffer(">"),
either(
buffer(">"),
buffer("]")
),
optional(
chain(
buffer("("),
Expand Down
12 changes: 12 additions & 0 deletions tests/GenericsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ public function test_can_handle_object_types()
$greeter = process(__DIR__ . "/fixtures/can-handle-object-types.pre");
$this->assertEquals("hello chris.", $greeter->greet(new Name("chris")));
}

public function test_can_handle_square_brackets()
{
$greeter = process(__DIR__ . "/fixtures/can-handle-square-brackets.pre");
$this->assertEquals("hello chris.", $greeter->greet("chris"));
}

public function test_can_handle_default_parameter_values()
{
$greeter = process(__DIR__ . "/fixtures/can-handle-default-parameter-values.pre");
$this->assertEquals("hello chris.", $greeter->greet());
}
}
11 changes: 11 additions & 0 deletions tests/fixtures/can-handle-default-parameter-values.pre
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

if (!class_exists('DefaultGreeter')) {
class DefaultGreeter<Z> {
public function greet(Z $name = "chris") {
return "hello {$name}.";
}
}
}

return new DefaultGreeter<string>;
11 changes: 11 additions & 0 deletions tests/fixtures/can-handle-square-brackets.pre
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

if (!class_exists('SquareGreeter')) {
class SquareGreeter[Z] {
public function greet(Z $name) {
return "hello {$name}.";
}
}
}

return new Greeter[string];

0 comments on commit c72683a

Please sign in to comment.