diff --git a/Dotenv.php b/Dotenv.php index 46cf3cefa9b41b0c412f9b8bd47d14796fe29c03..89337b00d0bf8d291386de74a122c6837ee9695c 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -41,7 +41,7 @@ final class Dotenv { * @param \Composer\IO\IOInterface $io * The input-output object of the composer session. */ - public function __construct($name, IOInterface $io) { + public function __construct(string $name, IOInterface $io) { $this->name = strtoupper($name); $this->io = $io; $this->dotenv = new SymfonyDotenv(); @@ -67,7 +67,7 @@ final class Dotenv { * @return string * The value. */ - public function receive($key, $prompt, $default = '') { + public function receive(string $key, string $prompt, $default = ''): string { return $this->receiveGlobal($this->name . '_' . $key, $prompt, $default); } @@ -84,14 +84,14 @@ final class Dotenv { * @return string * The value. */ - public function receiveGlobal($key, $prompt, $default = '') { + public function receiveGlobal(string $key, string $prompt, $default = ''): string { $key = strtoupper($key); - $value = $_ENV[$key] ?? FALSE; - if ($value === FALSE) { + $value = $_ENV[$key] ?? NULL; + if ($value === NULL) { if ($this->io->isInteractive()) { $value = $this->io->ask($prompt . ': '); } - if ($value === FALSE) { + if ($value === NULL) { $value = $default; } $this->put($key, $value, TRUE); @@ -116,7 +116,7 @@ final class Dotenv { * The value of the environment variable. * @param bool $overwrite */ - public function put($key, $value, $overwrite = FALSE) { + public function put(string $key, string $value, $overwrite = FALSE): void { $data = $this->parse(); $key = strtoupper($key); if (!$overwrite && isset($data[$key])) { @@ -143,7 +143,7 @@ final class Dotenv { * @return array * The same data with environment variables being replaced. */ - public function replaceEnvironmentVariables($data) { + public function replaceEnvironmentVariables($data): array { array_walk_recursive($data, array($this, 'findEnvironmentVariables')); return $data; } @@ -154,8 +154,8 @@ final class Dotenv { * @param string $item * @param string $key */ - private function findEnvironmentVariables(&$item, &$key) { - $pattern = '@\{\$env:([A-Za-z0-9_]*)\}@i'; + private function findEnvironmentVariables(string &$item, string &$key): void { + $pattern = '@\{\$env:(\w)\}@i'; preg_match_all($pattern, $item, $item_matches); preg_match_all($pattern, $key, $key_matches); @@ -175,10 +175,10 @@ final class Dotenv { * * @return string */ - private function replaceItemEnvironmentVariables($item, $matches) { + private function replaceItemEnvironmentVariables(string $item, array $matches): string { foreach ($matches as $var) { - $value = $_ENV[$var] ?? FALSE; - if (!$value) { + $value = $_ENV[$var] ?? NULL; + if ($value === NULL) { $value = ''; } $item = str_replace( '{$env:' . $var . '}', $value, $item ); @@ -191,7 +191,7 @@ final class Dotenv { * * @return array */ - private function parse() { + private function parse(): array { $envContent = file_exists('.env') ? file_get_contents('.env') : ''; return $this->dotenv->parse($envContent); } @@ -199,7 +199,7 @@ final class Dotenv { /** * Load the local .env file. */ - private function load() { + private function load(): void { try { foreach ($this->parse() as $key => $value) { unset($_SERVER[$key]); diff --git a/composer.json b/composer.json index 5f7374ca917ed03769b4223e9e0bdfe292851fbc..4d259e88c8dd1a3dbd046f61ad117eff5a5049e4 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require": { "php": ">=7.2", - "lakedrops/composer-json-utils": "^1.4.1", + "lakedrops/composer-json-utils": "^1.7||dev-master", "symfony/dotenv": "^3.4||^4.4||^5.0" }, "require-dev": {