mirror of
https://github.com/danog/psalm.git
synced 2025-01-10 23:18:40 +01:00
Daniil Gentili
d0be59e16e
* 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
173 lines
5.0 KiB
PHP
173 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
use FilesystemIterator;
|
|
use RecursiveCallbackFilterIterator;
|
|
use RecursiveDirectoryIterator;
|
|
use RecursiveIterator;
|
|
use RecursiveIteratorIterator;
|
|
use UnexpectedValueException;
|
|
|
|
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;
|
|
|
|
/**
|
|
* @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 */
|
|
if (!file_exists($file_path)) {
|
|
throw new UnexpectedValueException('File ' . $file_path . ' should exist to get contents');
|
|
}
|
|
|
|
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
|
|
if (is_dir($file_path)) {
|
|
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
|
|
{
|
|
if (!file_exists($file_path)) {
|
|
throw new UnexpectedValueException('File should exist to get modified time');
|
|
}
|
|
|
|
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
|
|
{
|
|
return file_exists($file_path);
|
|
}
|
|
|
|
/**
|
|
* @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 = [];
|
|
|
|
$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);
|
|
}
|
|
);
|
|
}
|
|
|
|
/** @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;
|
|
}
|
|
}
|