1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/Provider/FileProvider.php

173 lines
5.0 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Provider;
use FilesystemIterator;
use RecursiveCallbackFilterIterator;
2021-12-03 21:40:18 +01:00
use RecursiveDirectoryIterator;
use RecursiveIterator;
2021-12-03 21:40:18 +01:00
use RecursiveIteratorIterator;
use UnexpectedValueException;
2019-07-05 22:24:00 +02:00
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function in_array;
use function is_dir;
use const DIRECTORY_SEPARATOR;
2022-01-03 07:55:32 +01:00
/**
* @internal
*/
class FileProvider
{
/**
* @var array<string, string>
*/
protected $temp_files = [];
/**
* @var array<string, string>
*/
protected static $open_files = [];
/** @psalm-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];
}
/** @psalm-suppress ImpureStaticProperty Used only for caching */
if (isset(self::$open_files[$file_path])) {
return self::$open_files[$file_path];
}
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
2019-07-11 17:07:39 +02:00
if (!file_exists($file_path)) {
2021-12-03 21:40:18 +01:00
throw new UnexpectedValueException('File ' . $file_path . ' should exist to get contents');
2019-07-11 17:07:39 +02:00
}
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
if (is_dir($file_path)) {
2021-12-03 21:40:18 +01:00
throw new UnexpectedValueException('File ' . $file_path . ' is a directory');
}
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
$file_contents = (string) file_get_contents($file_path);
/** @psalm-suppress ImpureStaticProperty Used only for caching */
self::$open_files[$file_path] = $file_contents;
return $file_contents;
}
public function setContents(string $file_path, string $file_contents): void
{
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents;
}
if (isset($this->temp_files[$file_path])) {
$this->temp_files[$file_path] = $file_contents;
}
file_put_contents($file_path, $file_contents);
}
public function setOpenContents(string $file_path, string $file_contents): void
{
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents;
}
}
public function getModifiedTime(string $file_path): int
{
2019-07-11 17:07:39 +02:00
if (!file_exists($file_path)) {
2021-12-03 21:40:18 +01:00
throw new UnexpectedValueException('File should exist to get modified time');
2019-07-11 17:07:39 +02:00
}
return (int) filemtime($file_path);
}
public function addTemporaryFileChanges(string $file_path, string $new_content): void
{
$this->temp_files[$file_path] = $new_content;
}
public function removeTemporaryFileChanges(string $file_path): void
{
unset($this->temp_files[$file_path]);
}
public function openFile(string $file_path): void
{
self::$open_files[$file_path] = $this->getContents($file_path, true);
}
public function isOpen(string $file_path): bool
{
return isset($this->temp_files[$file_path]) || isset(self::$open_files[$file_path]);
}
public function closeFile(string $file_path): void
{
unset($this->temp_files[$file_path], self::$open_files[$file_path]);
}
public function fileExists(string $file_path): bool
{
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
return file_exists($file_path);
}
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $filter
*
2020-10-17 18:36:44 +02:00
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, callable $filter = null): array
{
$file_paths = [];
$iterator = new RecursiveDirectoryIterator(
$dir_path,
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
);
if ($filter !== null) {
$iterator = new RecursiveCallbackFilterIterator(
$iterator,
/** @param mixed $_ */
static function (string $current, $_, RecursiveIterator $iterator) use ($filter): bool {
if ($iterator->hasChildren()) {
$path = $current . DIRECTORY_SEPARATOR;
} else {
$path = $current;
}
return $filter($path);
}
);
}
2021-12-04 03:37:19 +01:00
/** @var RecursiveDirectoryIterator */
$iterator = new RecursiveIteratorIterator($iterator);
$iterator->rewind();
while ($iterator->valid()) {
$extension = $iterator->getExtension();
if (in_array($extension, $file_extensions, true)) {
$file_paths[] = (string)$iterator->getRealPath();
}
$iterator->next();
}
return $file_paths;
}
}