From cbdd392bd54490e0f0b8f566ef97d51e802ac3c5 Mon Sep 17 00:00:00 2001 From: Ashoka de Wit Date: Tue, 6 Feb 2018 13:40:18 +0800 Subject: [PATCH 1/3] Add return type template for interface methods. This makes the template of pure_virtual.twig follow that of method.twig. --- templates/collection/methods/pure_virtual.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/collection/methods/pure_virtual.twig b/templates/collection/methods/pure_virtual.twig index 424c727..c9608a1 100644 --- a/templates/collection/methods/pure_virtual.twig +++ b/templates/collection/methods/pure_virtual.twig @@ -3,5 +3,5 @@ function {{ method.name }}({% include 'collection/argument_collection.twig' with { 'argument_collection': method.allArguments, 'length_restriction': 22 + method.name | length - } only %}); + } only %}){{ method.returnType is not empty ? ' : ' ~ method.returnType : '' -}}; {#- Trimming lines -#} From 084649ca435e8c5ed48e30489423c9a682cbe506 Mon Sep 17 00:00:00 2001 From: Ashoka de Wit Date: Tue, 6 Feb 2018 13:41:40 +0800 Subject: [PATCH 2/3] Filter the namespace of method return types. The return type is filtered in the same way as method argument types. --- templates/collection/methods/pure_virtual.twig | 2 +- templates/method.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/collection/methods/pure_virtual.twig b/templates/collection/methods/pure_virtual.twig index c9608a1..860db87 100644 --- a/templates/collection/methods/pure_virtual.twig +++ b/templates/collection/methods/pure_virtual.twig @@ -3,5 +3,5 @@ function {{ method.name }}({% include 'collection/argument_collection.twig' with { 'argument_collection': method.allArguments, 'length_restriction': 22 + method.name | length - } only %}){{ method.returnType is not empty ? ' : ' ~ method.returnType : '' -}}; + } only %}){{ method.returnType is not empty ? ' : ' ~ method.returnType|filter_namespace : '' -}}; {#- Trimming lines -#} diff --git a/templates/method.twig b/templates/method.twig index cecd552..1f1d87e 100644 --- a/templates/method.twig +++ b/templates/method.twig @@ -5,6 +5,6 @@ function {{ method.name }}({% include 'collection/argument_collection.twig' with { 'argument_collection': method.allArguments, 'length_restriction': 22 + method.name | length - } only %}){{ method.returnType is not empty ? ' : ' ~ method.returnType : '' -}} + } only %}){{ method.returnType is not empty ? ' : ' ~ method.returnType|filter_namespace : '' -}} {%- include 'method_body.twig' with { 'method': method } only %} {#- Trimming lines -#} From efac3fb1ba4c322c495da16992c4344a670b3332 Mon Sep 17 00:00:00 2001 From: Ashoka de Wit Date: Tue, 6 Feb 2018 13:42:27 +0800 Subject: [PATCH 3/3] Check for nullable when filtering namespaces. The method filter_namespace() was not compatible with nullable types. Before: ?Vendor\Project\MyClass became just MyClass ?DateTime stayed ?DateTime After: ?Vendor\Project\MyClass becomes ?MyClass ?DateTime stays ?DateTime --- .../TwigExtension/TypeSpec.php | 37 +++++++++++++++++++ .../TwigTemplateEngine/TwigExtension/Type.php | 11 +++++- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 spec/Memio/TwigTemplateEngine/TwigExtension/TypeSpec.php diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/TypeSpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/TypeSpec.php new file mode 100644 index 0000000..7011401 --- /dev/null +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/TypeSpec.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace spec\Memio\TwigTemplateEngine\TwigExtension; + +use PhpSpec\ObjectBehavior; + +class TypeSpec extends ObjectBehavior +{ + public function it_can_be_a_non_object() + { + $this->filterNamespace('int')->shouldBe('int'); + } + + public function it_can_be_a_nullable_non_object() + { + $this->filterNamespace('?int')->shouldBe('?int'); + } + + public function it_can_be_an_object() + { + $this->filterNamespace('Vendor\Project\MyClass')->shouldBe('MyClass'); + } + + public function it_can_be_a_nullable_object() + { + $this->filterNamespace('?Vendor\Project\MyClass')->shouldBe('?MyClass'); + } +} diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Type.php b/src/Memio/TwigTemplateEngine/TwigExtension/Type.php index 648819a..5ae0fa7 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Type.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Type.php @@ -61,12 +61,19 @@ public function hasTypehint($model) : bool public function filterNamespace(string $stringType) : string { + $nullablePrefix = substr($stringType, 0, 1) === '?' + ? '?' + : ''; + + $stringType = ltrim(ltrim($stringType, '?')); + $type = new ModelType($stringType); if (!$type->isObject()) { - return $stringType; + return $nullablePrefix.$stringType; } + $fullyQualifiedName = new FullyQualifiedName($stringType); - return $fullyQualifiedName->getName(); + return $nullablePrefix.$fullyQualifiedName->getName(); } }