Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Add method getPrototype in FunctionReflection class
Browse files Browse the repository at this point in the history
  • Loading branch information
blanchonvincent committed Oct 12, 2013
1 parent e0fd743 commit fc3f37f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
65 changes: 64 additions & 1 deletion library/Zend/Code/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

class FunctionReflection extends ReflectionFunction implements ReflectionInterface
{
/**
* Constant use in @MethodReflection to display prototype as an array
*/
const PROTOTYPE_AS_ARRAY = 'prototype_as_array';

/**
* Constant use in @MethodReflection to display prototype as a string
*/
const PROTOTYPE_AS_STRING = 'prototype_as_string';

/**
* Get function DocBlock
*
Expand Down Expand Up @@ -54,7 +64,7 @@ public function getStartLine($includeDocComment = false)
/**
* Get contents of function
*
* @param bool $includeDocBlock
* @param bool $includeDocBlock
* @return string
*/
public function getContents($includeDocBlock = true)
Expand All @@ -69,6 +79,58 @@ public function getContents($includeDocBlock = true)
);
}

/**
* Get method prototype
*
* @return array
*/
public function getPrototype($format = FunctionReflection::PROTOTYPE_AS_ARRAY)
{
$returnType = 'mixed';
$docBlock = $this->getDocBlock();
if ($docBlock) {
/** @var Zend\Code\Reflection\DocBlock\Tag\ReturnTag $return */
$return = $docBlock->getTag('return');
$returnTypes = $return->getTypes();
$returnType = count($returnTypes) > 1 ? implode('|', $returnTypes) : $returnTypes[0];
}

$prototype = array(
'namespace' => $this->getNamespaceName(),
'name' => substr($this->getName(), strlen($this->getNamespaceName()) + 1),
'return' => $returnType,
'arguments' => array(),
);

$parameters = $this->getParameters();
foreach ($parameters as $parameter) {
$prototype['arguments'][$parameter->getName()] = array(
'type' => $parameter->getType(),
'required' => !$parameter->isOptional(),
'by_ref' => $parameter->isPassedByReference(),
'default' => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
);
}

if ($format == FunctionReflection::PROTOTYPE_AS_STRING) {
$line = $prototype['return'] . ' ' . $prototype['name'] . '(';
$args = array();
foreach ($prototype['arguments'] as $name => $argument) {
$argsLine = ($argument['type'] ? $argument['type'] . ' ' : '') . ($argument['by_ref'] ? '&' : '') . '$' . $name;
if (!$argument['required']) {
$argsLine .= ' = ' . var_export($argument['default'], true);
}
$args[] = $argsLine;
}
$line .= implode(', ', $args);
$line .= ')';

return $line;
}

return $prototype;
}

/**
* Get function parameters
*
Expand Down Expand Up @@ -104,6 +166,7 @@ public function getReturn()
}

$tag = $docBlock->getTag('return');

return new DocBlockReflection('@return ' . $tag->getDescription());
}

Expand Down
28 changes: 28 additions & 0 deletions tests/ZendTest/Code/Reflection/FunctionReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ public function testFunctionDocBlockReturn()
$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function6');
$this->assertInstanceOf('Zend\Code\Reflection\DocBlockReflection', $function->getDocBlock());
}

public function testGetPrototypeMethod()
{
require_once __DIR__ . '/TestAsset/functions.php';

$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function2');
$prototype = array(
'namespace' => 'ZendTest\Code\Reflection\TestAsset',
'name' => 'function2',
'return' => 'string',
'arguments' => array(
'one' => array(
'type' => 'string',
'required' => true,
'by_ref' => false,
'default' => null,
),
'two' => array(
'type' => 'string',
'required' => false,
'by_ref' => false,
'default' => 'two',
),
),
);
$this->assertEquals($prototype, $function->getPrototype());
$this->assertEquals('string function2(string $one, string $two = \'two\')', $function->getPrototype(FunctionReflection::PROTOTYPE_AS_STRING));
}
}
4 changes: 2 additions & 2 deletions tests/ZendTest/Code/Reflection/TestAsset/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function function1()
*
* This is the long description for funciton two
*
* @param unknown_type $one
* @param unknown_type $two
* @param string $one
* @param string $two
* @return string
*/
function function2($one, $two = 'two')
Expand Down

0 comments on commit fc3f37f

Please sign in to comment.