Skip to content

Commit

Permalink
Merge pull request #7713 from AndrolGenhald/bugfix/self-in-attribute
Browse files Browse the repository at this point in the history
Use current context when analyzing attributes
  • Loading branch information
orklah committed Feb 21, 2022
2 parents 35fa8b2 + 103ec62 commit 1a5b120
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Analyzer/AttributeAnalyzer.php
Expand Up @@ -27,6 +27,7 @@ class AttributeAnalyzer
*/
public static function analyze(
SourceAnalyzer $source,
Context $context,
AttributeStorage $attribute,
AttributeGroup $attribute_group,
array $suppressed_issues,
Expand Down Expand Up @@ -111,7 +112,7 @@ public static function analyze(
new NodeDataProvider()
);

$statements_analyzer->analyze(self::attributeGroupToStmts($attribute_group), new Context());
$statements_analyzer->analyze(self::attributeGroupToStmts($attribute_group), $context);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -400,6 +400,7 @@ public function analyze(
foreach ($storage->attributes as $i => $attribute) {
AttributeAnalyzer::analyze(
$this,
$class_context,
$attribute,
$class->attrGroups[$i],
$storage->suppressed_issues + $this->getSuppressedIssues(),
Expand Down Expand Up @@ -1526,6 +1527,7 @@ private function checkForMissingPropertyType(
foreach ($property_storage->attributes as $i => $attribute) {
AttributeAnalyzer::analyze(
$source,
$context,
$attribute,
$stmt->attrGroups[$i],
$this->source->getSuppressedIssues(),
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -822,6 +822,7 @@ public function analyze(
foreach ($storage->attributes as $i => $attribute) {
AttributeAnalyzer::analyze(
$this,
$context,
$attribute,
$this->function->attrGroups[$i],
$storage->suppressed_issues + $this->getSuppressedIssues(),
Expand Down Expand Up @@ -1271,6 +1272,7 @@ private function processParams(
foreach ($function_param->attributes as $i => $attribute) {
AttributeAnalyzer::analyze(
$this,
$context,
$attribute,
$param_stmts[$offset]->attrGroups[$i],
$storage->suppressed_issues,
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php
Expand Up @@ -95,10 +95,12 @@ public function analyze(): void
}

$class_storage = $codebase->classlike_storage_provider->get($fq_interface_name);
$interface_context = new Context($this->getFQCLN());

foreach ($class_storage->attributes as $i => $attribute) {
AttributeAnalyzer::analyze(
$this,
$interface_context,
$attribute,
$this->class->attrGroups[$i],
$class_storage->suppressed_issues + $this->getSuppressedIssues(),
Expand All @@ -113,7 +115,7 @@ public function analyze(): void

$type_provider = new NodeDataProvider();

$method_analyzer->analyze(new Context($this->getFQCLN()), $type_provider);
$method_analyzer->analyze($interface_context, $type_provider);

$actual_method_id = $method_analyzer->getMethodId();

Expand Down
82 changes: 82 additions & 0 deletions tests/AttributeTest.php
Expand Up @@ -264,6 +264,74 @@ public function __construct(?array $listOfB = null) {}
class C {}
',
],
'selfInClassAttribute' => [
'<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(self::class)]
class A
{
#[SomeAttr(self::class)]
public const CONST = "const";
#[SomeAttr(self::class)]
public string $foo = "bar";
#[SomeAttr(self::class)]
public function baz(): void {}
}
',
],
'parentInClassAttribute' => [
'<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
class A {}
#[SomeAttr(parent::class)]
class B extends A
{
#[SomeAttr(parent::class)]
public const CONST = "const";
#[SomeAttr(parent::class)]
public string $foo = "bar";
#[SomeAttr(parent::class)]
public function baz(): void {}
}
',
],
'selfInInterfaceAttribute' => [
'<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(self::class)]
interface C
{
#[SomeAttr(self::class)]
public const CONST = "const";
#[SomeAttr(self::class)]
public function baz(): void {}
}
',
],
];
}

Expand Down Expand Up @@ -403,6 +471,20 @@ private function __construct() {}
false,
'8.0'
],
'noParentInAttributeOnClassWithoutParent' => [
'<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(parent::class)]
class A {}
',
'error_message' => 'ParentNotFound',
],
];
}
}

0 comments on commit 1a5b120

Please sign in to comment.