Skip to content

Commit 49ddced

Browse files
minor #29172 [Fwb][EventDispatcher][HttpKernel] Fix getClosureScopeClass usage to describe callables (ogizanagi)
This PR was merged into the 3.4 branch. Discussion ---------- [Fwb][EventDispatcher][HttpKernel] Fix getClosureScopeClass usage to describe callables | Q | A | ------------- | --- | Branch? | 3.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29054 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A `\ReflectionFunctionAbstract::getClosureScopeClass` returns a `\ReflectionClass` instance, not the class name. Before this patch: ```diff --- Expected +++ Actual @@ @@ -'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen' +'Class [ <user> class Symfony\Component\EventDispatcher\Tests\Debug\FooListener ] { + @@ [...]/src/Symfony/Component/EventDispatcher/Tests/Debug/WrappedListenerTest.php 28-33 + + - Constants [0] { + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [0] { + } + + - Methods [1] { + Method [ <user> public method listen ] { + @@ [...]/src/Symfony/Component/EventDispatcher/Tests/Debug/WrappedListenerTest.php 30 - 32 + } + } +} +::listen' ``` Commits ------- 61e4592 [Fwb][EventDispatcher][HttpKernel] Fix getClosureScopeClass usage to describe callables
2 parents b6dac0f + 61e4592 commit 49ddced

File tree

13 files changed

+97
-31
lines changed

13 files changed

+97
-31
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,9 @@ private function getCallableData($callable, array $options = array())
374374
}
375375
$data['name'] = $r->name;
376376

377-
$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
378-
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
379-
$data['class'] = $scopeClass;
380-
if (!$class) {
377+
if ($class = $r->getClosureScopeClass()) {
378+
$data['class'] = $class->name;
379+
if (!$r->getClosureThis()) {
381380
$data['static'] = true;
382381
}
383382
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,9 @@ protected function describeCallable($callable, array $options = array())
360360
}
361361
$string .= "\n".sprintf('- Name: `%s`', $r->name);
362362

363-
$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
364-
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
365-
$string .= "\n".sprintf('- Class: `%s`', $class);
366-
if (!$class) {
363+
if ($class = $r->getClosureScopeClass()) {
364+
$string .= "\n".sprintf('- Class: `%s`', $class->name);
365+
if (!$r->getClosureThis()) {
367366
$string .= "\n- Static: yes";
368367
}
369368
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,7 @@ private function formatCallable($callable)
474474
return 'Closure()';
475475
}
476476
if ($class = $r->getClosureScopeClass()) {
477-
return sprintf('%s::%s()', $class, $r->name);
478-
}
479-
if ($class = $r->getClosureThis()) {
480-
return sprintf('%s::%s()', \get_class($class), $r->name);
477+
return sprintf('%s::%s()', $class->name, $r->name);
481478
}
482479

483480
return $r->name.'()';

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,9 @@ private function getCallableDocument($callable)
586586
}
587587
$callableXML->setAttribute('name', $r->name);
588588

589-
$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
590-
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
591-
$callableXML->setAttribute('class', $class);
592-
if (!$class) {
589+
if ($class = $r->getClosureScopeClass()) {
590+
$callableXML->setAttribute('class', $class->name);
591+
if (!$r->getClosureThis()) {
593592
$callableXML->setAttribute('static', 'true');
594593
}
595594
}

src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static function getEventDispatchers()
155155

156156
public static function getCallables()
157157
{
158-
return array(
158+
$callables = array(
159159
'callable_1' => 'array_key_exists',
160160
'callable_2' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass', 'staticMethod'),
161161
'callable_3' => array(new CallableClass(), 'method'),
@@ -164,6 +164,12 @@ public static function getCallables()
164164
'callable_6' => function () { return 'Closure'; },
165165
'callable_7' => new CallableClass(),
166166
);
167+
168+
if (\PHP_VERSION_ID >= 70100) {
169+
$callables['callable_from_callable'] = \Closure::fromCallable(new CallableClass());
170+
}
171+
172+
return $callables;
167173
}
168174
}
169175

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "closure",
3+
"name": "__invoke",
4+
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
- Type: `closure`
3+
- Name: `__invoke`
4+
- Class: `Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass::__invoke()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<callable type="closure" name="__invoke" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass"/>

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatc
4646
$r = new \ReflectionFunction($listener);
4747
if (false !== strpos($r->name, '{closure}')) {
4848
$this->pretty = $this->name = 'closure';
49-
} elseif ($this->name = $r->getClosureScopeClass()) {
50-
$this->pretty = $this->name.'::'.$r->name;
51-
} elseif ($class = $r->getClosureThis()) {
52-
$this->name = \get_class($class);
49+
} elseif ($class = $r->getClosureScopeClass()) {
50+
$this->name = $class->name;
5351
$this->pretty = $this->name.'::'.$r->name;
5452
} else {
5553
$this->pretty = $this->name = $r->name;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher\Tests\Debug;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
16+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17+
use Symfony\Component\Stopwatch\Stopwatch;
18+
19+
class WrappedListenerTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider provideListenersToDescribe
23+
*/
24+
public function testListenerDescription(callable $listener, $expected)
25+
{
26+
$wrappedListener = new WrappedListener($listener, null, $this->getMockBuilder(Stopwatch::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock());
27+
28+
$this->assertStringMatchesFormat($expected, $wrappedListener->getPretty());
29+
}
30+
31+
public function provideListenersToDescribe()
32+
{
33+
$listeners = array(
34+
array(new FooListener(), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::__invoke'),
35+
array(array(new FooListener(), 'listen'), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'),
36+
array(array('Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic'), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic'),
37+
array('var_dump', 'var_dump'),
38+
array(function () {}, 'closure'),
39+
);
40+
41+
if (\PHP_VERSION_ID >= 70100) {
42+
$listeners[] = array(\Closure::fromCallable(array(new FooListener(), 'listen')), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen');
43+
$listeners[] = array(\Closure::fromCallable(array('Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic')), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic');
44+
$listeners[] = array(\Closure::fromCallable(function () {}), 'closure');
45+
}
46+
47+
return $listeners;
48+
}
49+
}
50+
51+
class FooListener
52+
{
53+
public function listen()
54+
{
55+
}
56+
57+
public function __invoke()
58+
{
59+
}
60+
61+
public static function listenStatic()
62+
{
63+
}
64+
}

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,7 @@ protected function parseController($controller)
393393
$controller['method'] = $r->name;
394394

395395
if ($class = $r->getClosureScopeClass()) {
396-
$controller['class'] = $class;
397-
} elseif ($class = $r->getClosureThis()) {
398-
$controller['class'] = \get_class($class);
396+
$controller['class'] = $class->name;
399397
} else {
400398
return $r->name;
401399
}

src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested,
4040
$a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
4141

4242
if (false === strpos($c->name, '{closure}')) {
43-
if (isset($a[$prefix.'class'])) {
44-
$stub->class = $a[$prefix.'class']->value.'::'.$c->name;
45-
} elseif (isset($a[$prefix.'this'])) {
46-
$stub->class = $a[$prefix.'this']->class.'::'.$c->name;
47-
} else {
48-
$stub->class = $c->name;
49-
}
43+
$stub->class = isset($a[$prefix.'class']) ? $a[$prefix.'class']->value.'::'.$c->name : $c->name;
5044
unset($a[$prefix.'class']);
5145
}
5246

0 commit comments

Comments
 (0)