Skip to content

Fix trait detection recursion #1050

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 3 commits into from
Mar 5, 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
5 changes: 4 additions & 1 deletion src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode
private function getNameScopeMap(string $fileName): array
{
if (!isset($this->memoryCache[$fileName])) {
$cacheKey = sprintf('%s-phpdocstring-v21-explicit-mixed', $fileName);
$cacheKey = sprintf('%s-phpdocstring-v22-trait-bug', $fileName);
$variableCacheKey = sprintf('%s-%s', implode(',', array_map(static fn (array $file): string => sprintf('%s-%d', $file['filename'], $file['modifiedTime']), $this->getCachedDependentFilesWithTimestamps($fileName))), $this->phpVersion->getVersionString());
$map = $this->cache->load($cacheKey, $variableCacheKey);

Expand Down Expand Up @@ -291,6 +291,9 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA
} elseif ((bool) $node->getAttribute('anonymousClass', false)) {
$className = $node->name->name;
} else {
if ($traitFound) {
return self::SKIP_NODE;
}
$className = ltrim(sprintf('%s\\%s', $namespace, $node->name->name), '\\');
}
$classStack[] = $className;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ public function testBug6212(): void
$this->assertNoErrors($errors);
}

public function testBug6740(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-6740-b.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6740-a.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Bug6740;

trait BlockTemplate
{
public function __construct()
{
parent::__construct();

$this->StdClassSetup(get_class());
}
}

class A
{
/** @var string[] */

private $classList = [];

/**
* @returns $this
*/

public function __construct()
{
}

/**
* Apply all the standard configuration needs for a sub-class
*
* @param string $baseClass
*/

public function StdClassSetup($baseClass): void
{
$this->classList[] = $baseClass;
}

/**
* @return string[]
*/

public function GetClassList()
{
return $this->classList;
}
}

class Box extends A
{
use BlockTemplate;
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6740-b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Bug6740;

class B extends A
{
use BlockTemplate;
}