Skip to content

Commit

Permalink
Fix #16 - add support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed May 20, 2018
1 parent 7bc4262 commit 6250c2a
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 67 deletions.
52 changes: 42 additions & 10 deletions src/Psalm/Checker/CommentChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function getTypeFromComment(
) {
$var_id = null;

$var_type_string = null;
$var_type_tokens = null;
$original_type = null;

$var_comments = [];
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function getTypeFromComment(
}

try {
$var_type_string = Type::fixUpLocalType(
$var_type_tokens = Type::fixUpLocalType(
$line_parts[0],
$aliases,
$template_types
Expand All @@ -89,16 +89,16 @@ public static function getTypeFromComment(
}
}

if (!$var_type_string || !$original_type) {
if (!$var_type_tokens || !$original_type) {
continue;
}

try {
$defined_type = Type::parseString($var_type_string, false, $template_types ?: []);
$defined_type = Type::parseTokens($var_type_tokens, false, $template_types ?: []);
} catch (TypeParseTreeException $e) {
if (is_int($came_from_line_number)) {
throw new DocblockParseException(
$var_type_string .
implode('', $var_type_tokens) .
' is not a valid type' .
' (from ' .
$source->getCheckedFilePath() .
Expand All @@ -108,7 +108,7 @@ public static function getTypeFromComment(
);
}

throw new DocblockParseException($var_type_string . ' is not a valid type');
throw new DocblockParseException(implode('', $var_type_tokens) . ' is not a valid type');
}

$defined_type->setFromDocblock();
Expand Down Expand Up @@ -197,12 +197,9 @@ public static function extractFunctionDocblockInfo($comment, $line_number)
}

if (count($line_parts) > 1) {
if (preg_match('/^' . self::TYPE_REGEX . '$/', $line_parts[0])
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& !strpos($line_parts[0], '::')
&& $line_parts[0][0] !== '{'
&& !in_array($line_parts[0], ['null', 'false', 'true'], true)
) {
if ($line_parts[1][0] === '&') {
$line_parts[1] = substr($line_parts[1], 1);
Expand Down Expand Up @@ -438,10 +435,45 @@ public static function splitDocLine($return_block)

$return_block = preg_replace('/[ \t]+/', ' ', $return_block);

$quote_char = null;
$escaped = false;

for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
$char = $return_block[$i];
$next_char = $i < $l - 1 ? $return_block[$i + 1] : null;

if ($quote_char) {
if ($char === $quote_char && $i > 1 && !$escaped) {
$quote_char = null;

$type .= $char;

continue;
}

if ($char === '\\' && !$escaped && ($next_char === $quote_char || $next_char === '\\')) {
$escaped = true;

$type .= $char;

continue;
}

$escaped = false;

$type .= $char;

continue;
}

if ($char === '"' || $char === '\'') {
$quote_char = $char;

$type .= $char;

continue;
}

if ($char === '[' || $char === '{' || $char === '(' || $char === '<') {
$brackets .= $char;
} elseif ($char === ']' || $char === '}' || $char === ')' || $char === '>') {
Expand Down
6 changes: 5 additions & 1 deletion src/Psalm/Checker/TypeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,11 @@ public static function isAtomicContainedBy(
}

if ($input_type_part instanceof Scalar) {
if ($container_type_part instanceof Scalar) {
if ($container_type_part instanceof Scalar
&& !$container_type_part instanceof TLiteralInt
&& !$container_type_part instanceof TLiteralString
&& !$container_type_part instanceof TLiteralFloat
) {
$has_scalar_match = true;
}
} elseif ($container_type_part instanceof TObject &&
Expand Down
Loading

0 comments on commit 6250c2a

Please sign in to comment.