1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-05 20:48:45 +01:00
psalm/src/Psalm/Provider/FileProvider.php

118 lines
2.7 KiB
PHP

<?php
namespace Psalm\Provider;
use PhpParser;
use Psalm\Checker\ProjectChecker;
class FileProvider
{
/**
* @var array<string, string>
*/
protected $temp_files = [];
/**
* @param string $file_path
*
* @return string
*/
public function getContents($file_path, bool $go_to_source = false)
{
if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) {
return $this->temp_files[strtolower($file_path)];
}
return (string)file_get_contents($file_path);
}
/**
* @param string $file_path
* @param string $file_contents
*
* @return void
*/
public function setContents($file_path, $file_contents)
{
file_put_contents($file_path, $file_contents);
}
/**
* @param string $file_path
*
* @return int
*/
public function getModifiedTime($file_path)
{
return (int)filemtime($file_path);
}
/**
* @param \LanguageServerProtocol\TextDocumentContentChangeEvent[] $changes
* @return void
*/
public function addTemporaryFileChanges(string $file_path, array $changes)
{
if (count($changes) === 1 && $changes[0]->range === null) {
$new_content = $changes[0]->text;
$this->temp_files[strtolower($file_path)] = $new_content;
} else {
throw new \UnexpectedValueException('Incremental changes not supported');
}
}
/**
* @return void
*/
public function openFile(string $file_path)
{
$this->temp_files[strtolower($file_path)] = $this->getContents($file_path, true);
}
/**
* @return void
*/
public function closeFile(string $file_path)
{
unset($this->temp_files[strtolower($file_path)]);
}
/**
* @param string $file_path
*
* @return bool
*/
public function fileExists($file_path)
{
return file_exists($file_path);
}
/**
* @param string $dir_path
* @param array<string> $file_extensions
*
* @return array<int, string>
*/
public function getFilesInDir($dir_path, array $file_extensions)
{
$file_paths = [];
/** @var \RecursiveDirectoryIterator */
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir_path));
$iterator->rewind();
while ($iterator->valid()) {
if (!$iterator->isDot()) {
$extension = $iterator->getExtension();
if (in_array($extension, $file_extensions, true)) {
$file_paths[] = (string)$iterator->getRealPath();
}
}
$iterator->next();
}
return $file_paths;
}
}