Skip to content

Commit

Permalink
reduce typecasting - keep ints as ints
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Nelson committed Dec 12, 2019
1 parent e670202 commit cfec396
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/ParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function replaceSurroundingToken(string $parameter, string $token, cal
{
$oldParameter = $parameter;

while (mb_substr_count($parameter, $token) > 0) {
while (is_string($parameter) && mb_substr_count($parameter, $token) > 0) {
$pos1 = mb_strpos($parameter, $token);
$pos2 = mb_strpos($parameter, $token, $pos1 + 1);

Expand All @@ -126,22 +126,25 @@ protected function replaceSurroundingToken(string $parameter, string $token, cal
if ($pos2 === false) {
throw new ConfigException("An uneven number of '{$token}' token bindings exists for '{$oldParameter}'");
}

$value = mb_substr($parameter, $pos1 + 1, $pos2 - ($pos1 + 1));
$newValue = $callable($value);
if (!is_string($newValue) && !is_numeric($newValue)) {
if (mb_strlen($value) + 2 === mb_strlen($parameter)) {
return $newValue;
}

// If it took up the entire parameter
if (($pos1 === 0 && ($pos2 + 1) === mb_strlen($parameter))) {
$parameter = $newValue;
continue;
}

if (!is_string($newValue) && !is_numeric($newValue)) {
throw new ConfigException(
"Parameter '{$value}' as part of '{$oldParameter}' resolved to a non-string. This is only permissible if the parameter attempts no interpolation"
);
}

$parameter = mb_substr($parameter, 0, $pos1) . $newValue . mb_substr($parameter, $pos2 + 1);
}

return str_replace(self::ESCAPED_TOKEN, $token, $parameter);
return is_string($parameter) ? str_replace(self::ESCAPED_TOKEN, $token, $parameter) : $parameter;
}

public function getResolvedConstants() : array
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/Basic/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function testBasic()
$this->assertEquals("MyService1", $container["my_service"]->getFirstArgument());
$this->assertInstanceOf(ExampleClass::class, $container["my_service_2"]);
$this->assertEquals("MyService2", $container["my_service_2"]->getFirstArgument());
$arguments = $container["my_service_2"]->getArguments();

self::assertSame("potatosalad", $arguments[3]);
}

public function testBoolAndInt()
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/Basic/file1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- "%my_parameter_2%"
- "%my_parameter_3%"
- "%my_parameter_5%"
- "%my_parameter_7%%my_parameter_8%"

parameters:
my_parameter_2: "MyService2"
Expand All @@ -19,3 +20,6 @@ parameters:

my_parameter_5: "%my_parameter_6%"
my_parameter_6: 5003

my_parameter_7: "potato"
my_parameter_8: "salad"

0 comments on commit cfec396

Please sign in to comment.