1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/Provider/FakeFileProvider.php
Daniil Gentili d0be59e16e
Immutable unions (#8627)
* Immutable CodeLocation

* Remove excess clones

* Remove external clones

* Remove leftover clones

* Fix final clone issue

* Immutable storages

* Refactoring

* Fixes

* Fixes

* Fix

* Fix

* Fixes

* Simplify

* Fixes

* Fix

* Fixes

* Update

* Fix

* Cache global types

* Fix

* Update

* Update

* Fixes

* Fixes

* Refactor

* Fixes

* Fix

* Fix

* More caching

* Fix

* Fix

* Update

* Update

* Fix

* Fixes

* Update

* Refactor

* Update

* Fixes

* Break one more test

* Fix

* FIx

* Fix

* Fix

* Fix

* Fix

* Improve performance and readability

* Equivalent logic

* Fixes

* Revert

* Revert "Revert"

This reverts commit f9175100c8452c80559234200663fd4c4f4dd889.

* Fix

* Fix reference bug

* Make default TypeVisitor immutable

* Bugfix

* Remove clones

* Partial refactoring

* Refactoring

* Fixes

* Fix

* Fixes

* Fixes

* cs-fix

* Fix final bugs

* Add test

* Misc fixes

* Update

* Fixes

* Experiment with removing different property

* revert "Experiment with removing different property"

This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9.

* Uniform naming

* Uniform naming

* Hack hotfix

* Clean up $_FILES ref #8621

* Undo hack, try fixing properly

* Helper method

* Remove redundant call

* Partially fix bugs

* Cleanup

* Change defaults

* Fix bug

* Fix (?, hope this doesn't break anything else)

* cs-fix

* Review fixes

* Bugfix

* Bugfix

* Improve logic

* Update
2022-11-04 19:04:23 +01:00

80 lines
2.1 KiB
PHP

<?php
namespace Psalm\Internal\Provider;
use function microtime;
use function strpos;
/**
* @internal
*/
class FakeFileProvider extends FileProvider
{
/**
* @var array<string, string>
*/
public $fake_files = [];
/**
* @var array<string, int>
*/
public $fake_file_times = [];
public function fileExists(string $file_path): bool
{
return isset($this->fake_files[$file_path]) || parent::fileExists($file_path);
}
/** @psalm-external-mutation-free */
public function getContents(string $file_path, bool $go_to_source = false): string
{
if (!$go_to_source && isset($this->temp_files[$file_path])) {
return $this->temp_files[$file_path];
}
return $this->fake_files[$file_path] ?? parent::getContents($file_path);
}
public function setContents(string $file_path, string $file_contents): void
{
$this->fake_files[$file_path] = $file_contents;
}
public function setOpenContents(string $file_path, string $file_contents): void
{
if (isset($this->fake_files[$file_path])) {
$this->fake_files[$file_path] = $file_contents;
}
}
public function getModifiedTime(string $file_path): int
{
return $this->fake_file_times[$file_path] ?? parent::getModifiedTime($file_path);
}
public function registerFile(string $file_path, string $file_contents): void
{
$this->fake_files[$file_path] = $file_contents;
$this->fake_file_times[$file_path] = (int)microtime(true);
}
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $filter
*
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, callable $filter = null): array
{
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $filter);
foreach ($this->fake_files as $file_path => $_) {
if (strpos($file_path, $dir_path) === 0) {
$file_paths[] = $file_path;
}
}
return $file_paths;
}
}