Skip to content

Commit d0015c2

Browse files
committed
bug #62482 [DependencyInjection] Fix loose validation in #[Autowire] attribute (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Fix loose validation in `#[Autowire]` attribute | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT The `Autowire` attribute uses a chain of `xor` conditions to ensure that only one of `$value`, `$service`, `$expression`, `$env`, or `$param` is passed. However, `xor` is pairwise associative. If **three** of these arguments are passed, the expression evaluates to `true` (`(true ^ true) ^ true === true`), and the validation passes incorrectly. Commits ------- f0a2f56 [DependencyInjection] Fix loose validation in #[Autowire] attribute
2 parents 765e80e + f0a2f56 commit d0015c2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/DependencyInjection/Attribute/Autowire.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
if (null !== $value && null !== $service) {
5353
throw new LogicException('#[Autowire] attribute cannot declare $value and $service at the same time.');
5454
}
55-
} elseif (!(null !== $value xor null !== $service xor null !== $expression xor null !== $env xor null !== $param)) {
55+
} elseif (1 !== (null !== $value) + (null !== $service) + (null !== $expression) + (null !== $env) + (null !== $param)) {
5656
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, $env, $param or $value.');
5757
}
5858

src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,24 @@ public static function provideMultipleParameters(): iterable
8282

8383
yield [['value' => 'some-value', 'expression' => 'expr']];
8484
}
85+
86+
/**
87+
* @dataProvider provideMutuallyExclusiveOptions
88+
*/
89+
public function testConstructThrowsOnMutuallyExclusiveOptions(array $parameters)
90+
{
91+
$this->expectException(LogicException::class);
92+
$this->expectExceptionMessage('#[Autowire] attribute must declare exactly one of $service, $expression, $env, $param or $value.');
93+
94+
new Autowire(...$parameters);
95+
}
96+
97+
public static function provideMutuallyExclusiveOptions(): iterable
98+
{
99+
yield [[]];
100+
yield [['value' => 'some-value', 'service' => 'id']];
101+
yield [['value' => 'some-value', 'service' => 'id', 'expression' => 'expr']];
102+
yield [['value' => 'some-value', 'service' => 'id', 'expression' => 'expr', 'env' => 'ENV']];
103+
yield [['value' => 'some-value', 'service' => 'id', 'expression' => 'expr', 'env' => 'ENV', 'param' => 'param']];
104+
}
85105
}

0 commit comments

Comments
 (0)