Skip to content

Commit

Permalink
Added type information to help phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Jul 14, 2020
1 parent 8e2176f commit 7da90f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 0 additions & 4 deletions phpstan.src.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ parameters:
- '#Variable \$defaults might not be defined.#'
- '/^Parameter \#1 \$readers of class Dotenv\\Repository\\[a-zA-Z]*Repository[a-zA-Z]* constructor expects/'
- '/^Parameter \#2 \$writers of class Dotenv\\Repository\\[a-zA-Z]*Repository[a-zA-Z]* constructor expects/'
- '#Unable to resolve the template type S in call to method PhpOption\\Option<mixed>::flatMap\(\)#'
- '#Method Dotenv\\Loader\\Parser::processChar\(\) should return Dotenv\\Result\\Result<array.+, string> but returns Dotenv\\Result\\Result<array.+, mixed>.#'
- '#Parameter \#1 \$callable of method PhpOption\\Option<Dotenv\\Loader\\Value\|null>::map\(\) expects callable\(Dotenv\\Loader\\Value\|null\): mixed, Closure\(Dotenv\\Loader\\Value\): mixed given.#'
- '#Parameter \#1 \$callable of method PhpOption\\Option<array\|null>::[a-z]+\(\) expects callable\(array\|null\): [a-z]+, Closure\(array\): mixed given.#'
- '#Parameter \#1 \$name of method Dotenv\\Repository\\AbstractRepository::set\(\) expects string, string\|null given.#'
- '#Parameter \#.+ of method Dotenv\\Repository\\AbstractRepository::offset.+ should be contravariant .+#'
6 changes: 5 additions & 1 deletion src/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ private function processEntries(RepositoryInterface $repository, array $entries)
*/
private static function resolveNestedVariables(RepositoryInterface $repository, Value $value = null)
{
return Option::fromValue($value)
/** @var Option<Value> */
$option = Option::fromValue($value);

return $option
->map(function (Value $v) use ($repository) {
/** @var string */
return array_reduce($v->getVars(), function ($s, $i) use ($repository) {
return substr($s, 0, $i).self::resolveNestedVariable($repository, substr($s, $i));
}, $v->getChars());
Expand Down
23 changes: 23 additions & 0 deletions src/Loader/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,61 +138,84 @@ private static function processChar($state, $char)
switch ($state) {
case self::INITIAL_STATE:
if ($char === '\'') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::SINGLE_QUOTED_STATE]);
} elseif ($char === '"') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::DOUBLE_QUOTED_STATE]);
} elseif ($char === '#') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::COMMENT_STATE]);
} elseif ($char === '$') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, true, self::UNQUOTED_STATE]);
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::UNQUOTED_STATE]);
}
case self::UNQUOTED_STATE:
if ($char === '#') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::COMMENT_STATE]);
} elseif (ctype_space($char)) {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::WHITESPACE_STATE]);
} elseif ($char === '$') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, true, self::UNQUOTED_STATE]);
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::UNQUOTED_STATE]);
}
case self::SINGLE_QUOTED_STATE:
if ($char === '\'') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::WHITESPACE_STATE]);
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::SINGLE_QUOTED_STATE]);
}
case self::DOUBLE_QUOTED_STATE:
if ($char === '"') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::WHITESPACE_STATE]);
} elseif ($char === '\\') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::ESCAPE_SEQUENCE_STATE]);
} elseif ($char === '$') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, true, self::DOUBLE_QUOTED_STATE]);
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::DOUBLE_QUOTED_STATE]);
}
case self::ESCAPE_SEQUENCE_STATE:
if ($char === '"' || $char === '\\') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::DOUBLE_QUOTED_STATE]);
} elseif ($char === '$') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([$char, false, self::DOUBLE_QUOTED_STATE]);
} elseif (in_array($char, ['f', 'n', 'r', 't', 'v'], true)) {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create([stripcslashes('\\'.$char), false, self::DOUBLE_QUOTED_STATE]);
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Error::create('an unexpected escape sequence');
}
case self::WHITESPACE_STATE:
if ($char === '#') {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::COMMENT_STATE]);
} elseif (!ctype_space($char)) {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Error::create('unexpected whitespace');
} else {
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::WHITESPACE_STATE]);
}
case self::COMMENT_STATE:
/** @var \Dotenv\Result\Result<array{string,bool,int},string> */
return Success::create(['', false, self::COMMENT_STATE]);
default:
throw new RuntimeException('Parser entered invalid state.');
Expand Down
1 change: 1 addition & 0 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static function replaceCallback($pattern, callable $callback, $subject, $
public static function split($pattern, $subject)
{
return self::pregAndWrap(function ($subject) use ($pattern) {
/** @var string[] */
return (array) @preg_split($pattern, $subject);
}, $subject);
}
Expand Down

0 comments on commit 7da90f2

Please sign in to comment.