Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use current context when analyzing attributes #7713

Merged
merged 2 commits into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
],
];
}
}