Skip to content

Commit

Permalink
Support a bunch of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 31, 2020
1 parent 938cebc commit 957a554
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
43 changes: 41 additions & 2 deletions src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,37 @@ function (array $l, array $r) : int {

foreach ($node->attrGroups as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$storage->attributes[] = AttributeResolver::resolve(
$attribute = AttributeResolver::resolve(
$this->codebase,
$this->file_scanner,
$this->file_storage,
$this->aliases,
$attr,
$this->storage->name ?? null
);

if ($attribute->fq_class_name === 'Psalm\\Deprecated'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Deprecated'
) {
$storage->deprecated = true;
}

if ($attribute->fq_class_name === 'Psalm\\Internal' && !$storage->internal) {
$storage->internal = NamespaceAnalyzer::getNameSpaceRoot($fq_classlike_name);
}

if ($attribute->fq_class_name === 'Psalm\\Immutable'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Immutable'
) {
$storage->mutation_free = true;
$storage->external_mutation_free = true;
}

if ($attribute->fq_class_name === 'Psalm\\ExternalMutationFree') {
$storage->external_mutation_free = true;
}

$storage->attributes[] = $attribute;
}
}

Expand Down Expand Up @@ -1416,14 +1439,30 @@ private function visitPropertyDeclaration(

foreach ($stmt->attrGroups as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$property_storage->attributes[] = AttributeResolver::resolve(
$attribute = AttributeResolver::resolve(
$this->codebase,
$this->file_scanner,
$this->file_storage,
$this->aliases,
$attr,
$this->storage->name ?? null
);

if ($attribute->fq_class_name === 'Psalm\\Deprecated'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Deprecated'
) {
$property_storage->deprecated = true;
}

if ($attribute->fq_class_name === 'Psalm\\Internal' && !$property_storage->internal) {
$property_storage->internal = NamespaceAnalyzer::getNameSpaceRoot($fq_classlike_name);
}

if ($attribute->fq_class_name === 'Psalm\\Readonly') {
$property_storage->readonly = true;
}

$property_storage->attributes[] = $attribute;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psalm\Exception\IncorrectDocblockException;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\SimpleTypeInferer;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Internal\Type\TypeAlias;
Expand Down Expand Up @@ -768,14 +769,46 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal

foreach ($stmt->getAttrGroups() as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$storage->attributes[] = AttributeResolver::resolve(
$attribute = AttributeResolver::resolve(
$this->codebase,
$this->file_scanner,
$this->file_storage,
$this->aliases,
$attr,
$this->classlike_storage->name ?? null
);

if ($attribute->fq_class_name === 'Psalm\\Pure'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Pure'
) {
$storage->specialize_call = true;
$storage->mutation_free = true;
if ($storage instanceof MethodStorage) {
$storage->external_mutation_free = true;
}
}

if ($attribute->fq_class_name === 'Psalm\\Deprecated'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Deprecated'
) {
$storage->deprecated = true;
}

if ($attribute->fq_class_name === 'Psalm\\Internal' && !$storage->internal && $fq_classlike_name) {
$storage->internal = NamespaceAnalyzer::getNameSpaceRoot($fq_classlike_name);
}

if ($attribute->fq_class_name === 'Psalm\\ExternalMutationFree'
&& $storage instanceof MethodStorage
) {
$storage->external_mutation_free = true;
}

if ($attribute->fq_class_name === 'JetBrains\\PhpStorm\\NoReturn') {
$storage->return_type = new Type\Union([new Type\Atomic\TNever()]);
}

$storage->attributes[] = $attribute;
}
}

Expand Down

0 comments on commit 957a554

Please sign in to comment.