Skip to content
Draft
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
27 changes: 21 additions & 6 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function is_array;
use function is_callable;
use function is_file;
use function is_null;
use function ltrim;
use function md5;
use function sprintf;
Expand Down Expand Up @@ -109,21 +110,35 @@ public function getResolvedPhpDoc(
return $this->createResolvedPhpDocBlock($phpDocKey, $nameScopeMap[$nameScopeKey], $docComment, $fileName);
}

$keyFileName = $fileName;
if (!isset($this->inProcess[$fileName][$nameScopeKey])) { // wrong $fileName due to traits
if (!is_null($className)) {
$reflectionProvider = $this->reflectionProviderProvider->getReflectionProvider();
if ($reflectionProvider->hasClass($className)) {
$classFileName = $reflectionProvider->getClass($className)->getFileName();
$nameScopeKeyCandidate = $this->getNameScopeKey($classFileName, $className, $traitName, $functionName);
if (isset($this->inProcess[$classFileName][$nameScopeKeyCandidate])) {
$keyFileName = $classFileName;
$nameScopeKey = $nameScopeKeyCandidate;
}
}
}
}
if (!isset($this->inProcess[$keyFileName][$nameScopeKey])) {
return ResolvedPhpDocBlock::createEmpty();
}

if ($this->inProcess[$fileName][$nameScopeKey] === true) { // PHPDoc has cyclic dependency
if ($this->inProcess[$keyFileName][$nameScopeKey] === true) { // PHPDoc has cyclic dependency
return ResolvedPhpDocBlock::createEmpty();
}

if (is_callable($this->inProcess[$fileName][$nameScopeKey])) {
$resolveCallback = $this->inProcess[$fileName][$nameScopeKey];
$this->inProcess[$fileName][$nameScopeKey] = true;
$this->inProcess[$fileName][$nameScopeKey] = $resolveCallback();
if (is_callable($this->inProcess[$keyFileName][$nameScopeKey])) {
$resolveCallback = $this->inProcess[$keyFileName][$nameScopeKey];
$this->inProcess[$keyFileName][$nameScopeKey] = true;
$this->inProcess[$keyFileName][$nameScopeKey] = $resolveCallback();
}

return $this->createResolvedPhpDocBlock($phpDocKey, $this->inProcess[$fileName][$nameScopeKey], $docComment, $fileName);
return $this->createResolvedPhpDocBlock($phpDocKey, $this->inProcess[$keyFileName][$nameScopeKey], $docComment, $fileName);
}

private function createResolvedPhpDocBlock(string $phpDocKey, NameScope $nameScope, string $phpDocString, ?string $fileName): ResolvedPhpDocBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php declare(strict_types = 1);

namespace PHPStan\Generics;

use Nette\Utils\Json;
use Nette\Utils\JsonException;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use function escapeshellarg;
use function escapeshellcmd;
use function exec;
use function implode;
use function is_null;
use function putenv;
use function sprintf;
use const DIRECTORY_SEPARATOR;
use const PHP_BINARY;

/**
* @group genericsmulti
*/
class GenericsWithMultipleFilesIntegrationTest extends PHPStanTestCase
{

/**
* @return array<array<string>>
*/
public function dataTopics(): array
{
return [

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.

Check failure on line 30 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Method PHPStan\Generics\GenericsWithMultipleFilesIntegrationTest::dataTopics() should return array<array<string>> but returns array<string, array<int, array<int, int>|string>>.
'bug9630' => ['bug9630', [9]],
'bug9630_2' => ['bug9630_2', [9]],
];
}

/**
* @dataProvider dataTopics
*
* @param int[] $levels
*/
public function testDir(string $dir, array $levels): void
{
$dir = $this->getDataPath() . DIRECTORY_SEPARATOR . $dir;
$this->assertDirectoryIsReadable($dir);
$phpstanCmd = escapeshellcmd($this->getPhpStanExecutablePath());
$configPath = $this->getPhpStanConfigPath();

$configOption = is_null($configPath) ? '' : '--configuration ' . escapeshellarg($configPath);

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Call to function is_null() with string will always evaluate to false.

Check failure on line 48 in tests/PHPStan/Generics/GenericsWithMultipleFilesIntegrationTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Call to function is_null() with string will always evaluate to false.

exec(sprintf('%s %s clear-result-cache %s 2>&1', escapeshellcmd(PHP_BINARY), $phpstanCmd, $configOption), $clearResultCacheOutputLines, $clearResultCacheExitCode);

if ($clearResultCacheExitCode !== 0) {
throw new ShouldNotHappenException('Could not clear result cache: ' . implode("\n", $clearResultCacheOutputLines));
}

putenv('__PHPSTAN_FORCE_VALIDATE_STUB_FILES=1');

foreach ($levels as $level) {
unset($outputLines);

$toExec = sprintf('%s %s analyse --no-progress --error-format=prettyJson --level=%d %s %s', escapeshellcmd(PHP_BINARY), $phpstanCmd, $level, $configOption, escapeshellarg($dir));

exec($toExec, $outputLines);

$output = implode("\n", $outputLines);

try {
$actualJson = Json::decode($output, Json::FORCE_ARRAY);
} catch (JsonException) {
throw new JsonException(sprintf('Cannot decode: %s', $output));
}

// Check that there was no error during the execution of PHPStan
foreach ($actualJson['files'] as $file => $fileJson) {
$this->assertCount(0, $fileJson['messages'], 'The file ' . $file . ' contains errors. The command that produced the error was: ' . $toExec);
}
}
}

public function getDataPath(): string
{
return __DIR__ . '/data';
}

public function getPhpStanExecutablePath(): string
{
return __DIR__ . '/../../../bin/phpstan';
}

public function getPhpStanConfigPath(): string
{
return __DIR__ . '/generics.neon';
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Generics/data/bug9630/C1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Generics\Bug9630;

/**
* @implements B<A1>
*/
class C1 implements B
{
/**
* @use T1<A2>
*/
use T1;

public function f(): ?A
{
return $this->getParam(new A2());
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Generics/data/bug9630/T1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Generics\Bug9630;

/**
* @template T of A
*/
trait T1
{
/**
* @use T2<template-type<T, A, 'T'>>
*/
use T2;

/**
* @param T $p
* @return template-type<T, A, 'T'>
*/
public function getParam(A $p): ?A
{
return $this->getParamFromT2($p->getOther());
}
}

19 changes: 19 additions & 0 deletions tests/PHPStan/Generics/data/bug9630/T2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Generics\Bug9630;

/**
* @template S of A
*/
trait T2
{
/**
* @param S $p
* @return S
*/
public function getParamFromT2(A $p): ?A
{
return $p;
}
}

48 changes: 48 additions & 0 deletions tests/PHPStan/Generics/data/bug9630/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Generics\Bug9630;

/**
* @template T of A
*/
interface A
{
/**
* @return T
*/
public function getOther(): A;
}

/**
* @implements A<A2>
*/
class A1 implements A
{
public function getOther(): A2
{
return new A2();
}
}

/**
* @implements A<A1>
*/
class A2 implements A
{
public function getOther(): A1
{
return new A1();
}
}

/**
* @template T of A
*/
interface B
{
/**
* @return T|null
*/
public function f(): ?A;
}

19 changes: 19 additions & 0 deletions tests/PHPStan/Generics/data/bug9630_2/C1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Generics\Bug9630_2;

/**
* @implements B<A1>
*/
class C1 implements B
{
/**
* @use T1<A2>
*/
use T1;

public function f(): ?A
{
return $this->getParam(new A2());
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Generics/data/bug9630_2/T1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Generics\Bug9630_2;

/**
* @template T of A
*/
trait T1
{
/**
* @use T2<template-type<T, A, 'T'>>
*/
use T2;

/**
* @param T $p
* @return template-type<T, A, 'T'>
*/
public function getParam(A $p): ?A
{
return $this->getParamFromT2($p->getOther());
}
}

19 changes: 19 additions & 0 deletions tests/PHPStan/Generics/data/bug9630_2/T2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Generics\Bug9630_2;

/**
* @template T of A
*/
trait T2
{
/**
* @param T $p
* @return T
*/
public function getParamFromT2(A $p): ?A
{
return $p;
}
}

48 changes: 48 additions & 0 deletions tests/PHPStan/Generics/data/bug9630_2/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Generics\Bug9630_2;

/**
* @template T of A
*/
interface A
{
/**
* @return T
*/
public function getOther(): A;
}

/**
* @implements A<A2>
*/
class A1 implements A
{
public function getOther(): A2
{
return new A2();
}
}

/**
* @implements A<A1>
*/
class A2 implements A
{
public function getOther(): A1
{
return new A1();
}
}

/**
* @template T of A
*/
interface B
{
/**
* @return T|null
*/
public function f(): ?A;
}

Loading