1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add test for #1017

This commit is contained in:
Matthew Brown 2018-10-11 23:00:32 -04:00
parent af149bd4dc
commit edc219facb
6 changed files with 248 additions and 4 deletions

View File

@ -877,11 +877,15 @@ class ClassLikes
$this->existing_classlikes_lc[$fq_class_name_lc],
$this->existing_classes_lc[$fq_class_name_lc],
$this->existing_traits_lc[$fq_class_name_lc],
$this->existing_traits[$fq_class_name],
$this->existing_interfaces_lc[$fq_class_name_lc],
$this->existing_interfaces[$fq_class_name],
$this->existing_classes[$fq_class_name],
$this->trait_nodes[$fq_class_name_lc],
$this->trait_aliases[$fq_class_name_lc],
$this->classlike_references[$fq_class_name_lc]
);
$this->scanner->removeClassLike($fq_class_name_lc);
}
}

View File

@ -165,9 +165,17 @@ class Scanner
*/
public function removeFile($file_path)
{
unset(
$this->scanned_files[$file_path]
);
unset($this->scanned_files[$file_path]);
}
/**
* @param string $fq_classlike_name_lc
* @return void
*/
public function removeClassLike($fq_classlike_name_lc)
{
unset($this->classlike_files[$fq_classlike_name_lc]);
unset($this->deep_scanned_classlike_files[$fq_classlike_name_lc]);
}
/**

View File

@ -0,0 +1,115 @@
<?php
namespace Psalm\Tests\FileUpdates;
use Psalm\Checker\FileChecker;
use Psalm\Checker\ProjectChecker;
use Psalm\Provider\Providers;
use Psalm\Tests\TestConfig;
use Psalm\Tests\Provider;
class CachedStorageTest extends \Psalm\Tests\TestCase
{
/**
* @return void
*/
public function setUp()
{
parent::setUp();
FileChecker::clearCache();
$this->file_provider = new \Psalm\Tests\Provider\FakeFileProvider();
$config = new TestConfig();
$providers = new Providers(
$this->file_provider,
new Provider\ParserInstanceCacheProvider(),
new Provider\FileStorageInstanceCacheProvider(),
new Provider\ClassLikeStorageInstanceCacheProvider(),
new Provider\FakeFileReferenceCacheProvider()
);
$this->project_checker = new ProjectChecker(
$config,
$providers,
false,
true,
ProjectChecker::TYPE_CONSOLE,
1,
false
);
$this->project_checker->infer_types_from_usage = true;
}
/**
* @return void
*/
public function testValidInclude()
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$this->project_checker->diff_methods = true;
$codebase = $this->project_checker->getCodebase();
$vendor_files = [
getcwd() . DIRECTORY_SEPARATOR . 'V1.php' => '<?php
namespace AnotherPackage;
interface StorageInterface {
public function getRecord(): OperationInterface;
}',
getcwd() . DIRECTORY_SEPARATOR . 'V2.php' => '<?php
namespace AnotherPackage;
interface OperationInterface {
public function getResult(): ResultInterface;
}',
getcwd() . DIRECTORY_SEPARATOR . 'V3.php' => '<?php
namespace AnotherPackage;
interface ResultInterface {}',
];
$analyzable_files = [
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
use AnotherPackage\StorageInterface;
class C {
/** @var ?StorageInterface */
private $storage;
public function zugzug() : void {
if (!$this->storage) {
return;
}
$result = $this->storage->getRecord()->getResult();
}
}',
];
foreach ($vendor_files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
$codebase->scanner->addFilesToShallowScan([$file_path => $file_path]);
}
foreach ($analyzable_files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
$codebase->addFilesToAnalyze([$file_path => $file_path]);
}
$codebase->scanFiles();
$codebase->analyzer->analyzeFiles($this->project_checker, 1, false);
$codebase->reloadFiles($this->project_checker, array_keys($analyzable_files + $vendor_files));
foreach ($analyzable_files as $file_path => $_) {
$codebase->addFilesToAnalyze([$file_path => $file_path]);
}
$codebase->scanFiles();
$codebase->analyzer->analyzeFiles($this->project_checker, 1, false);
}
}

View File

@ -21,7 +21,6 @@ class CorrectMethodTest extends \Psalm\Tests\TestCase
$this->file_provider = new \Psalm\Tests\Provider\FakeFileProvider();
$config = new TestConfig();
$config->throw_exception = false;
$providers = new Providers(
$this->file_provider,

View File

@ -0,0 +1,55 @@
<?php
namespace Psalm\Tests\Provider;
use Psalm\Config;
use Psalm\Storage\ClassLikeStorage;
class ClassLikeStorageInstanceCacheProvider extends \Psalm\Provider\ClassLikeStorageCacheProvider
{
/** @var array<string, ClassLikeStorage> */
private $cache = [];
public function __construct()
{
}
/**
* @param string|null $file_path
* @param string|null $file_contents
*
* @return void
*/
public function writeToCache(ClassLikeStorage $storage, $file_path, $file_contents)
{
$fq_classlike_name_lc = strtolower($storage->name);
$this->cache[$fq_classlike_name_lc] = $storage;
}
/**
* @param string $fq_classlike_name_lc
* @param string|null $file_path
* @param string|null $file_contents
*
* @return ClassLikeStorage
*/
public function getLatestFromCache($fq_classlike_name_lc, $file_path, $file_contents)
{
$cached_value = $this->loadFromCache($fq_classlike_name_lc);
if (!$cached_value) {
throw new \UnexpectedValueException('Should be in cache');
}
return $cached_value;
}
/**
* @param string $fq_classlike_name_lc
*
* @return ClassLikeStorage|null
*/
private function loadFromCache($fq_classlike_name_lc)
{
return $this->cache[$fq_classlike_name_lc] ?? null;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Psalm\Tests\Provider;
use Psalm\Storage\FileStorage;
class FileStorageInstanceCacheProvider extends \Psalm\Provider\FileStorageCacheProvider
{
/** @var array<string, FileStorage> */
private $cache = [];
public function __construct()
{
}
/**
* @param string $file_path
* @param string $file_contents
*
* @return void
*/
public function writeToCache(FileStorage $storage, $file_contents)
{
$file_path = strtolower($storage->file_path);
$this->cache[$file_path] = $storage;
}
/**
* @param string $file_path
* @param string $file_contents
*
* @return FileStorage|null
*/
public function getLatestFromCache($file_path, $file_contents)
{
$cached_value = $this->loadFromCache(strtolower($file_path));
if (!$cached_value) {
return null;
}
return $cached_value;
}
/**
* @param string $file_path
*
* @return void
*/
public function removeCacheForFile($file_path)
{
unset($this->cache[strtolower($file_path)]);
}
/**
* @param string $file_path
*
* @return FileStorage|null
*/
private function loadFromCache($file_path)
{
return $this->cache[strtolower($file_path)] ?? null;
}
}