Skip to content

[Fwb][EventDispatcher][HttpKernel] Fix getClosureScopeClass usage to describe callables #29172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,9 @@ private function getCallableData($callable, array $options = array())
}
$data['name'] = $r->name;

$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
$data['class'] = $scopeClass;
if (!$class) {
if ($class = $r->getClosureScopeClass()) {
$data['class'] = $class->name;
if (!$r->getClosureThis()) {
$data['static'] = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,9 @@ protected function describeCallable($callable, array $options = array())
}
$string .= "\n".sprintf('- Name: `%s`', $r->name);

$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
$string .= "\n".sprintf('- Class: `%s`', $class);
if (!$class) {
if ($class = $r->getClosureScopeClass()) {
$string .= "\n".sprintf('- Class: `%s`', $class->name);
if (!$r->getClosureThis()) {
$string .= "\n- Static: yes";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,7 @@ private function formatCallable($callable)
return 'Closure()';
}
if ($class = $r->getClosureScopeClass()) {
return sprintf('%s::%s()', $class, $r->name);
}
if ($class = $r->getClosureThis()) {
return sprintf('%s::%s()', \get_class($class), $r->name);
return sprintf('%s::%s()', $class->name, $r->name);
}

return $r->name.'()';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,9 @@ private function getCallableDocument($callable)
}
$callableXML->setAttribute('name', $r->name);

$class = ($class = $r->getClosureThis()) ? \get_class($class) : null;
if ($scopeClass = $r->getClosureScopeClass() ?: $class) {
$callableXML->setAttribute('class', $class);
if (!$class) {
if ($class = $r->getClosureScopeClass()) {
$callableXML->setAttribute('class', $class->name);
if (!$r->getClosureThis()) {
$callableXML->setAttribute('static', 'true');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static function getEventDispatchers()

public static function getCallables()
{
return array(
$callables = array(
'callable_1' => 'array_key_exists',
'callable_2' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass', 'staticMethod'),
'callable_3' => array(new CallableClass(), 'method'),
Expand All @@ -164,6 +164,12 @@ public static function getCallables()
'callable_6' => function () { return 'Closure'; },
'callable_7' => new CallableClass(),
);

if (\PHP_VERSION_ID >= 70100) {
$callables['callable_from_callable'] = \Closure::fromCallable(new CallableClass());
}

return $callables;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "closure",
"name": "__invoke",
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

- Type: `closure`
- Name: `__invoke`
- Class: `Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass::__invoke()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<callable type="closure" name="__invoke" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\CallableClass"/>
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatc
$r = new \ReflectionFunction($listener);
if (false !== strpos($r->name, '{closure}')) {
$this->pretty = $this->name = 'closure';
} elseif ($this->name = $r->getClosureScopeClass()) {
$this->pretty = $this->name.'::'.$r->name;
} elseif ($class = $r->getClosureThis()) {
$this->name = \get_class($class);
} elseif ($class = $r->getClosureScopeClass()) {
$this->name = $class->name;
$this->pretty = $this->name.'::'.$r->name;
} else {
$this->pretty = $this->name = $r->name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher\Tests\Debug;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class WrappedListenerTest extends TestCase
{
/**
* @dataProvider provideListenersToDescribe
*/
public function testListenerDescription(callable $listener, $expected)
{
$wrappedListener = new WrappedListener($listener, null, $this->getMockBuilder(Stopwatch::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock());

$this->assertStringMatchesFormat($expected, $wrappedListener->getPretty());
}

public function provideListenersToDescribe()
{
$listeners = array(
array(new FooListener(), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::__invoke'),
array(array(new FooListener(), 'listen'), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'),
array(array('Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic'), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic'),
array('var_dump', 'var_dump'),
array(function () {}, 'closure'),
);

if (\PHP_VERSION_ID >= 70100) {
$listeners[] = array(\Closure::fromCallable(array(new FooListener(), 'listen')), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen');
$listeners[] = array(\Closure::fromCallable(array('Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic')), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic');
$listeners[] = array(\Closure::fromCallable(function () {}), 'closure');
}

return $listeners;
}
}

class FooListener
{
public function listen()
{
}

public function __invoke()
{
}

public static function listenStatic()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,7 @@ protected function parseController($controller)
$controller['method'] = $r->name;

if ($class = $r->getClosureScopeClass()) {
$controller['class'] = $class;
} elseif ($class = $r->getClosureThis()) {
$controller['class'] = \get_class($class);
$controller['class'] = $class->name;
} else {
return $r->name;
}
Expand Down
8 changes: 1 addition & 7 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested,
$a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);

if (false === strpos($c->name, '{closure}')) {
if (isset($a[$prefix.'class'])) {
$stub->class = $a[$prefix.'class']->value.'::'.$c->name;
} elseif (isset($a[$prefix.'this'])) {
$stub->class = $a[$prefix.'this']->class.'::'.$c->name;
} else {
$stub->class = $c->name;
}
$stub->class = isset($a[$prefix.'class']) ? $a[$prefix.'class']->value.'::'.$c->name : $c->name;
unset($a[$prefix.'class']);
}

Expand Down