Skip to content

Loading…

Hotfix - `callable` type introspection for method parameters #4150

Closed
wants to merge 2 commits into from

2 participants

@Ocramius
Zend Framework member

This PR introduces a fix that allows Zend\Code to recognize the callable type hint on method parameters.

This PR is BC.

@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/4150' into develop
Forward port #4150
717bafe
@weierophinney weierophinney added a commit that closed this pull request
@weierophinney weierophinney Merge branch 'hotfix/4150'
Close #4150
01b7fba
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/4150'
Close #4150
096862b
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/4150' into develop
Forward port #4150
f3cec14
@Ocramius Ocramius deleted the Ocramius:hotfix/callable-type-introspection branch
@weierophinney weierophinney referenced this pull request
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2 library/Zend/Code/Generator/ParameterGenerator.php
@@ -56,6 +56,8 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
if ($reflectionParameter->isArray()) {
$param->setType('array');
+ } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
+ $param->setType('callable');
} else {
$typeClass = $reflectionParameter->getClass();
if ($typeClass) {
View
2 library/Zend/Code/Reflection/ParameterReflection.php
@@ -77,6 +77,8 @@ public function getType()
{
if ($this->isArray()) {
return 'array';
+ } elseif (method_exists($this, 'isCallable') && $this->isCallable()) {
+ return 'callable';
}
if (($class = $this->getClass()) instanceof \ReflectionClass) {
View
14 tests/ZendTest/Code/Generator/ParameterGeneratorTest.php
@@ -12,6 +12,7 @@
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\ValueGenerator;
+use Zend\Code\Reflection\ParameterReflection;
/**
* @category Zend
@@ -116,6 +117,19 @@ public function testFromReflectionGetWithNativeType()
$this->assertEquals('', $codeGenParam->getType());
}
+ public function testCallableTypeHint()
+ {
+ if (PHP_VERSION_ID < 50400) {
+ $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
+ }
+
+ $parameter = ParameterGenerator::fromReflection(
+ new ParameterReflection(array('ZendTest\Code\Generator\TestAsset\CallableTypeHintClass', 'foo'), 'bar')
+ );
+
+ $this->assertEquals('callable', $parameter->getType());
+ }
+
/**
* @dataProvider dataFromReflectionGenerate
* @param string $methodName
View
10 tests/ZendTest/Code/Generator/TestAsset/CallableTypeHintClass.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace ZendTest\Code\Generator\TestAsset;
+
+class CallableTypeHintClass
+{
+ public function foo(callable $bar)
+ {
+ }
+}
View
11 tests/ZendTest/Code/Reflection/ParameterReflectionTest.php
@@ -48,6 +48,17 @@ public function testTypeReturn($param, $type)
$this->assertEquals($type, $parameter->getType());
}
+ public function testCallableTypeHint()
+ {
+ if (PHP_VERSION_ID < 50400) {
+ $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
+ }
+
+ $parameter = new Reflection\ParameterReflection(array('ZendTest\Code\Reflection\TestAsset\CallableTypeHintClass', 'foo'), 'bar');
+
+ $this->assertEquals('callable', $parameter->getType());
+ }
+
public function paramTypeTestProvider()
{
return array(
View
10 tests/ZendTest/Code/Reflection/TestAsset/CallableTypeHintClass.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace ZendTest\Code\Reflection\TestAsset;
+
+class CallableTypeHintClass
+{
+ public function foo(callable $bar)
+ {
+ }
+}
Something went wrong with that request. Please try again.