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
2 changes: 1 addition & 1 deletion lib/Domain/Builder/MethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MethodBuilder extends AbstractBuilder implements NamedBuilder
protected $visibility;

/**
* @var Type
* @var ReturnType
Comment thread
dantleech marked this conversation as resolved.
Resolved
*/
protected $returnType;

Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Prototype/ReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(Type $class)

public static function fromString($string)
{
return new self(Type::fromString($string));
return new self(Type::fromString($string, true));
}

public function __toString()
Expand Down
22 changes: 19 additions & 3 deletions lib/Domain/Prototype/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Phpactor\CodeBuilder\Domain\Prototype;

use Phpactor\CodeBuilder\Domain\Type\Exception\TypeCannotBeNullableException;

final class Type extends Prototype
{
/**
Expand All @@ -14,14 +16,23 @@ final class Type extends Prototype
*/
private $none = false;

public function __construct(string $type = null)
/**
* @var bool
*/
private $nullable = false;

public function __construct(string $type = null, bool $nullable = false)
{
$this->type = $type;
$this->nullable = $nullable;
}

public static function fromString(string $string): Type
{
return new self($string);
$nullable = 0 === strpos($string, '?');
$type = $nullable ? substr($string, 1) : $string;

return new self($type, $nullable);
}

public static function none(): Type
Expand All @@ -34,7 +45,7 @@ public static function none(): Type

public function __toString()
{
return $this->type;
return $this->nullable ? sprintf('?%s', $this->type) : $this->type;
}

public function namespace(): ?string
Expand All @@ -54,4 +65,9 @@ public function notNone(): bool
{
return false === $this->none;
}

public function nullable(): bool
{
return $this->nullable;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Domain/Builder/SourceCodeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ public function provideMethodBuilder()
return [
'Method return type' => [
$this->builder()->class('Dog')->method('one')
->returnType('string')
->returnType('?string')
->visibility('private')
->parameter('one')
->type('One')
->defaultValue(1)
->end(),
function ($method) {
$this->assertEquals('string', $method->returnType()->__toString());
$this->assertEquals('?string', $method->returnType()->__toString());
}
],
'Method mofifiers 1' => [
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/Domain/Prototype/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,37 @@ public function testItReturnsANamespace()
$type = Type::fromString('Foo\\Bar');
$this->assertEquals('Foo', $type->namespace());

$type = Type::fromString('?Foo\\Bar');
$this->assertEquals('Foo', $type->namespace());

$type = Type::fromString('Bar');
$this->assertNull($type->namespace());

$type = Type::fromString('?Bar');
$this->assertNull($type->namespace());

$type = Type::none();
$this->assertNull($type->namespace());
}

public function testItAllowsNullable()
{
$type = Type::fromString('Foo\\Bar');
$this->assertFalse($type->nullable());

$type = Type::fromString('string');
$this->assertFalse($type->nullable());

$type = Type::fromString('Foo\\Bar');
$this->assertFalse($type->nullable());

$type = Type::fromString('string');
$this->assertFalse($type->nullable());

$type = Type::fromString('?Foo\\Bar');
$this->assertTrue($type->nullable());

$type = Type::fromString('?string');
$this->assertTrue($type->nullable());
}
}