Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
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
1 change: 1 addition & 0 deletions lib/Adapter/WorseReflection/WorseBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private function resolveTypeNameFromNameImports(Type $type, NameImports $imports
$typeName = $alias;
}
}

return $typeName;
}
}
2 changes: 1 addition & 1 deletion templates/Method.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/
{% endif %}
{% if prototype.isAbstract %}abstract {%endif %}{{ prototype.visibility }} {% if prototype.isStatic %}static {%endif %}function {{ prototype.name}}({% spaceless %}
{% for parameter in prototype.parameters %}{{ generator.render(parameter, variant)|raw }}{% if not loop.last %}, {% endif %}{% endfor %}{% endspaceless %}){% if prototype.returnType.notNone %}: {{ prototype.returnType }}{% endif %}
{% for parameter in prototype.parameters %}{{ generator.render(parameter, variant)|raw }}{% if not loop.last %}, {% endif %}{% endfor %}{% endspaceless %}){% if prototype.returnType.notNone %}: {{ prototype.returnType.nullable ? '?' : '' }}{{ prototype.returnType }}{% endif %}
2 changes: 1 addition & 1 deletion templates/Parameter.php.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% if prototype.type.notNone %}{{ prototype.type }} {% endif %}
{% if prototype.type.notNone %}{{ prototype.type.nullable ? '?' : '' }}{{ prototype.type }} {% endif %}
{% if prototype.byReference %}&{% endif %}{% if true %}${{ prototype.name }}{% endif %}
{% if prototype.defaultValue.notNone %} = {{ prototype.defaultValue.export|raw }}{% endif %}
2 changes: 1 addition & 1 deletion templates/Property.php.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if prototype.type.notNone %}
/**
* @var {{ prototype.type }}
* @var {{ prototype.type }}{{ prototype.type.nullable ? '|null' }}
*/
{% endif %}
{{ prototype.visibility }} ${{ prototype.name}}{% if prototype.defaultValue.notNone %} = {{ prototype.defaultValue.export }}{% endif %};
19 changes: 19 additions & 0 deletions tests/Adapter/GeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ public function hello()
])),
<<<'EOT'
private function hello($one, string $two, $three = 42)
EOT
],
'Renders a method nullable parameter' => [
new Method('hello', Visibility::private(), Parameters::fromParameters([
new Parameter('two', Type::fromString('?string')),
])),
<<<'EOT'
private function hello(?string $two)
EOT
],
'Renders a method parameter passed as a reference' => [
Expand Down Expand Up @@ -317,6 +325,17 @@ private function hello()
),
<<<'EOT'
private function hello(): Hello
EOT
],
'Renders method nullable return type' => [
new Method(
'hello',
Visibility::private(),
Parameters::empty(),
ReturnType::fromString('?Hello')
),
<<<'EOT'
private function hello(): ?Hello
EOT
],
'Renders a class with a parent' => [
Expand Down
59 changes: 58 additions & 1 deletion tests/Adapter/UpdaterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phpactor\CodeBuilder\Domain\Code;
use Phpactor\CodeBuilder\Domain\Builder\SourceCodeBuilder;
use Phpactor\CodeBuilder\Domain\Prototype\SourceCode;
use Phpactor\WorseReflection\Core\Type;

abstract class UpdaterTestCase extends TestCase
{
Expand Down Expand Up @@ -895,7 +896,7 @@ class Aardvark
EOT
];

yield 'It adds a documented properties' => [
yield 'It adds a typed property' => [
<<<'EOT'
class Aardvark
{
Expand All @@ -917,6 +918,33 @@ class Aardvark
*/
public $propertyOne;
}
EOT
];

yield 'It adds a nullable typed property' => [
<<<'EOT'
class Aardvark
{
public $eyes
}
EOT
, SourceCodeBuilder::create()
->class('Aardvark')
->property('propertyOne')->type(
Type::fromString('Hello')->asNullable()
)->end()
->end()
->build(),
<<<'EOT'
class Aardvark
{
public $eyes

/**
* @var Hello|null
*/
public $propertyOne;
}
EOT
];

Expand Down Expand Up @@ -1279,6 +1307,35 @@ public function methodOne(Barf $sniff)
{
}
}
EOT
];

yield 'It adds nullable typed parameters' => [
<<<'EOT'
class Aardvark
{
public function methodOne()
{
}
}
EOT
, SourceCodeBuilder::create()
->class('Aardvark')
->method('methodOne')
->parameter('sniff')->type(
Type::fromString('Barf')->asNullable()
)
->end()
->end()
->end()
->build(),
<<<'EOT'
class Aardvark
{
public function methodOne(?Barf $sniff)
{
}
}
EOT
];

Expand Down