diff --git a/composer.json b/composer.json index 8585f01..8a75aa8 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": ">=7.0" + "php": ">=7.1" }, "autoload": { "psr-4": { diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index 6a587d2..28b60e4 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace NeonXP\Dotenv\Compiler; use NeonXP\Dotenv\Exception\RuntimeException; -use NeonXP\Dotenv\Types\KeyValue; /** * Class Compiler @@ -20,61 +19,64 @@ class Compiler implements CompilerInterface const REGEX_VARIABLE = '/\$\{(.+?)\}/'; /** - * @var KeyValue[] + * @var array[] */ protected $collection = []; /** - * @var KeyValue[] + * @var array[] */ protected $cache = []; /** * @inheritdoc - * @param KeyValue[] $collection + * @param array[] $collection */ public function setRawCollection(array $collection): void { $this->collection = []; $this->cache = []; - foreach ($collection as $keyValue) { - $this->collection[$keyValue->getKey()] = $keyValue; + foreach ($collection as $array) { + $this->collection[$array['key']] = $array; } } /** * @inheritdoc - * @param KeyValue $keyValue - * @return KeyValue + * @param array $array + * @return array */ - public function compileKeyValue(KeyValue $keyValue): KeyValue + public function compile(array $array): array { - $newValue = preg_replace_callback(self::REGEX_VARIABLE, function ($variable) use ($keyValue) { + $newValue = preg_replace_callback(self::REGEX_VARIABLE, function ($variable) use ($array) { $variable = $variable[1]; - if ($variable === $keyValue->getKey()) { + if ($variable === $array['key']) { throw new RuntimeException('Self referencing'); } if (isset($this->cache[$variable])) { - return $this->cache[$variable]->getValue(); + return $this->cache[$variable]['value']; } elseif (isset($this->collection[$variable]) && !$this->needToCompile($this->collection[$variable])) { - return $this->collection[$variable]->getValue(); + return $this->collection[$variable]['value']; } elseif (isset($this->collection[$variable]) && $this->needToCompile($this->collection[$variable])) { - return $this->compileKeyValue($this->collection[$variable])->getValue(); + return $this->compile($this->collection[$variable])['value']; } return "UNKNOWN VARIABLE {$variable}"; - }, $keyValue->getValue()); - $result = new KeyValue($keyValue->getKey(), $newValue); - $this->cache[$result->getKey()] = $result; + }, $array['value']); + $result = [ + 'key' => $array['key'], + 'value' => $newValue + ]; + $this->cache[$result['key']] = $result; return $result; } /** - * @param KeyValue $keyValue + * @param array $array * @return bool */ - protected function needToCompile(KeyValue $keyValue): bool + protected function needToCompile(array $array): bool { - return !!preg_match(self::REGEX_VARIABLE, $keyValue->getValue()); + return !!preg_match(self::REGEX_VARIABLE, $array['value']); } } \ No newline at end of file diff --git a/src/Compiler/CompilerInterface.php b/src/Compiler/CompilerInterface.php index 15b6823..22daad7 100644 --- a/src/Compiler/CompilerInterface.php +++ b/src/Compiler/CompilerInterface.php @@ -8,8 +8,6 @@ declare(strict_types=1); namespace NeonXP\Dotenv\Compiler; -use NeonXP\Dotenv\Types\KeyValue; - /** * Interface CompilerInterface * @package NeonXP\Dotenv\Compiler @@ -17,13 +15,13 @@ use NeonXP\Dotenv\Types\KeyValue; interface CompilerInterface { /** - * @param KeyValue[] $collection + * @param array[] $collection */ public function setRawCollection(array $collection): void; /** - * @param KeyValue $keyValue - * @return KeyValue + * @param array $array + * @return array */ - public function compileKeyValue(KeyValue $keyValue): KeyValue; + public function compile(array $array): array; } \ No newline at end of file diff --git a/src/Dotenv.php b/src/Dotenv.php index 3f6eb90..95dda02 100644 --- a/src/Dotenv.php +++ b/src/Dotenv.php @@ -15,7 +15,6 @@ use NeonXP\Dotenv\Loader\FileLoader; use NeonXP\Dotenv\Loader\LoaderInterface; use NeonXP\Dotenv\Parser\Parser; use NeonXP\Dotenv\Parser\ParserInterface; -use NeonXP\Dotenv\Types\KeyValue; /** * Class Dotenv @@ -75,9 +74,9 @@ class Dotenv implements \ArrayAccess, \IteratorAggregate $rawData = array_map([$this->parser, 'parseLine'], $lines); $this->compiler->setRawCollection($rawData); $this->loadedValues = array_reduce( - array_map([$this->compiler, 'compileKeyValue'], $rawData), - function (array $acc, KeyValue $current) { - $acc[$current->getKey()] = $current->getValue(); + array_map([$this->compiler, 'compile'], $rawData), + function (array $acc, $current) { + $acc[$current['key']] = $current['value']; return $acc; }, [] diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index 7a5c37b..36410fc 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -8,8 +8,6 @@ declare(strict_types=1); namespace NeonXP\Dotenv\Parser; -use NeonXP\Dotenv\Types\KeyValue; - /** * Class Parser * @package NeonXP\Dotenv\Parser @@ -28,7 +26,7 @@ class Parser implements ParserInterface const BOOLEAN = '/^(true|false)$/i'; const NUMBER = '/^(\d+)$/'; - public function parseLine(string $line): KeyValue + public function parseLine(string $line): array { $line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line); list($key, $value) = explode('=', $line, 2) + ['', '']; @@ -50,6 +48,6 @@ class Parser implements ParserInterface $value = intval($value); } - return new KeyValue($key, $value); + return ['key' => $key, 'value' => $value]; } } \ No newline at end of file diff --git a/src/Parser/ParserInterface.php b/src/Parser/ParserInterface.php index f67ffb5..4e63b33 100644 --- a/src/Parser/ParserInterface.php +++ b/src/Parser/ParserInterface.php @@ -8,8 +8,6 @@ declare(strict_types=1); namespace NeonXP\Dotenv\Parser; -use NeonXP\Dotenv\Types\KeyValue; - /** * Interface ParserInterface * @package NeonXP\Dotenv\Parser @@ -18,7 +16,7 @@ interface ParserInterface { /** * @param string $line - * @return KeyValue + * @return array */ - public function parseLine(string $line): KeyValue; + public function parseLine(string $line): array; } \ No newline at end of file diff --git a/src/Types/KeyValue.php b/src/Types/KeyValue.php deleted file mode 100644 index 54b0360..0000000 --- a/src/Types/KeyValue.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @license: MIT - */ - -namespace NeonXP\Dotenv\Types; - -/** - * Class KeyValue - * @package NeonXP\Dotenv\Types - */ -class KeyValue -{ - /** - * @var string - */ - private $key; - - /** - * @var string - */ - private $value; - - /** - * KeyValue constructor. - * @param string $key - * @param mixed $value - */ - public function __construct(string $key, $value) - { - $this->key = $key; - $this->value = $value; - } - - /** - * @return string - */ - public function getKey(): string - { - return $this->key; - } - - /** - * @return mixed - */ - public function getValue() - { - return $this->value; - } -} \ No newline at end of file diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 9a820ec..ed9e757 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -6,7 +6,6 @@ declare(strict_types=1); * @license: MIT */ -use NeonXP\Dotenv\Types\KeyValue; use PHPUnit\Framework\TestCase; /** @@ -29,16 +28,16 @@ class CompilerTest extends TestCase 'KEY4' => 'Test VALUE1 VALUE3 => VALUE3', ]; $compiler = new \NeonXP\Dotenv\Compiler\Compiler(); - $collectionOfKeyValues = []; + $collectionOfarrays = []; foreach ($collection as $key => $value) { - $collectionOfKeyValues[] = new KeyValue($key, $value); + $collectionOfarrays[] = ['key' => $key, 'value' => $value]; } - $compiler->setRawCollection($collectionOfKeyValues); + $compiler->setRawCollection($collectionOfarrays); foreach ($tests as $key => $expected) { - $result = $compiler->compileKeyValue(new KeyValue($key, $collection[$key])); - $this->assertEquals($key, $result->getKey()); - $this->assertEquals($expected, $result->getValue()); + $result = $compiler->compile(['key' => $key, 'value' => $collection[$key]]); + $this->assertEquals($key, $result['key']); + $this->assertEquals($expected, $result['value']); } } } \ No newline at end of file diff --git a/tests/DotenvTest.php b/tests/DotenvTest.php index 59dd8b0..757ce70 100644 --- a/tests/DotenvTest.php +++ b/tests/DotenvTest.php @@ -11,7 +11,6 @@ use NeonXP\Dotenv\Dotenv; use NeonXP\Dotenv\Exception\RuntimeException; use NeonXP\Dotenv\Loader\LoaderInterface; use NeonXP\Dotenv\Parser\ParserInterface; - use PHPUnit\Framework\TestCase; /** @@ -45,12 +44,8 @@ class DotenvTest extends TestCase { $dotenv = new Dotenv($this->mockLoader, $this->mockParser, $this->mockCompiler); - try { - $dotenv->get('TEST1'); - $this->assertTrue(false, 'Dotenv must throws exception if it not loaded'); - } catch (RuntimeException $exception) { - $this->assertTrue(true, 'Dotenv must throws exception if it not loaded'); - } + $this->expectException(RuntimeException::class); + $dotenv->get('TEST1'); $dotenv->load(); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 9f13261..c67056b 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); * @author: Alexander Kiryukhin * @license: MIT */ + use NeonXP\Dotenv\Parser\Parser; use PHPUnit\Framework\TestCase; @@ -30,8 +31,8 @@ class ParserTest extends TestCase foreach ($tests as $test => $expected) { $result = $parser->parseLine($test); - $this->assertEquals($expected[0], $result->getKey()); - $this->assertEquals($expected[1], $result->getValue()); + $this->assertEquals($expected[0], $result['key']); + $this->assertEquals($expected[1], $result['value']); } } } \ No newline at end of file diff --git a/tests/mocks/MockCompiler.php b/tests/mocks/MockCompiler.php index 89db68f..fced5a2 100644 --- a/tests/mocks/MockCompiler.php +++ b/tests/mocks/MockCompiler.php @@ -7,7 +7,6 @@ declare(strict_types=1); */ use NeonXP\Dotenv\Compiler\CompilerInterface; -use NeonXP\Dotenv\Types\KeyValue; /** * Class MockCompiler @@ -16,7 +15,7 @@ class MockCompiler implements CompilerInterface { /** - * @param KeyValue[] $collection + * @param array[] $collection */ function setRawCollection(array $collection): void { @@ -24,11 +23,11 @@ class MockCompiler implements CompilerInterface } /** - * @param KeyValue $keyValue - * @return KeyValue + * @param array $array + * @return array */ - function compileKeyValue(KeyValue $keyValue): KeyValue + function compile(array $array): array { - return $keyValue; + return $array; } } \ No newline at end of file diff --git a/tests/mocks/MockParser.php b/tests/mocks/MockParser.php index 39c369c..d327f9f 100644 --- a/tests/mocks/MockParser.php +++ b/tests/mocks/MockParser.php @@ -7,7 +7,6 @@ declare(strict_types=1); */ use NeonXP\Dotenv\Parser\ParserInterface; -use NeonXP\Dotenv\Types\KeyValue; /** * Class MockParser @@ -17,12 +16,12 @@ class MockParser implements ParserInterface /** * @param string $line - * @return KeyValue + * @return array */ - public function parseLine(string $line): KeyValue + public function parseLine(string $line): array { list($key, $value) = explode("=", $line); - return new KeyValue($key, $value); + return ['key' => $key, 'value' => $value]; } } \ No newline at end of file