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

Allow @psalm-property and @psalm-method

This commit is contained in:
bugreportuser 2019-02-22 19:42:36 -06:00 committed by Matthew Brown
parent 34b333cf7f
commit 68a135f434
4 changed files with 57 additions and 3 deletions

View File

@ -128,6 +128,7 @@ class DocComment
$special_key, $special_key,
[ [
'return', 'param', 'template', 'var', 'type', 'return', 'param', 'template', 'var', 'type',
'property', 'method',
'assert', 'assert-if-true', 'assert-if-false', 'suppress', 'assert', 'assert-if-true', 'assert-if-false', 'suppress',
'ignore-nullable-return', 'override-property-visibility', 'ignore-nullable-return', 'override-property-visibility',
'override-method-visibility', 'seal-properties', 'seal-methods', 'override-method-visibility', 'seal-properties', 'seal-methods',

View File

@ -613,8 +613,11 @@ class CommentAnalyzer
} }
} }
if (isset($comments['specials']['method'])) { if (isset($comments['specials']['method']) || isset($comments['specials']['psalm-method'])) {
foreach ($comments['specials']['method'] as $method_entry) { $all_methods = (isset($comments['specials']['method']) ? $comments['specials']['method'] : [])
+ (isset($comments['specials']['psalm-method']) ? $comments['specials']['psalm-method'] : []);
foreach ($all_methods as $method_entry) {
$method_entry = preg_replace('/[ \t]+/', ' ', trim($method_entry)); $method_entry = preg_replace('/[ \t]+/', ' ', trim($method_entry));
$docblock_lines = []; $docblock_lines = [];
@ -714,6 +717,7 @@ class CommentAnalyzer
} }
self::addMagicPropertyToInfo($info, $comments['specials'], 'property'); self::addMagicPropertyToInfo($info, $comments['specials'], 'property');
self::addMagicPropertyToInfo($info, $comments['specials'], 'psalm-property');
self::addMagicPropertyToInfo($info, $comments['specials'], 'property-read'); self::addMagicPropertyToInfo($info, $comments['specials'], 'property-read');
self::addMagicPropertyToInfo($info, $comments['specials'], 'property-write'); self::addMagicPropertyToInfo($info, $comments['specials'], 'property-write');
@ -723,7 +727,7 @@ class CommentAnalyzer
/** /**
* @param ClassLikeDocblockComment $info * @param ClassLikeDocblockComment $info
* @param array<string, array<int, string>> $specials * @param array<string, array<int, string>> $specials
* @param string $property_tag ('property', 'property-read', or 'property-write') * @param string $property_tag ('property', 'psalm-property', 'property-read', or 'property-write')
* *
* @throws DocblockParseException * @throws DocblockParseException
* *

View File

@ -386,6 +386,26 @@ class MagicMethodAnnotationTest extends TestCase
'$d' => 'D', '$d' => 'D',
] ]
], ],
'validSimplePsalmAnnotations' => [
'<?php
class ParentClass {
public function __call(string $name, array $args) {}
}
/**
* @psalm-method string getString() dsa sada
* @psalm-method void setInteger(int $integer) dsa sada
*/
class Child extends ParentClass {}
$child = new Child();
$a = $child->getString();
$child->setInteger(4);',
'assertions' => [
'$a' => 'string',
],
],
]; ];
} }

View File

@ -405,6 +405,35 @@ class MagicPropertyTest extends TestCase
$o->foo = "hello"; $o->foo = "hello";
}', }',
], ],
'psalmPropertyDocblock' => [
'<?php
namespace Bar;
/**
* @psalm-property string $foo
*/
class A {
/** @param string $name */
public function __get($name): ?string {
if ($name === "foo") {
return "hello";
}
return null;
}
/**
* @param string $name
* @param mixed $value
*/
public function __set($name, $value): void {
}
}
$a = new A();
$a->foo = "hello";
$a->bar = "hello"; // not a property',
],
]; ];
} }