Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5efed7b

Browse files
committed
Fix method conflicts with parent class
1 parent ae85387 commit 5efed7b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class PhpDumper extends Dumper
5858
private $targetDirRegex;
5959
private $targetDirMaxMatches;
6060
private $docStar;
61-
private $serviceIdToMethodNameMap = array();
62-
private $usedMethodNames = array();
61+
private $serviceIdToMethodNameMap;
62+
private $usedMethodNames;
6363

6464
/**
6565
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -108,6 +108,9 @@ public function dump(array $options = array())
108108
'namespace' => '',
109109
'debug' => true,
110110
), $options);
111+
112+
$this->initializeMethodNamesMap($options['base_class']);
113+
111114
$this->docStar = $options['debug'] ? '*' : '';
112115

113116
if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
@@ -1340,6 +1343,25 @@ private function getServiceCall($id, Reference $reference = null)
13401343
}
13411344
}
13421345

1346+
/**
1347+
* Initializes the method names map to avoid conflicts with the Container methods.
1348+
*
1349+
* @param string $class the container base class
1350+
*/
1351+
private function initializeMethodNamesMap($class)
1352+
{
1353+
$this->serviceIdToMethodNameMap = array();
1354+
$this->usedMethodNames = array();
1355+
1356+
try {
1357+
$reflectionClass = new \ReflectionClass($class);
1358+
foreach ($reflectionClass->getMethods() as $method) {
1359+
$this->usedMethodNames[strtolower($method->getName())] = true;
1360+
}
1361+
} catch (\ReflectionException $e) {
1362+
}
1363+
}
1364+
13431365
/**
13441366
* Convert a service id to a valid PHP method name.
13451367
*

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ public function testConflictingServiceIds()
158158
$this->assertTrue(method_exists($class, 'getFoobar2Service'));
159159
}
160160

161+
public function testConflictingMethodsWithParent()
162+
{
163+
$class = 'Symfony_DI_PhpDumper_Test_Conflicting_Method_With_Parent';
164+
$container = new ContainerBuilder();
165+
$container->register('bar', 'FooClass');
166+
$container->register('foo_bar', 'FooClass');
167+
$dumper = new PhpDumper($container);
168+
eval('?>'.$dumper->dump(array(
169+
'class' => $class,
170+
'base_class' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\containers\CustomContainer',
171+
)));
172+
173+
$this->assertTrue(method_exists($class, 'getBar2Service'));
174+
$this->assertTrue(method_exists($class, 'getFoobar2Service'));
175+
}
176+
161177
/**
162178
* @dataProvider provideInvalidFactories
163179
* @expectedException Symfony\Component\DependencyInjection\Exception\RuntimeException
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\containers;
4+
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
7+
8+
class CustomContainer extends Container
9+
{
10+
public function getBarService()
11+
{
12+
}
13+
14+
public function getFoobarService()
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)