Skip to content

Commit

Permalink
Don't inject promoted properties
Browse files Browse the repository at this point in the history
  • Loading branch information
apeschar committed Jun 16, 2023
1 parent 3366ab6 commit ce5ce3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/Definition/Source/AttributeBasedAutowiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ public function getDefinitions() : array
private function readProperties(ReflectionClass $class, ObjectDefinition $definition) : void
{
foreach ($class->getProperties() as $property) {
if ($property->isStatic()) {
continue;
}
$this->readProperty($property, $definition);
}

// Read also the *private* properties of the parent classes
/** @noinspection PhpAssignmentInConditionInspection */
while ($class = $class->getParentClass()) {
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE) as $property) {
if ($property->isStatic()) {
continue;
}
$this->readProperty($property, $definition, $class->getName());
}
}
Expand All @@ -100,6 +94,10 @@ private function readProperties(ReflectionClass $class, ObjectDefinition $defini
*/
private function readProperty(ReflectionProperty $property, ObjectDefinition $definition, string $classname = null) : void
{
if ($property->isStatic() || $property->isPromoted()) {
return;
}

// Look for #[Inject] attribute
try {
$attribute = $property->getAttributes(Inject::class)[0] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use DI\Test\UnitTest\Definition\Source\Fixtures\AnnotationFixtureTypedProperties;
use DI\Test\UnitTest\Definition\Source\Fixtures\AnnotationInjectableFixture;
use DI\Test\UnitTest\Definition\Source\Fixtures\AttributeFixture;
use DI\Test\UnitTest\Definition\Source\Fixtures\AttributeFixturePromotedProperty;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -242,6 +243,19 @@ public function testReadParentPrivateProperties()
$this->assertHasPropertyInjection($definition, 'propertyParentPrivate');
}

public function testPromotedProperties(): void
{
$definition = (new AttributeBasedAutowiring)->autowire(AttributeFixturePromotedProperty::class);
$this->assertNotHasPropertyInjection($definition, 'promotedProperty');

$constructorInjection = $definition->getConstructorInjection();
$this->assertInstanceOf(MethodInjection::class, $constructorInjection);

$parameters = $constructorInjection->getParameters();
$this->assertCount(1, $parameters);
$this->assertEquals(new Reference('foo'), $parameters[0]);
}

private function getMethodInjection(ObjectDefinition $definition, $name) : ?MethodInjection
{
$methodInjections = $definition->getMethodInjections();
Expand Down Expand Up @@ -279,7 +293,7 @@ private function assertNotHasPropertyInjection(ObjectDefinition $definition, $pr
$propertyInjections = $definition->getPropertyInjections();
foreach ($propertyInjections as $propertyInjection) {
if ($propertyInjection->getPropertyName() === $propertyName) {
$this->fail('No property injection found for ' . $propertyName);
$this->fail('Unexpected property injection found for ' . $propertyName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace DI\Test\UnitTest\Definition\Source\Fixtures;

use DI\Attribute\Inject;

class AttributeFixturePromotedProperty
{
public function __construct(#[Inject("foo")] public $promotedProperty) {
}
}

0 comments on commit ce5ce3d

Please sign in to comment.