Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Closed
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
19 changes: 15 additions & 4 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function fromArray(array $array)
{
if (!isset($array['name'])) {
throw new Exception\InvalidArgumentException(
'Paramerer generator requires that a name is provided for this object'
'Parameter generator requires that a name is provided for this object'
);
}

Expand Down Expand Up @@ -163,14 +163,25 @@ public function __construct(
}

/**
* @param string $type
* @param string|TypeGenerator $type
* @throws Exception\InvalidArgumentException
* @return ParameterGenerator
*/
public function setType($type)
{
$this->type = TypeGenerator::fromTypeString($type);
if (is_string($type)) {
$this->type = TypeGenerator::fromTypeString($type);
return $this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate return $this; could be moved to the bottom outside the if statements

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He's returning early here.

}

return $this;
if ($type instanceof TypeGenerator) {
$this->type = $type;
return $this;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this block, you should throw an exception (unknown parameter type passed in)


throw new Exception\InvalidArgumentException(
'Unknown parameter type passed in'
);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ final class TypeGenerator implements GeneratorInterface
*/
private $type;

/**
* @var string;
*/
private $aliased = false;

/**
* @var string[]
*
Expand All @@ -44,7 +49,7 @@ final class TypeGenerator implements GeneratorInterface
*
* @throws InvalidArgumentException
*/
public static function fromTypeString($type)
public static function fromTypeString($type, $aliased = false)
{
list($wasTrimmed, $trimmedType) = self::trimType($type);

Expand All @@ -69,6 +74,7 @@ public static function fromTypeString($type)

$instance->type = $trimmedType;
$instance->isInternalPhpType = self::isInternalPhpType($trimmedType);
$instance->aliased = (boolean) $aliased;

return $instance;
}
Expand All @@ -86,7 +92,7 @@ public function generate()
return strtolower($this->type);
}

return '\\' . $this->type;
return ($this->aliased) ? $this->type : '\\' . $this->type;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace ZendTest\Code\Generator;

use Zend\Code\Exception\InvalidArgumentException;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\ValueGenerator;
use Zend\Code\Generator\TypeGenerator;
use Zend\Code\Reflection\ParameterReflection;
use ZendTest\Code\TestAsset\ClassTypeHintedClass;
use ZendTest\Code\TestAsset\DocBlockOnlyHintsClass;
Expand All @@ -30,6 +32,21 @@ public function testTypeGetterAndSetterPersistValue()
$this->assertEquals('Foo', $parameterGenerator->getType());
}

public function testTypeSetterWithTypeGenerator()
{
$parameterGenerator = new ParameterGenerator();
$parameterGenerator->setType(TypeGenerator::fromTypeString('Foo'));
$this->assertEquals('Foo', $parameterGenerator->getType());
}

public function testTypeSetterRejectInvalidType()
{
$this->setExpectedException(InvalidArgumentException::class);

$parameterGenerator = new ParameterGenerator();
$parameterGenerator->setType(new \stdclass);
}

public function testNameGetterAndSetterPersistValue()
{
$parameterGenerator = new ParameterGenerator();
Expand Down
81 changes: 81 additions & 0 deletions test/Generator/TypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,87 @@ public function testRejectsInvalidTypeString(string $typeString)
TypeGenerator::fromTypeString($typeString);
}

/**
* @dataProvider validTypeArrayProvider
*
* @param string $typeArray
* @param string $expectedReturnType
*/
public function testFromValidStringAlias(array $typeArray, string $expectedReturnType)
{
$generator = TypeGenerator::fromTypeString($typeArray['name'], $typeArray['alias']);

self::assertSame($expectedReturnType, $generator->generate());
}

public function validTypeArrayProvider()
{
return [
[['name' => 'foo', 'alias' => true], 'foo'],
[['name' => 'foo', 'alias' => false], '\\foo'],
[['name' => '\\foo', 'alias' => false], '\\foo'],
[['name' => '\\foo', 'alias' => true], 'foo'],
[['name' => 'a\\b\\c', 'alias' => false], '\\a\\b\\c'],
[['name' => 'a\\b\\c', 'alias' => true], 'a\\b\\c'],
[['name' => 'array', 'alias' => false], 'array'],
[['name' => 'array', 'alias' => true], 'array'],
[['name' => 'Array', 'alias' => false], 'array'],
[['name' => 'Array', 'alias' => true], 'array'],
[['name' => 'ARRAY', 'alias' => false], 'array'],
[['name' => 'ARRAY', 'alias' => true], 'array'],
[['name' => 'callable', 'alias' => false], 'callable'],
[['name' => 'callable', 'alias' => true], 'callable'],
[['name' => 'Callable', 'alias' => false], 'callable'],
[['name' => 'Callable', 'alias' => true], 'callable'],
[['name' => 'CALLABLE', 'alias' => false], 'callable'],
[['name' => 'CALLABLE', 'alias' => true], 'callable'],
[['name' => 'string', 'alias' => false], 'string'],
[['name' => 'string', 'alias' => true], 'string'],
[['name' => 'String', 'alias' => false], 'string'],
[['name' => 'String', 'alias' => true], 'string'],
[['name' => 'STRING', 'alias' => false], 'string'],
[['name' => 'STRING', 'alias' => true], 'string'],
[['name' => 'int', 'alias' => false], 'int'],
[['name' => 'int', 'alias' => true], 'int'],
[['name' => 'Int', 'alias' => false], 'int'],
[['name' => 'Int', 'alias' => true], 'int'],
[['name' => 'INT', 'alias' => false], 'int'],
[['name' => 'INT', 'alias' => true], 'int'],
[['name' => 'float', 'alias' => false], 'float'],
[['name' => 'float', 'alias' => true], 'float'],
[['name' => 'Float', 'alias' => false], 'float'],
[['name' => 'Float', 'alias' => true], 'float'],
[['name' => 'FLOAT', 'alias' => false], 'float'],
[['name' => 'FLOAT', 'alias' => true], 'float'],
[['name' => 'bool', 'alias' => false], 'bool'],
[['name' => 'bool', 'alias' => true], 'bool'],
[['name' => 'Bool', 'alias' => false], 'bool'],
[['name' => 'Bool', 'alias' => true], 'bool'],
[['name' => 'BOOL', 'alias' => false], 'bool'],
[['name' => 'BOOL', 'alias' => true], 'bool'],
[['name' => 'object', 'alias' => false], '\\object'],
[['name' => 'object', 'alias' => true], 'object'],
[['name' => 'Object', 'alias' => false], '\\Object'],
[['name' => 'Object', 'alias' => true], 'Object'],
[['name' => 'OBJECT', 'alias' => false], '\\OBJECT'],
[['name' => 'OBJECT', 'alias' => true], 'OBJECT'],
[['name' => 'mixed', 'alias' => false], '\\mixed'],
[['name' => 'mixed', 'alias' => true], 'mixed'],
[['name' => 'Mixed', 'alias' => false], '\\Mixed'],
[['name' => 'Mixed', 'alias' => true], 'Mixed'],
[['name' => 'MIXED', 'alias' => false], '\\MIXED'],
[['name' => 'MIXED', 'alias' => true], 'MIXED'],
[['name' => 'resource', 'alias' => false], '\\resource'],
[['name' => 'resource', 'alias' => true], 'resource'],
[['name' => 'Resource', 'alias' => false], '\\Resource'],
[['name' => 'Resource', 'alias' => true], 'Resource'],
[['name' => 'RESOURCE', 'alias' => false], '\\RESOURCE'],
[['name' => 'RESOURCE', 'alias' => true], 'RESOURCE'],
[['name' => 'foo_bar', 'alias' => false], '\\foo_bar'],
[['name' => 'foo_bar', 'alias' => true], 'foo_bar'],
];
}

/**
* @return string[][]
*/
Expand Down