Skip to content

Commit

Permalink
Merge pull request #8 from sjokki/version-2
Browse files Browse the repository at this point in the history
Filter namespaces of return types, keep nullable when filtering a namespace
  • Loading branch information
gnugat authored Feb 6, 2018
2 parents 5cdb20e + efac3fb commit 2853280
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
37 changes: 37 additions & 0 deletions spec/Memio/TwigTemplateEngine/TwigExtension/TypeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the memio/twig-template-engine package.
*
* (c) Loïc Faugeron <faugeron.loic@gmail.com>
*
* 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');
}
}
11 changes: 9 additions & 2 deletions src/Memio/TwigTemplateEngine/TwigExtension/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion templates/collection/methods/pure_virtual.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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|filter_namespace : '' -}};
{#- Trimming lines -#}
2 changes: 1 addition & 1 deletion templates/method.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 -#}

0 comments on commit 2853280

Please sign in to comment.