8000 [DI] fix definition and usage of AbstractArgument · symfony/symfony@03682ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 03682ba

Browse files
[DI] fix definition and usage of AbstractArgument
1 parent 69b6c90 commit 03682ba

12 files changed

+41
-33
lines changed

src/Symfony/Component/DependencyInjection/Argument/AbstractArgument.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616
*/
1717
final class AbstractArgument
1818
{
19-
private $serviceId;
20-
private $argKey;
2119
private $text;
20+
private $context;
2221

23-
public function __construct(string $serviceId, string $argKey, string $text = '')
22+
public function __construct(string $text = '')
2423
{
25-
$this->serviceId = $serviceId;
26-
$this->argKey = $argKey;
27-
$this->text = $text;
24+
$this->text = trim($text, '. ');
2825
}
2926

30-
public function getServiceId(): string
27+
public function setContext(string $context): void
3128
{
32-
return $this->serviceId;
29+
$this->context = $context.' is abstract'.('' === $this->text ? '' : ': ');
3330
}
3431

35-
public function getArgumentKey(): string
32+
public function getText(): string
3633
{
37-
return $this->argKey;
34+
return $this->text;
3835
}
3936

40-
public function getText(): string
37+
public function getTextWithContext(): string
4138
{
42-
return $this->text;
39+
return $this->context.$this->text.'.';
4340
}
4441
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
1415
use Symfony\Component\DependencyInjection\Definition;
1516
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
@@ -28,6 +29,10 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
2829
*/
2930
protected function processValue($value, bool $isRoot = false)
3031
{
32+
if ($value instanceof AbstractArgument && $value->getText().'.' === $value->getTextWithContext()) {
33+
$value->setContext(sprintf('A value found in service "%s"', $this->currentId));
34+
}
35+
3136
if (!$value instanceof Definition) {
3237
return parent::processValue($value, $isRoot);
3338
}
@@ -41,6 +46,10 @@ protected function processValue($value, bool $isRoot = false)
4146
$resolvedArguments = [];
4247

4348
foreach ($arguments as $key => $argument) {
49+
if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
50+
$argument->setContext(sprintf('__construct' === $method ? 'Argument "%s" of service "%s"' : 'Argument "%s" of method call "%s::%s()"', $key, $this->currentId, $method));
51+
}
52+
4453
if (\is_int($key)) {
4554
$resolvedArguments[$key] = $argument;
4655
continue;
@@ -107,6 +116,12 @@ protected function processValue($value, bool $isRoot = false)
107116
$value->setMethodCalls($calls);
108117
}
109118

119+
foreach ($value->getProperties() as $key => $argument) {
120+
if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
121+
$argument->setContext(sprintf('Property "%s" of service "%s"', $key, $this->currentId));
122+
}
123+
}
124+
110125
return parent::processValue($value, $isRoot);
111126
}
112127
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ private function doResolveServices($value, array &$inlineServices = [], bool $is
12191219
} elseif ($value instanceof Expression) {
12201220
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this]);
12211221
} elseif ($value instanceof AbstractArgument) {
1222-
throw new RuntimeException(sprintf('Argument "%s" of service "%s" is abstract%s, did you forget to define it?', $value->getArgumentKey(), $value->getServiceId(), $value->getText() ? ' ('.$value->getText().')' : ''));
1222+
throw new RuntimeException($value->getTextWithContext());
12231223
}
12241224

