Skip to content
Permalink
Browse files

Fix #2484 - add support for spaces in assert-if-true types

  • Loading branch information
muglug committed Dec 19, 2019
1 parent cde7c80 commit 15cd62d33204b5999b2d07cc1a82f0fb8be26f4a
Showing with 31 additions and 8 deletions.
  1. +8 −8 src/Psalm/Internal/Analyzer/CommentAnalyzer.php
  2. +23 −0 tests/AssertAnnotationTest.php
@@ -611,30 +611,30 @@ public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $commen

if (isset($parsed_docblock['specials']['psalm-assert-if-true'])) {
foreach ($parsed_docblock['specials']['psalm-assert-if-true'] as $assertion) {
$assertion_parts = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $assertion));
$line_parts = self::splitDocLine($assertion);

if (count($assertion_parts) < 2 || $assertion_parts[1][0] !== '$') {
if (count($line_parts) < 2 || $line_parts[1][0] !== '$') {
throw new IncorrectDocblockException('Misplaced variable');
}

$info->if_true_assertions[] = [
'type' => $assertion_parts[0],
'param_name' => substr($assertion_parts[1], 1),
'type' => $line_parts[0],
'param_name' => substr($line_parts[1], 1),
];
}
}

if (isset($parsed_docblock['specials']['psalm-assert-if-false'])) {
foreach ($parsed_docblock['specials']['psalm-assert-if-false'] as $assertion) {
$assertion_parts = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $assertion));
$line_parts = self::splitDocLine($assertion);

if (count($assertion_parts) < 2 || $assertion_parts[1][0] !== '$') {
if (count($line_parts) < 2 || $line_parts[1][0] !== '$') {
throw new IncorrectDocblockException('Misplaced variable');
}

$info->if_false_assertions[] = [
'type' => $assertion_parts[0],
'param_name' => substr($assertion_parts[1], 1),
'type' => $line_parts[0],
'param_name' => substr($line_parts[1], 1),
];
}
}
@@ -707,6 +707,29 @@ function gimmeAString(?string $v): string {
return $v;
}',
],
'assertIfTrueWithSpace' => [
'<?php
/**
* @param mixed $data
* @return bool
* @psalm-assert-if-true array{type: string} $data
*/
function isBar($data) {
return isset($data["type"]);
}
/**
* @param mixed $data
* @return string
*/
function doBar($data) {
if (isBar($data)) {
return $data["type"];
}
throw new \Exception();
}'
],
];
}

0 comments on commit 15cd62d

Please sign in to comment.
You can’t perform that action at this time.