Skip to content

Class attributes should also be included in class statements #1338

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

Merged
merged 1 commit into from
May 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,15 @@ private function processStmtNode(
throw new ShouldNotHappenException();
}

$classStatementsGatherer = new ClassStatementsGatherer($classReflection, $nodeCallback);
foreach ($stmt->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
foreach ($attr->args as $arg) {
$this->processExprNode($arg->value, $classScope, $nodeCallback, ExpressionContext::createDeep());
$this->processExprNode($arg->value, $classScope, $classStatementsGatherer, ExpressionContext::createDeep());
}
}
}

$classStatementsGatherer = new ClassStatementsGatherer($classReflection, $nodeCallback);
$this->processStmtNodes($stmt, $stmt->stmts, $classScope, $classStatementsGatherer);
$nodeCallback(new ClassPropertiesNode($stmt, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls()), $classScope);
$nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls()), $classScope);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,16 @@ public function testBug7078(): void
$this->assertNoErrors($errors);
}

public function testBug7116(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$errors = $this->runAnalyse(__DIR__ . '/data/bug-7116.php');
$this->assertNoErrors($errors);
}

public function testBug3853(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3853.php');
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7116.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace Bug7116;

#[\Attribute(\Attribute::TARGET_CLASS)]
class Cache
{
public function __construct(public int $ttl)
{
}
}

#[Cache(ttl: self::CACHE_TTL)]
class HelloWorld
{
private const CACHE_TTL = 60;
}