Skip to content

Commit

Permalink
Cleanups in the parameter generator (further removal of conditionals)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Nov 28, 2013
1 parent 6b9887f commit 6a0bdb0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
42 changes: 31 additions & 11 deletions src/ProxyManager/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,49 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
{
/* @var $param self */
$param = new static();

$param->setName($reflectionParameter->getName());
$param->setPosition($reflectionParameter->getPosition());

if ($reflectionParameter->isArray()) {
$param->setType('array');
} elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
$param->setType('callable');
} else {
$typeClass = $reflectionParameter->getClass();
if ($typeClass) {
$param->setType($typeClass->getName());
}
}
$type = static::extractParameterType($reflectionParameter);

$param->setPosition($reflectionParameter->getPosition());
if (null !== $type) {
$param->setType($type);
}

if ($reflectionParameter->isOptional()) {
$param->setDefaultValue($reflectionParameter->getDefaultValue());
}

$param->setPassedByReference($reflectionParameter->isPassedByReference());

return $param;
}

/**
* Retrieves the type of a reflection parameter (null if none is found)
*
* @param ParameterReflection $reflectionParameter
*
* @return string|null
*/
private static function extractParameterType(ParameterReflection $reflectionParameter)
{
if ($reflectionParameter->isArray()) {
return 'array';
}

if (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
return 'callable';
}

if ($typeClass = $reflectionParameter->getClass()) {
return $typeClass->getName();
}

return null;
}

/**
* @return string
*/
Expand Down
23 changes: 2 additions & 21 deletions tests/ProxyManagerTest/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*
* @covers \ProxyManager\Generator\ParameterGenerator
*/
class ParameterGeneratorTest extends PHPUnit_Framework_TestCase
{
/**
* @covers \ProxyManager\Generator\ParameterGenerator::generate
* @covers \ProxyManager\Generator\ParameterGenerator::getGeneratedType
*/
public function testGeneratesProperTypeHint()
{
$generator = new ParameterGenerator('foo');
Expand All @@ -48,10 +46,6 @@ public function testGeneratesProperTypeHint()
$this->assertSame('\\fooClass $foo', $generator->generate());
}

/**
* @covers \ProxyManager\Generator\ParameterGenerator::generate
* @covers \ProxyManager\Generator\ParameterGenerator::getGeneratedType
*/
public function testGeneratesMethodWithCallableType()
{
if (PHP_VERSION_ID < 50400) {
Expand All @@ -66,9 +60,6 @@ public function testGeneratesMethodWithCallableType()
$this->assertSame('callable $foo', $generator->generate());
}

/**
* @covers \ProxyManager\Generator\ParameterGenerator::fromReflection
*/
public function testVisitMethodWithCallable()
{
if (PHP_VERSION_ID < 50400) {
Expand All @@ -85,11 +76,6 @@ public function testVisitMethodWithCallable()
$this->assertSame('callable', $generator->getType());
}

/**
* @covers \ProxyManager\Generator\ParameterGenerator::fromReflection
* @covers \ProxyManager\Generator\ParameterGenerator::generate
* @covers \ProxyManager\Generator\ParameterGenerator::getGeneratedType
*/
public function testReadsParameterDefaults()
{
$parameter = ParameterGenerator::fromReflection(new ParameterReflection(
Expand All @@ -109,11 +95,6 @@ public function testReadsParameterDefaults()
$this->assertStringMatchesFormat('array%a$parameter%a=%aarray(\'foo\')', $parameter->generate());
}

/**
* @covers \ProxyManager\Generator\ParameterGenerator::fromReflection
* @covers \ProxyManager\Generator\ParameterGenerator::generate
* @covers \ProxyManager\Generator\ParameterGenerator::getGeneratedType
*/
public function testReadsParameterTypeHint()
{
$parameter = ParameterGenerator::fromReflection(new ParameterReflection(
Expand Down

0 comments on commit 6a0bdb0

Please sign in to comment.