Skip to content

Commit 98481b9

Browse files
committed
Fix @stof's comments
1 parent cf63181 commit 98481b9

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class AutowirePass implements CompilerPassInterface
3838
*/
3939
public function process(ContainerBuilder $container)
4040
{
41-
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
41+
$throwingAutoloader = function ($class) {
42+
throw new \ReflectionException(sprintf('Class %s does not exist', $class));
43+
};
4244
spl_autoload_register($throwingAutoloader);
4345

4446
$this->typeHelper = new ServiceTypeHelper($container);
@@ -256,7 +258,8 @@ private function getOfType($type, $serviceId)
256258
$services = $this->typeHelper->getOfType($type);
257259
if (1 === count($services)) {
258260
return $services[0];
259-
} elseif (1 < count($services)) {
261+
}
262+
if (1 < count($services)) {
260263
$classOrInterface = class_exists($type) ? 'class' : 'interface';
261264
$matchingServices = implode(', ', $services);
262265

@@ -315,7 +318,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
315318
$argumentDefinition = $this->container->register($argumentId, $typeHint->name);
316319
$argumentDefinition->setPublic(false);
317320

318-
$this->populateAvailableType($argumentId, $argumentDefinition);
321+
$this->typeHelper->reset();
319322

320323
try {
321324
$this->completeDefinition($argumentId, $argumentDefinition, array('__construct'));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\DependencyInjection\Tests\Fixtures;
13+
14+
class BadParent extends ThisDoesNotExist
15+
{
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\DependencyInjection\Tests\Util;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Tests\Fixtures\BadParent;
16+
use Symfony\Component\DependencyInjection\Util\ServiceTypeHelper;
17+
18+
class ServiceTypeHelperTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testIgnoreServiceWithClassNotExisting()
21+
{
22+
$container = new ContainerBuilder();
23+
$container->register('class_not_exist', 'NotExistingClass');
24+
25+
$helper = new ServiceTypeHelper($container);
26+
$this->assertEmpty($helper->getOfType('NotExistingClass'));
27+
}
28+
29+
public function testIgnoreServiceWithParentNotExisting()
30+
{
31+
$container = new ContainerBuilder();
32+
$container->register('bad_parent', BadParent::class);
33+
34+
$helper = new ServiceTypeHelper($container);
35+
$this->assertEmpty($helper->getOfType(BadParent::class));
36+
}
37+
}

src/Symfony/Component/DependencyInjection/Util/ServiceTypeHelper.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\DependencyInjection\Util;
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
15-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16-
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
1715
use Symfony\Component\DependencyInjection\Definition;
1816

1917
/**
@@ -24,7 +22,7 @@
2422
*/
2523
final class ServiceTypeHelper
2624
{
27-
private static $classesName;
25+
private static $classNames;
2826
private $container;
2927
private $typeMap;
3028

@@ -66,10 +64,17 @@ public function reset()
6664
*/
6765
private function populateAvailableTypes()
6866
{
67+
$throwingAutoloader = function ($class) {
68+
throw new \ReflectionException(sprintf('Class %s does not exist', $class));
69+
};
70+
spl_autoload_register($throwingAutoloader);
71+
6972
$this->typeMap = array();
7073
foreach ($this->container->getDefinitions() as $id => $definition) {
7174
$this->populateAvailableType($id, $definition);
7275
}
76+
77+
spl_autoload_unregister($throwingAutoloader);
7378
}
7479

7580
/**
@@ -85,7 +90,7 @@ private function populateAvailableType($id, Definition $definition)
8590
return;
8691
}
8792

88-
if (false === ($class = $this->getClass($definition))) {
93+
if (null === ($class = $this->getClass($definition))) {
8994
return;
9095
}
9196

@@ -112,19 +117,19 @@ private function populateAvailableType($id, Definition $definition)
112117
*
113118
* @param Definition $definition
114119
*
115-
* @return string|false
120+
* @return string|null
116121
*/
117122
private function getClass(Definition $definition)
118123
{
119124
// Cannot use reflection if the class isn't set
120125
if (!$class = $definition->getClass()) {
121-
return false;
126+
return;
122127
}
123128

124129
// Normalize the class name (`\Foo` -> `Foo`)
125130
$class = $this->container->getParameterBag()->resolveValue($class);
126-
if (isset(self::$classesName[$class])) {
127-
return self::$classesName[$class];
131+
if (isset(self::$classNames[$class])) {
132+
return self::$classNames[$class] ?: null;
128133
}
129134

130135
try {
@@ -133,6 +138,6 @@ private function getClass(Definition $definition)
133138
$name = false;
134139
}
135140

136-
return self::$classesName[$class] = $name;
141+
return self::$classNames[$class] = $name ?: null;
137142
}
138143
}

0 commit comments

Comments
 (0)