Skip to content

Commit

Permalink
fix: now initEnvValue() converts type when the default property value…
Browse files Browse the repository at this point in the history
… is int and float
  • Loading branch information
kenjis committed Mar 6, 2022
1 parent f969513 commit fe778b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
22 changes: 17 additions & 5 deletions system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct()
*
* @param mixed $property
*
* @return mixed
* @return void
*/
protected function initEnvValue(&$property, string $name, string $prefix, string $shortPrefix)
{
Expand All @@ -102,16 +102,28 @@ protected function initEnvValue(&$property, string $name, string $prefix, string
} elseif ($value === 'true') {
$value = true;
}
$property = is_bool($value) ? $value : trim($value, '\'"');
}
if (is_bool($value)) {
$property = $value;

return;
}

$value = trim($value, '\'"');

return $property;
if (is_int($property)) {
$value = (int) $value;
} elseif (is_float($property)) {
$value = (float) $value;
}

$property = $value;
}
}

/**
* Retrieve an environment-specific configuration setting
*
* @return mixed
* @return string|null
*/
protected function getEnvValue(string $property, string $prefix, string $shortPrefix)
{
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public function testBasicValues()
$this->assertSame(18, $config->golf);
}

public function testUseDefaultValueTypeIntAndFloatValues()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();

$this->assertSame(0.0, $config->float);
$this->assertSame(999, $config->int);
}

public function testUseDefaultValueTypeStringValue()
{
$dotenv = new DotEnv($this->fixturesFolder, '.env');
$dotenv->load();
$config = new SimpleConfig();

$this->assertSame('123456', $config->password);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
Expand Down
5 changes: 5 additions & 0 deletions tests/system/Config/fixtures/.env
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ SimpleConfig.crew.captain = Malcolm
SimpleConfig.crew.pilot = Wash
SimpleConfig.crew.comms = true
SimpleConfig.crew.doctor = false

SimpleConfig.float = '0.0'
SimpleConfig.int = '999'

SimpleConfig.password = 123456
3 changes: 3 additions & 0 deletions tests/system/Config/fixtures/SimpleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ class SimpleConfig extends \CodeIgniter\Config\BaseConfig
public $one_deep = [
'under_deep' => null,
];
public $float = 12.34;
public $int = 1234;
public $password = 'secret';
}

0 comments on commit fe778b9

Please sign in to comment.