Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add argument `&$asGhostObject` to LazyProxy's `DumperInterface` to allow using ghost objects for lazy loading services
* Add `enum` env var processor
* Add `shuffle` env var processor

6.1
---
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function getProvidedTypes(): array
'trim' => 'string',
'require' => 'bool|int|float|string|array',
'enum' => \BackedEnum::class,
'shuffle' => 'array',
];
}

Expand Down Expand Up @@ -206,6 +207,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
return null;
}

if ('shuffle' === $prefix) {
\is_array($env) ? shuffle($env) : throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is "abusing" ternary, the more "classical"

            if (!\is_array($env)) {
                throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
            }

            shuffle($env);

would be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subjective

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, hence "IMHO" 😉 but I can try "more objective" arguments:

  • it's not using the expression result (BTW: shuffle() always returns true [historically could also return false], and if designed today would probably be : void; and throw wasn't usable as/in an expression before PHP 8.0)
  • it's inconsistent with all the rest of the file (even the general codebase)

Dunno if the CS tool could flag it?


return $env;
}

if (!\is_scalar($env)) {
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,23 @@ public function testGetEnvCsv($value, $processed)
$this->assertSame($processed, $result);
}

public function testGetEnvShuffle()
{
mt_srand(2);

$this->assertSame(
['bar', 'foo'],
(new EnvVarProcessor(new Container()))->getEnv('shuffle', '', fn () => ['foo', 'bar']),
);
}

public function testGetEnvShuffleInvalid()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Env var "foo" cannot be shuffled, expected array, got "string".');
(new EnvVarProcessor(new Container()))->getEnv('shuffle', 'foo', fn () => 'bar');
}

public function validCsv()
{
$complex = <<<'CSV'
Expand Down