Skip to content
This repository

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

Closed
wants to merge 2 commits into from

2 participants

Marco Pivetta Matthew Weier O'Phinney
Marco Pivetta
Collaborator

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

This PR is BC.

Marco Pivetta Ocramius referenced this pull request in Ocramius/ProxyManager
Merged

License compatibility #18

Marco Pivetta Ocramius referenced this pull request in Ocramius/ProxyManager
Closed

`callable` type introspection for method parameters #21

Marco Pivetta Ocramius referenced this pull request in Ocramius/ProxyManager
Merged

Allowing callable type hint for methods #23

Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/4150'
Close #4150
096862b
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/4150' into develop
Forward port #4150
f3cec14
Marco Pivetta Ocramius deleted the branch
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.
2  library/Zend/Code/Generator/ParameterGenerator.php
@@ -56,6 +56,8 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
56 56
57 57 if ($reflectionParameter->isArray()) {
58 58 $param->setType('array');
  59 + } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
  60 + $param->setType('callable');
59 61 } else {
60 62 $typeClass = $reflectionParameter->getClass();
61 63 if ($typeClass) {
2  library/Zend/Code/Reflection/ParameterReflection.php
@@ -77,6 +77,8 @@ public function getType()
77 77 {
78 78 if ($this->isArray()) {
79 79 return 'array';
  80 + } elseif (method_exists($this, 'isCallable') && $this->isCallable()) {
  81 + return 'callable';
80 82 }
81 83
82 84 if (($class = $this->getClass()) instanceof \ReflectionClass) {
14 tests/ZendTest/Code/Generator/ParameterGeneratorTest.php
@@ -12,6 +12,7 @@
12 12
13 13 use Zend\Code\Generator\ParameterGenerator;
14 14 use Zend\Code\Generator\ValueGenerator;
  15 +use Zend\Code\Reflection\ParameterReflection;
15 16
16 17 /**
17 18 * @category Zend
@@ -116,6 +117,19 @@ public function testFromReflectionGetWithNativeType()
116 117 $this->assertEquals('', $codeGenParam->getType());
117 118 }
118 119
  120 + public function testCallableTypeHint()
  121 + {
  122 + if (PHP_VERSION_ID < 50400) {
  123 + $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
  124 + }
  125 +
  126 + $parameter = ParameterGenerator::fromReflection(
  127 + new ParameterReflection(array('ZendTest\Code\Generator\TestAsset\CallableTypeHintClass', 'foo'), 'bar')
  128 + );
  129 +
  130 + $this->assertEquals('callable', $parameter->getType());
  131 + }
  132 +
119 133 /**
120 134 * @dataProvider dataFromReflectionGenerate
121 135 * @param string $methodName
10 tests/ZendTest/Code/Generator/TestAsset/CallableTypeHintClass.php
... ... @@ -0,0 +1,10 @@
  1 +<?php
  2 +
  3 +namespace ZendTest\Code\Generator\TestAsset;
  4 +
  5 +class CallableTypeHintClass
  6 +{
  7 + public function foo(callable $bar)
  8 + {
  9 + }
  10 +}
11 tests/ZendTest/Code/Reflection/ParameterReflectionTest.php
@@ -48,6 +48,17 @@ public function testTypeReturn($param, $type)
48 48 $this->assertEquals($type, $parameter->getType());
49 49 }
50 50
  51 + public function testCallableTypeHint()
  52 + {
  53 + if (PHP_VERSION_ID < 50400) {
  54 + $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
  55 + }
  56 +
  57 + $parameter = new Reflection\ParameterReflection(array('ZendTest\Code\Reflection\TestAsset\CallableTypeHintClass', 'foo'), 'bar');
  58 +
  59 + $this->assertEquals('callable', $parameter->getType());
  60 + }
  61 +
51 62 public function paramTypeTestProvider()
52 63 {
53 64 return array(
10 tests/ZendTest/Code/Reflection/TestAsset/CallableTypeHintClass.php
... ... @@ -0,0 +1,10 @@
  1 +<?php
  2 +
  3 +namespace ZendTest\Code\Reflection\TestAsset;
  4 +
  5 +class CallableTypeHintClass
  6 +{
  7 + public function foo(callable $bar)
  8 + {
  9 + }
  10 +}

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.