Skip to content

Commit

Permalink
Three ... dots are deprecated, triggers notices
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 14, 2021
1 parent b58b33f commit f3462df
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/DI/Extensions/DecoratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function beforeCompile()
$info->tags[InjectExtension::TAG_INJECT] = $info->inject;
}

$this->addSetups($type, Nette\DI\Helpers::filterArguments($info->setup));
$this->addTags($type, Nette\DI\Helpers::filterArguments($info->tags));
$this->addSetups($type, Nette\DI\Helpers::filterArguments($info->setup, 'decorator'));
$this->addTags($type, Nette\DI\Helpers::filterArguments($info->tags, 'decorator'));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DI/Extensions/ParametersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function loadConfiguration()

foreach ($this->dynamicParams as $key) {
$params[$key] = array_key_exists($key, $params)
? new DynamicParameter($generator->formatPhp('($this->parameters[?] \?\? ?)', $resolver->completeArguments(Nette\DI\Helpers::filterArguments([$key, $params[$key]]))))
? new DynamicParameter($generator->formatPhp('($this->parameters[?] \?\? ?)', $resolver->completeArguments(Nette\DI\Helpers::filterArguments([$key, $params[$key]], 'parameters'))))
: new DynamicParameter((new Nette\PhpGenerator\Dumper)->format('$this->parameters[?]', $key));
}

Expand Down
14 changes: 8 additions & 6 deletions src/DI/Extensions/ServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ private function loadDefinition(?string $name, \stdClass $config): void
*/
private function updateServiceDefinition(Definitions\ServiceDefinition $definition, \stdClass $config): void
{
$desc = $definition->getDescriptor();
if ($config->create) {
$definition->setFactory(Helpers::filterArguments([$config->create])[0]);
$definition->setFactory(Helpers::filterArguments([$config->create], $desc)[0]);
$definition->setType(null);
}

Expand All @@ -95,7 +96,7 @@ private function updateServiceDefinition(Definitions\ServiceDefinition $definiti
}

if ($config->arguments) {
$arguments = Helpers::filterArguments($config->arguments);
$arguments = Helpers::filterArguments($config->arguments, $desc);
if (empty($config->reset['arguments']) && !Nette\Utils\Arrays::isList($arguments)) {
$arguments = array_replace($definition->getFactory()->arguments, $arguments);
}
Expand All @@ -108,7 +109,7 @@ private function updateServiceDefinition(Definitions\ServiceDefinition $definiti
$definition->setSetup([]);
}

foreach (Helpers::filterArguments($config->setup) as $id => $setup) {
foreach (Helpers::filterArguments($config->setup, $desc) as $id => $setup) {
if (is_array($setup)) {
$setup = new Statement(key($setup), array_values($setup));
}
Expand Down Expand Up @@ -138,22 +139,23 @@ private function updateAccessorDefinition(Definitions\AccessorDefinition $defini
private function updateFactoryDefinition(Definitions\FactoryDefinition $definition, \stdClass $config): void
{
$resultDef = $definition->getResultDefinition();
$desc = $definition->getDescriptor();

if (isset($config->implement)) {
$definition->setImplement($config->implement);
$definition->setAutowired(true);
}

if ($config->create) {
$resultDef->setFactory(Helpers::filterArguments([$config->create])[0]);
$resultDef->setFactory(Helpers::filterArguments([$config->create], $desc)[0]);
}

if ($config->type) {
$resultDef->setFactory($config->type);
}

if ($config->arguments) {
$arguments = Helpers::filterArguments($config->arguments);
$arguments = Helpers::filterArguments($config->arguments, $desc);
if (empty($config->reset['arguments']) && !Nette\Utils\Arrays::isList($arguments)) {
$arguments = array_replace($resultDef->getFactory()->arguments, $arguments);
}
Expand All @@ -166,7 +168,7 @@ private function updateFactoryDefinition(Definitions\FactoryDefinition $definiti
$resultDef->setSetup([]);
}

foreach (Helpers::filterArguments($config->setup) as $id => $setup) {
foreach (Helpers::filterArguments($config->setup, $desc) as $id => $setup) {
if (is_array($setup)) {
$setup = new Statement(key($setup), array_values($setup));
}
Expand Down
9 changes: 5 additions & 4 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ public static function escape($value)
/**
* Removes ... and process constants recursively.
*/
public static function filterArguments(array $args): array
public static function filterArguments(array $args, string $hint = ''): array
{
foreach ($args as $k => $v) {
if ($v === '...') {
trigger_error("[$hint] Replace ... with _ in configuration file.", E_USER_DEPRECATED);
unset($args[$k]);
} elseif (
PHP_VERSION_ID >= 80100
Expand All @@ -156,10 +157,10 @@ public static function filterArguments(array $args): array
} elseif (is_string($v) && preg_match('#^@[\w\\\\]+$#D', $v)) {
$args[$k] = new Reference(substr($v, 1));
} elseif (is_array($v)) {
$args[$k] = self::filterArguments($v);
$args[$k] = self::filterArguments($v, $hint);
} elseif ($v instanceof Statement) {
[$tmp] = self::filterArguments([$v->getEntity()]);
$args[$k] = new Statement($tmp, self::filterArguments($v->arguments));
[$tmp] = self::filterArguments([$v->getEntity()], $hint);
$args[$k] = new Statement($tmp, self::filterArguments($v->arguments, $hint));
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.services.autowiring.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ services:
factory: Factory()::createModel
setup:
# local methods
- test(...)
- test(_)
- @model::test()
- @self::test()
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/Helpers.filterArguments.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Assert::same([], Helpers::filterArguments([]));

Assert::same(
['a', 'b', 3 => ['c'], [1 => 'd']],
Helpers::filterArguments(['a', 'b', '...', ['c', '...'], ['...', 'd']])
@Helpers::filterArguments(['a', 'b', '...', ['c', '...'], ['...', 'd']]) // ... is deprecated
);

Assert::same(
Expand All @@ -33,5 +33,5 @@ Assert::equal(

Assert::equal(
[new Statement('class', ['a', 2 => Nette\DI\ContainerBuilder::THIS_CONTAINER])],
Helpers::filterArguments([new Statement('class', ['a', '...', 'Nette\DI\ContainerBuilder::THIS_CONTAINER'])])
@Helpers::filterArguments([new Statement('class', ['a', '...', 'Nette\DI\ContainerBuilder::THIS_CONTAINER'])]) // ... is deprecated
);

0 comments on commit f3462df

Please sign in to comment.