12251225
return $value;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ private function dumpValue($value, bool $interpolate = true): string
17871787
return $code;
17881788
}
17891789
} elseif ($value instanceof AbstractArgument) {
1790-
throw new RuntimeException(sprintf('Argument "%s" of service "%s" is abstract%s, did you forget to define it?', $value->getArgumentKey(), $value->getServiceId(), $value->getText() ? ' ('.$value->getText().')' : ''));
1790+
throw new RuntimeException($value->getTextWithContext());
17911791
} elseif (\is_object($value) || \is_resource($value)) {
17921792
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
17931793
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ private function convertParameters(array $parameters, string $type, \DOMElement
320320
$text = $this->document->createTextNode(self::phpToXml(base64_encode($value)));
321321
$element->appendChild($text);
322322
} elseif ($value instanceof AbstractArgument) {
323-
$argKey = $value->getArgumentKey();
324-
if (!is_numeric($argKey)) {
325-
$element->setAttribute('key', $argKey);
326-
}
327323
$element->setAttribute('type', 'abstract');
328324
$text = $this->document->createTextNode(self::phpToXml($value->getText()));
329325
$element->appendChild($text);

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
537537
$arguments[$key] = $value;
538538
break;
539539
case 'abstract':
540-
$serviceId = $node->getAttribute('id');
541-
$arguments[$key] = new AbstractArgument($serviceId, (string) $key, $arg->nodeVal F987 ue);
540+
$arguments[$key] = new AbstractArgument($arg->nodeValue);
542541
break;
543542
case 'string':
544543
$arguments[$key] = $arg->nodeValue;

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa
449449
}
450450

451451
if (isset($service['arguments'])) {
452-
$definition->setArguments($this->resolveServices($service['arguments'], $file, false, $id));
452+
$definition->setArguments($this->resolveServices($service['arguments'], $file));
453453
}
454454

455455
if (isset($service['properties'])) {
@@ -721,7 +721,7 @@ private function validate($content, string $file): ?array
721721
*
722722
* @return array|string|Reference|ArgumentInterface
723723
*/
724-
private function resolveServices($value, string $file, bool $isParameter = false, string $serviceId = '', string $argKey = '')
724+
private function resolveServices($value, string $file, bool $isParameter = false)
725725
{
726726
if ($value instanceof TaggedValue) {
727727
$argument = $value->getValue();
@@ -795,15 +795,15 @@ private function resolveServices($value, string $file, bool $isParameter = false
795795
return new Reference($id);
796796
}
797797
if ('abstract' === $value->getTag()) {
798-
return new AbstractArgument($serviceId, $argKey, $value->getValue());
798+
return new AbstractArgument($value->getValue());
799799
}
800800

801801
throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
802802
}
803803

804804
if (\is_array($value)) {
805805
foreach ($value as $k => $v) {
806-
$value[$k] = $this->resolveServices($v, $file, $isParameter, $serviceId, $k);
806+
$value[$k] = $this->resolveServices($v, $file, $isParameter);
807807
}
808808
} elseif (\is_string($value) && 0 === strpos($value, '@=')) {
809809
if (!class_exists(Expression::class)) {

src/Symfony/Component/DependencyInjection/Tests/Argument/AbstractArgumentTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class AbstractArgumentTest extends TestCase
1818
{
1919
public function testAbstractArgumentGetters()
2020
{
21-
$argument = new AbstractArgument('foo', '$bar', 'should be defined by Pass');
22-
$this->assertSame('foo', $argument->getServiceId());
23-
$this->assertSame('$bar', $argument->getArgumentKey());
21+
$argument = new AbstractArgument('should be defined by Pass');
2422
$this->assertSame('should be defined by Pass', $argument->getText());
2523
}
2624
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,14 @@ public function testCreateServiceWithExpression()
552552
public function testCreateServiceWithAbstractArgument()
553553
{
554554
$this->expectException(RuntimeException::class);
555-
$this->expectExceptionMessage('Argument "$baz" of service "foo" is abstract (should be defined by Pass), did you forget to define it?');
555+
$this->expectExceptionMessage('Argument "$baz" of service "foo" is abstract: should be defined by Pass.');
556556

557557
$builder = new ContainerBuilder();
558558
$builder->register('foo', FooWithAbstractArgument::class)
559-
->addArgument(new AbstractArgument('foo', '$baz', 'should be defined by Pass'));
559+
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
560+
->setPublic(true);
561+
562+
$builder->compile();
560563

561564
$builder->get('foo');
562565
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,12 @@ public function testMultipleDeprecatedAliasesWorking()
13721372
public function testDumpServiceWithAbstractArgument()
13731373
{
13741374
$this->expectException(RuntimeException::class);
1375-
$this->expectExceptionMessage('Argument "$baz" of service "Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument" is abstract (should be defined by Pass), did you forget to define it?');
1375+
$this->expectExceptionMessage('Argument "$baz" of service "Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument" is abstract: should be defined by Pass.');
13761376

13771377
$container = new ContainerBuilder();
13781378

13791379
$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
1380-
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
1380+
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
13811381
->setArgument('$bar', 'test')
13821382
->setPublic(true);
13831383

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function testDumpServiceWithAbstractArgument()
264264
{
265265
$container = new ContainerBuilder();
266266
$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
267-
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
267+
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
268268
->setArgument('$bar', 'test');
269269

270270
$dumper = new XmlDumper($container);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testDumpServiceWithAbstractArgument()
123123
{
124124
$container = new ContainerBuilder();
125125
$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
126-
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
126+
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
127127
->setArgument('$bar', 'test');
128128

129129
$dumper = new YamlDumper($container);

0 commit comments

Comments
 (0)
0