Skip to content

Commit

Permalink
minor #17900 [DependencyInjection] Simplified code in AutowirePass (h…
Browse files Browse the repository at this point in the history
…ason)

This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Simplified code in AutowirePass

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

ce0357e [DependencyInjection] Simplified code in AutowirePass
  • Loading branch information
fabpot committed Feb 26, 2016
2 parents 1f33be2 + ce0357e commit cc9f6b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
Expand Up @@ -148,45 +148,17 @@ private function populateAvailableType($id, Definition $definition)
$this->types[$type] = $id;
}

// Cannot use reflection if the class isn't set
if (!$definition->getClass()) {
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
return;
}

if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
$this->extractInterfaces($id, $reflectionClass);
$this->extractAncestors($id, $reflectionClass);
}
}

/**
* Extracts the list of all interfaces implemented by a class.
*
* @param string $id
* @param \ReflectionClass $reflectionClass
*/
private function extractInterfaces($id, \ReflectionClass $reflectionClass)
{
foreach ($reflectionClass->getInterfaces() as $interfaceName => $reflectionInterface) {
$this->set($interfaceName, $id);

$this->extractInterfaces($id, $reflectionInterface);
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
$this->set($reflectionInterface->name, $id);
}
}

/**
* Extracts all inherited types of a class.
*
* @param string $id
* @param \ReflectionClass $reflectionClass
*/
private function extractAncestors($id, \ReflectionClass $reflectionClass)
{
$this->set($reflectionClass->name, $id);

if ($reflectionParentClass = $reflectionClass->getParentClass()) {
$this->extractAncestors($id, $reflectionParentClass);
}
do {
$this->set($reflectionClass->name, $id);
} while ($reflectionClass = $reflectionClass->getParentClass());
}

/**
Expand Down Expand Up @@ -256,6 +228,7 @@ private function getReflectionClass($id, Definition $definition)
return $this->reflectionClasses[$id];
}

// Cannot use reflection if the class isn't set
if (!$class = $definition->getClass()) {
return;
}
Expand Down
Expand Up @@ -61,9 +61,10 @@ public function testProcessAutowireInterface()
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(2, $container->getDefinition('g')->getArguments());
$this->assertCount(3, $container->getDefinition('g')->getArguments());
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(0));
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(1));
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(2));
}

public function testCompleteExistingDefinition()
Expand Down Expand Up @@ -317,13 +318,21 @@ interface EInterface extends DInterface
{
}

class F implements EInterface
interface IInterface
{
}

class I implements IInterface
{
}

class F extends I implements EInterface
{
}

class G
{
public function __construct(DInterface $d, EInterface $e)
public function __construct(DInterface $d, EInterface $e, IInterface $i)
{
}
}
Expand Down

0 comments on commit cc9f6b0

Please sign in to comment.