1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #263 - add support for @psalm-param and @psalm-var annotations

This commit is contained in:
Matthew Brown 2017-11-02 21:45:17 -04:00
parent b163c296ff
commit a651fad6f0
2 changed files with 49 additions and 4 deletions

View File

@ -44,13 +44,16 @@ class CommentChecker
$comments = self::parseDocComment($comment, $var_line_number);
if (!isset($comments['specials']['var'])) {
if (!isset($comments['specials']['var']) && !isset($comments['specials']['psalm-var'])) {
return;
}
if ($comments) {
$all_vars = (isset($comments['specials']['var']) ? $comments['specials']['var'] : [])
+ (isset($comments['specials']['psalm-var']) ? $comments['specials']['psalm-var'] : []);
/** @var int $line_number */
foreach ($comments['specials']['var'] as $line_number => $var_line) {
foreach ($all_vars as $line_number => $var_line) {
$var_line = trim($var_line);
if (!$var_line) {
@ -165,9 +168,12 @@ class CommentChecker
}
}
if (isset($comments['specials']['param'])) {
if (isset($comments['specials']['param']) || isset($comments['specials']['psalm-param'])) {
$all_params = (isset($comments['specials']['param']) ? $comments['specials']['param'] : [])
+ (isset($comments['specials']['psalm-param']) ? $comments['specials']['psalm-param'] : []);
/** @var string $param */
foreach ($comments['specials']['param'] as $line_number => $param) {
foreach ($all_params as $line_number => $param) {
try {
$line_parts = self::splitDocLine($param);
} catch (DocblockParseException $e) {

View File

@ -234,6 +234,32 @@ class AnnotationTest extends TestCase
}
}',
],
'psalmVar' => [
'<?php
class A
{
/** @psalm-var array<int, string> */
public $foo = [];
public function updateFoo() : void {
$this->foo[5] = "hello";
}
}',
],
'psalmParam' => [
'<?php
function takesInt(int $a) : void {}
/**
* @psalm-param array<int, string> $a
* @param string[] $a
*/
function foo(array $a) : void {
foreach ($a as $key => $value) {
takesInt($key);
}
}',
],
];
}
@ -490,6 +516,19 @@ class AnnotationTest extends TestCase
. ' could not infer',
'error_levels' => ['MixedArgument', 'InvalidReturnType', 'MixedAssignment'],
],
'psalmInvalidVar' => [
'<?php
class A
{
/** @psalm-var array<int, string> */
public $foo = [];
public function updateFoo() : void {
$this->foo["boof"] = "hello";
}
}',
'error_message' => 'InvalidPropertyAssignment',
],
];
}
}