1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #2148 - support arrays properly

This commit is contained in:
Matthew Brown 2019-09-21 21:50:11 -04:00
parent 264131c211
commit 0ae04c9743
3 changed files with 43 additions and 21 deletions

View File

@ -618,15 +618,15 @@ class CommentAnalyzer
if (isset($parsed_docblock['specials']['psalm-assert'])) {
foreach ($parsed_docblock['specials']['psalm-assert'] 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->assertions[] = [
'type' => $assertion_parts[0],
'param_name' => substr($assertion_parts[1], 1),
'type' => $line_parts[0],
'param_name' => substr($line_parts[1], 1),
];
}
}

View File

@ -587,7 +587,13 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
}
try {
$new_type_part = Atomic::create($assertion, null, $template_type_map);
if (strpos($assertion, '<') || strpos($assertion, '[')) {
$new_type_union = Type::parseString($assertion);
$new_type_part = array_values($new_type_union->getTypes());
} else {
$new_type_part = Atomic::create($assertion, null, $template_type_map);
}
} catch (\Psalm\Exception\TypeParseTreeException $e) {
$new_type_part = new TMixed();

View File

@ -718,6 +718,38 @@ class AssertTest extends TestCase
return count() > 0;
}',
],
'parseAssertion' => [
'<?php
/**
* @psalm-assert array<string, string[]> $data
* @param mixed $data
*/
function isArrayOfStrings($data): void {}
function foo(array $arr) : void {
isArrayOfStrings($arr);
foreach ($arr as $a) {
foreach ($a as $b) {
echo $b;
}
}
}'
],
'noExceptionOnShortArrayAssertion' => [
'<?php
/**
* @param mixed[] $a
*/
function one(array $a): void {
isInts($a);
}
/**
* @psalm-assert int[] $value
* @param mixed $value
*/
function isInts($value): void {}',
],
];
}
@ -888,22 +920,6 @@ class AssertTest extends TestCase
}',
'error_message' => 'TypeDoesNotContainType',
],
'noExceptionOnMalformedAssertion' => [
'<?php
/**
* @param mixed[] $a
*/
function one(array $a): void {
isInts($a);
}
/**
* @psalm-assert int[] $value
* @param mixed $value
*/
function isInts($value): void {}',
'error_message' => 'InvalidDocblock',
],
];
}
}