Skip to content

Commit

Permalink
Support for @phpstan-property @phpstan-property-read and @phpstan-pro…
Browse files Browse the repository at this point in the history
…perty-write
  • Loading branch information
mad-briller authored Nov 22, 2021
1 parent 0900192 commit 21e8667
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,43 @@ public function resolvePropertyTags(PhpDocNode $phpDocNode, NameScope $nameScope
{
$resolved = [];

foreach ($phpDocNode->getPropertyTagValues() as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);

$resolved[$propertyName] = new PropertyTag(
$propertyType,
true,
true
);
foreach (['@property', '@phpstan-property'] as $tagName) {
foreach ($phpDocNode->getPropertyTagValues($tagName) as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);

$resolved[$propertyName] = new PropertyTag(
$propertyType,
true,
true
);
}
}

foreach ($phpDocNode->getPropertyReadTagValues() as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
foreach (['@property-read', '@phpstan-property-read'] as $tagName) {
foreach ($phpDocNode->getPropertyReadTagValues($tagName) as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);

$resolved[$propertyName] = new PropertyTag(
$propertyType,
true,
false
);
$resolved[$propertyName] = new PropertyTag(
$propertyType,
true,
false
);
}
}

foreach ($phpDocNode->getPropertyWriteTagValues() as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
foreach (['@property-write', '@phpstan-property-write'] as $tagName) {
foreach ($phpDocNode->getPropertyWriteTagValues($tagName) as $tagValue) {
$propertyName = substr($tagValue->propertyName, 1);
$propertyType = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);

$resolved[$propertyName] = new PropertyTag(
$propertyType,
false,
true
);
$resolved[$propertyName] = new PropertyTag(
$propertyType,
false,
true
);
}
}

return $resolved;
Expand Down
3 changes: 3 additions & 0 deletions src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class InvalidPHPStanDocTagRule implements \PHPStan\Rules\Rule
'@phpstan-impure',
'@phpstan-type',
'@phpstan-import-type',
'@phpstan-property',
'@phpstan-property-read',
'@phpstan-property-write',
];

private Lexer $phpDocLexer;
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/filesystem-functions.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/classPhpDocs-phpstanPropertyPrefix.php');
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/data/classPhpDocs-phpstanPropertyPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ClassPhpDocsNamespace;

use function PHPStan\Testing\assertType;

/**
* @property string $base
*
* @property string $foo
* @phpstan-property int $foo
*
* @property-read string $bar
* @phpstan-property-read int $bar
*
* @property-write string $baz
* @phpstan-property-write int $baz
*/
class PhpstanProperties
{
public function doFoo()
{
assertType('string', $this->base);
assertType('int', $this->foo);
assertType('int', $this->bar);
assertType('int', $this->baz);
}
}

0 comments on commit 21e8667

Please sign in to comment.