From 248a07fd359273b2791708c9732020fd8b40061c Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 2 Jul 2021 00:26:20 +0300 Subject: [PATCH] Ship FakeFileProvider even with dist dev releases Refs psalm/psalm.dev#60 --- .../Internal/Provider/FakeFileProvider.php | 86 +++++++++++++++++++ tests/Internal/Provider/FakeFileProvider.php | 82 +----------------- 2 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 src/Psalm/Internal/Provider/FakeFileProvider.php diff --git a/src/Psalm/Internal/Provider/FakeFileProvider.php b/src/Psalm/Internal/Provider/FakeFileProvider.php new file mode 100644 index 000000000..0a7de8e83 --- /dev/null +++ b/src/Psalm/Internal/Provider/FakeFileProvider.php @@ -0,0 +1,86 @@ + + */ + public $fake_files = []; + + /** + * @var array + */ + public $fake_file_times = []; + + public function fileExists(string $file_path): bool + { + return isset($this->fake_files[$file_path]) || parent::fileExists($file_path); + } + + public function getContents(string $file_path, bool $go_to_source = false): string + { + if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) { + return $this->temp_files[strtolower($file_path)]; + } + + if (isset($this->fake_files[$file_path])) { + return $this->fake_files[$file_path]; + } + + return 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[strtolower($file_path)])) { + $this->fake_files[strtolower($file_path)] = $file_contents; + } + } + + public function getModifiedTime(string $file_path): int + { + if (isset($this->fake_file_times[$file_path])) { + return $this->fake_file_times[$file_path]; + } + + return parent::getModifiedTime($file_path); + } + + /** + * @psalm-suppress InvalidPropertyAssignmentValue because microtime is needed for cache busting + */ + public function registerFile(string $file_path, string $file_contents): void + { + $this->fake_files[$file_path] = $file_contents; + $this->fake_file_times[$file_path] = microtime(true); + } + + /** + * @param array $file_extensions + * + * @return list + */ + public function getFilesInDir(string $dir_path, array $file_extensions): array + { + $file_paths = parent::getFilesInDir($dir_path, $file_extensions); + + foreach ($this->fake_files as $file_path => $_) { + if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) { + $file_paths[] = $file_path; + } + } + + return $file_paths; + } +} diff --git a/tests/Internal/Provider/FakeFileProvider.php b/tests/Internal/Provider/FakeFileProvider.php index 45cac5a8a..a9029f8ed 100644 --- a/tests/Internal/Provider/FakeFileProvider.php +++ b/tests/Internal/Provider/FakeFileProvider.php @@ -1,85 +1,7 @@ - */ - public $fake_files = []; - - /** - * @var array - */ - public $fake_file_times = []; - - public function fileExists(string $file_path): bool - { - return isset($this->fake_files[$file_path]) || parent::fileExists($file_path); - } - - public function getContents(string $file_path, bool $go_to_source = false): string - { - if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) { - return $this->temp_files[strtolower($file_path)]; - } - - if (isset($this->fake_files[$file_path])) { - return $this->fake_files[$file_path]; - } - - return 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[strtolower($file_path)])) { - $this->fake_files[strtolower($file_path)] = $file_contents; - } - } - - public function getModifiedTime(string $file_path): int - { - if (isset($this->fake_file_times[$file_path])) { - return $this->fake_file_times[$file_path]; - } - - return parent::getModifiedTime($file_path); - } - - /** - * @psalm-suppress InvalidPropertyAssignmentValue because microtime is needed for cache busting - */ - public function registerFile(string $file_path, string $file_contents): void - { - $this->fake_files[$file_path] = $file_contents; - $this->fake_file_times[$file_path] = microtime(true); - } - - /** - * @param array $file_extensions - * - * @return list - */ - public function getFilesInDir(string $dir_path, array $file_extensions): array - { - $file_paths = parent::getFilesInDir($dir_path, $file_extensions); - - foreach ($this->fake_files as $file_path => $_) { - if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) { - $file_paths[] = $file_path; - } - } - - return $file_paths; - } }