Skip to content

Commit 21e8667

Browse files
authored
Support for @phpstan-property @phpstan-property-read and @phpstan-property-write
1 parent 0900192 commit 21e8667

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,43 @@ public function resolvePropertyTags(PhpDocNode $phpDocNode, NameScope $nameScope
9797
{
9898
$resolved = [];
9999

100-
foreach ($phpDocNode->getPropertyTagValues() as $tagValue) {
101-
$propertyName = substr($tagValue->propertyName, 1);
102-
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
103-
104-
$resolved[$propertyName] = new PropertyTag(
105-
$propertyType,
106-
true,
107-
true
108-
);
100+
foreach (['@property', '@phpstan-property'] as $tagName) {
101+
foreach ($phpDocNode->getPropertyTagValues($tagName) as $tagValue) {
102+
$propertyName = substr($tagValue->propertyName, 1);
103+
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
104+
105+
$resolved[$propertyName] = new PropertyTag(
106+
$propertyType,
107+
true,
108+
true
109+
);
110+
}
109111
}
110112

111-
foreach ($phpDocNode->getPropertyReadTagValues() as $tagValue) {
112-
$propertyName = substr($tagValue->propertyName, 1);
113-
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
113+
foreach (['@property-read', '@phpstan-property-read'] as $tagName) {
114+
foreach ($phpDocNode->getPropertyReadTagValues($tagName) as $tagValue) {
115+
$propertyName = substr($tagValue->propertyName, 1);
116+
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
114117

115-
$resolved[$propertyName] = new PropertyTag(
116-
$propertyType,
117-
true,
118-
false
119-
);
118+
$resolved[$propertyName] = new PropertyTag(
119+
$propertyType,
120+
true,
121+
false
122+
);
123+
}
120124
}
121125

122-
foreach ($phpDocNode->getPropertyWriteTagValues() as $tagValue) {
123-
$propertyName = substr($tagValue->propertyName, 1);
124-
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
126+
foreach (['@property-write', '@phpstan-property-write'] as $tagName) {
127+
foreach ($phpDocNode->getPropertyWriteTagValues($tagName) as $tagValue) {
128+
$propertyName = substr($tagValue->propertyName, 1);
129+
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
125130

126-
$resolved[$propertyName] = new PropertyTag(
127-
$propertyType,
128-
false,
129-
true
130-
);
131+
$resolved[$propertyName] = new PropertyTag(
132+
$propertyType,
133+
false,
134+
true
135+
);
136+
}
131137
}
132138

133139
return $resolved;

src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class InvalidPHPStanDocTagRule implements \PHPStan\Rules\Rule
3333
'@phpstan-impure',
3434
'@phpstan-type',
3535
'@phpstan-import-type',
36+
'@phpstan-property',
37+
'@phpstan-property-read',
38+
'@phpstan-property-write',
3639
];
3740

3841
private Lexer $phpDocLexer;

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ public function dataFileAsserts(): iterable
564564
}
565565

566566
yield from $this->gatherAssertTypes(__DIR__ . '/data/filesystem-functions.php');
567+
568+
yield from $this->gatherAssertTypes(__DIR__ . '/data/classPhpDocs-phpstanPropertyPrefix.php');
567569
}
568570

569571
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ClassPhpDocsNamespace;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @property string $base
9+
*
10+
* @property string $foo
11+
* @phpstan-property int $foo
12+
*
13+
* @property-read string $bar
14+
* @phpstan-property-read int $bar
15+
*
16+
* @property-write string $baz
17+
* @phpstan-property-write int $baz
18+
*/
19+
class PhpstanProperties
20+
{
21+
public function doFoo()
22+
{
23+
assertType('string', $this->base);
24+
assertType('int', $this->foo);
25+
assertType('int', $this->bar);
26+
assertType('int', $this->baz);
27+
}
28+
}

0 commit comments

Comments
 (0)