fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -27,7 +27,7 @@
"require-dev": {
"ext-filter": "*",
"bamarni/composer-bin-plugin": "^1.4.1",
"phpunit/phpunit": "^7.5.20 || ^8.5.21 || ^9.5.10"
"phpunit/phpunit": "^7.5.20 || ^8.5.30 || ^9.5.25"
},
"autoload": {
"psr-4": {
@@ -43,11 +43,18 @@
"ext-filter": "Required to use the boolean validator."
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true
},
"preferred-install": "dist"
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": true
},
"branch-alias": {
"dev-master": "5.4-dev"
"dev-master": "5.5-dev"
}
}
}

View File

@@ -74,6 +74,7 @@ final class EntryParser
})->getOrElse([$line, null]);
if ($result[0] === '') {
/** @var \GrahamCampbell\ResultType\Result<array{string,string|null},string> */
return Error::create(self::getErrorMessage('an unexpected equals', $line));
}
@@ -102,9 +103,11 @@ final class EntryParser
}
if (!self::isValidName($name)) {
/** @var \GrahamCampbell\ResultType\Result<string,string> */
return Error::create(self::getErrorMessage('an invalid name', $name));
}
/** @var \GrahamCampbell\ResultType\Result<string,string> */
return Success::create($name);
}
@@ -136,7 +139,7 @@ final class EntryParser
*/
private static function isValidName(string $name)
{
return Regex::matches('~\A[a-zA-Z0-9_.]+\z~', $name)->success()->getOrElse(false);
return Regex::matches('~(*UTF8)\A[\p{Ll}\p{Lu}\p{M}\p{N}_.]+\z~', $name)->success()->getOrElse(false);
}
/**
@@ -154,6 +157,7 @@ final class EntryParser
private static function parseValue(string $value)
{
if (\trim($value) === '') {
/** @var \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Value,string> */
return Success::create(Value::blank());
}
@@ -164,10 +168,13 @@ final class EntryParser
});
});
}, Success::create([Value::blank(), self::INITIAL_STATE]))->flatMap(static function (array $result) {
/** @psalm-suppress DocblockTypeContradiction */
if (in_array($result[1], self::REJECT_STATES, true)) {
/** @var \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Value,string> */
return Error::create('a missing closing quote');
}
/** @var \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Value,string> */
return Success::create($result[0]);
})->mapError(static function (string $err) use ($value) {
return self::getErrorMessage($err, $value);

View File

@@ -58,15 +58,17 @@ final class Lines
*/
private static function multilineProcess(bool $multiline, string $line, array $buffer)
{
$startsOnCurrentLine = $multiline ? false : self::looksLikeMultilineStart($line);
// check if $line can be multiline variable
if ($started = self::looksLikeMultilineStart($line)) {
if ($startsOnCurrentLine) {
$multiline = true;
}
if ($multiline) {
\array_push($buffer, $line);
if (self::looksLikeMultilineStop($line, $started)) {
if (self::looksLikeMultilineStop($line, $startsOnCurrentLine)) {
$multiline = false;
$line = \implode("\n", $buffer);
$buffer = [];
@@ -104,7 +106,7 @@ final class Lines
return true;
}
return Regex::occurences('/(?=([^\\\\]"))/', \str_replace('\\\\', '', $line))->map(static function (int $count) use ($started) {
return Regex::occurrences('/(?=([^\\\\]"))/', \str_replace('\\\\', '', $line))->map(static function (int $count) use ($started) {
return $started ? $count > 1 : $count >= 1;
})->success()->getOrElse(false);
}

View File

@@ -44,6 +44,7 @@ final class Parser implements ParserInterface
return \array_reduce($entries, static function (Result $result, string $raw) {
return $result->flatMap(static function (array $entries) use ($raw) {
return EntryParser::parse($raw)->map(static function (Entry $entry) use ($entries) {
/** @var \Dotenv\Parser\Entry[] */
return \array_merge($entries, [$entry]);
});
});

View File

@@ -50,7 +50,7 @@ final class ApacheAdapter implements AdapterInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
@@ -65,8 +65,8 @@ final class ApacheAdapter implements AdapterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -78,7 +78,7 @@ final class ApacheAdapter implements AdapterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -40,7 +40,7 @@ final class ArrayAdapter implements AdapterInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
@@ -52,8 +52,8 @@ final class ArrayAdapter implements AdapterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -67,7 +67,7 @@ final class ArrayAdapter implements AdapterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -33,7 +33,7 @@ final class EnvConstAdapter implements AdapterInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
@@ -41,6 +41,9 @@ final class EnvConstAdapter implements AdapterInterface
{
/** @var \PhpOption\Option<string> */
return Option::fromArraysValue($_ENV, $name)
->filter(static function ($value) {
return \is_scalar($value);
})
->map(static function ($value) {
if ($value === false) {
return 'false';
@@ -50,17 +53,16 @@ final class EnvConstAdapter implements AdapterInterface
return 'true';
}
return $value;
})->filter(static function ($value) {
return \is_string($value);
/** @psalm-suppress PossiblyInvalidCast */
return (string) $value;
});
}
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -74,7 +76,7 @@ final class EnvConstAdapter implements AdapterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -37,8 +37,8 @@ final class GuardedWriter implements WriterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -56,7 +56,7 @@ final class GuardedWriter implements WriterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/
@@ -74,7 +74,7 @@ final class GuardedWriter implements WriterInterface
/**
* Determine if the given variable is allowed.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -45,8 +45,8 @@ final class ImmutableWriter implements WriterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -72,7 +72,7 @@ final class ImmutableWriter implements WriterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/
@@ -99,7 +99,7 @@ final class ImmutableWriter implements WriterInterface
*
* That is, is it an "existing" variable.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -30,7 +30,7 @@ final class MultiReader implements ReaderInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/

View File

@@ -28,8 +28,8 @@ final class MultiWriter implements WriterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -47,7 +47,7 @@ final class MultiWriter implements WriterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -48,7 +48,7 @@ final class PutenvAdapter implements AdapterInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
@@ -63,8 +63,8 @@ final class PutenvAdapter implements AdapterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -78,7 +78,7 @@ final class PutenvAdapter implements AdapterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -9,7 +9,7 @@ interface ReaderInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/

View File

@@ -45,8 +45,8 @@ final class ReplacingWriter implements WriterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -63,7 +63,7 @@ final class ReplacingWriter implements WriterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/
@@ -83,7 +83,7 @@ final class ReplacingWriter implements WriterInterface
* Returns true if it currently exists, or existed at any point in the past
* that we are aware of.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -33,7 +33,7 @@ final class ServerConstAdapter implements AdapterInterface
/**
* Read an environment variable, if it exists.
*
* @param string $name
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
@@ -41,6 +41,9 @@ final class ServerConstAdapter implements AdapterInterface
{
/** @var \PhpOption\Option<string> */
return Option::fromArraysValue($_SERVER, $name)
->filter(static function ($value) {
return \is_scalar($value);
})
->map(static function ($value) {
if ($value === false) {
return 'false';
@@ -50,17 +53,16 @@ final class ServerConstAdapter implements AdapterInterface
return 'true';
}
return $value;
})->filter(static function ($value) {
return \is_string($value);
/** @psalm-suppress PossiblyInvalidCast */
return (string) $value;
});
}
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -74,7 +76,7 @@ final class ServerConstAdapter implements AdapterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -9,8 +9,8 @@ interface WriterInterface
/**
* Write to an environment variable, if possible.
*
* @param string $name
* @param string $value
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
@@ -19,7 +19,7 @@ interface WriterInterface
/**
* Delete an environment variable, if possible.
*
* @param string $name
* @param non-empty-string $name
*
* @return bool
*/

View File

@@ -6,6 +6,7 @@ namespace Dotenv\Repository;
use Dotenv\Repository\Adapter\ReaderInterface;
use Dotenv\Repository\Adapter\WriterInterface;
use InvalidArgumentException;
final class AdapterRepository implements RepositoryInterface
{
@@ -46,7 +47,7 @@ final class AdapterRepository implements RepositoryInterface
*/
public function has(string $name)
{
return $this->reader->read($name)->isDefined();
return '' !== $name && $this->reader->read($name)->isDefined();
}
/**
@@ -54,10 +55,16 @@ final class AdapterRepository implements RepositoryInterface
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get(string $name)
{
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->reader->read($name)->getOrElse(null);
}
@@ -67,10 +74,16 @@ final class AdapterRepository implements RepositoryInterface
* @param string $name
* @param string $value
*
* @throws \InvalidArgumentException
*
* @return bool
*/
public function set(string $name, string $value)
{
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->writer->write($name, $value);
}
@@ -79,10 +92,16 @@ final class AdapterRepository implements RepositoryInterface
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return bool
*/
public function clear(string $name)
{
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->writer->delete($name);
}
}

View File

@@ -111,7 +111,7 @@ final class RepositoryBuilder
}
/**
* Determine if the given name if of an adapaterclass.
* Determine if the given name if of an adapterclass.
*
* @param string $name
*

View File

@@ -20,6 +20,8 @@ interface RepositoryInterface
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get(string $name);
@@ -30,6 +32,8 @@ interface RepositoryInterface
* @param string $name
* @param string $value
*
* @throws \InvalidArgumentException
*
* @return bool
*/
public function set(string $name, string $value);
@@ -39,6 +43,8 @@ interface RepositoryInterface
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return bool
*/
public function clear(string $name);

View File

@@ -47,7 +47,7 @@ final class Regex
*
* @return \GrahamCampbell\ResultType\Result<int,string>
*/
public static function occurences(string $pattern, string $subject)
public static function occurrences(string $pattern, string $subject)
{
return self::pregAndWrap(static function (string $subject) use ($pattern) {
return (int) @\preg_match_all($pattern, $subject);
@@ -102,9 +102,11 @@ final class Regex
$result = $operation($subject);
if (\preg_last_error() !== \PREG_NO_ERROR) {
/** @var \GrahamCampbell\ResultType\Result<V,string> */
return Error::create(\preg_last_error_msg());
}
/** @var \GrahamCampbell\ResultType\Result<V,string> */
return Success::create($result);
}
}