1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/src/Psalm/Internal/Provider/FileProvider.php

131 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Provider;
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;
2019-07-05 22:24:00 +02:00
use function strtolower;
class FileProvider
{
/**
* @var array<lowercase-string, string>
*/
protected $temp_files = [];
/**
* @var array<lowercase-string, string>
*/
protected $open_files = [];
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->open_files[strtolower($file_path)])) {
return $this->open_files[strtolower($file_path)];
}
2019-07-11 17:07:39 +02:00
if (!file_exists($file_path)) {
throw new \UnexpectedValueException('File ' . $file_path . ' should exist to get contents');
}
if (is_dir($file_path)) {
throw new \UnexpectedValueException('File ' . $file_path . ' is a directory');
}
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 (string)file_get_contents($file_path);
}
public function setContents(string $file_path, string $file_contents): void
{
if (isset($this->open_files[strtolower($file_path)])) {
$this->open_files[strtolower($file_path)] = $file_contents;
}
if (isset($this->temp_files[strtolower($file_path)])) {
$this->temp_files[strtolower($file_path)] = $file_contents;
}
file_put_contents($file_path, $file_contents);
}
public function setOpenContents(string $file_path, string $file_contents): void
{
if (isset($this->open_files[strtolower($file_path)])) {
$this->open_files[strtolower($file_path)] = $file_contents;
}
}
public function getModifiedTime(string $file_path): int
{
2019-07-11 17:07:39 +02:00
if (!file_exists($file_path)) {
throw new \UnexpectedValueException('File should exist to get modified time');
}
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 (int)filemtime($file_path);
}
public function addTemporaryFileChanges(string $file_path, string $new_content): void
{
$this->temp_files[strtolower($file_path)] = $new_content;
}
public function removeTemporaryFileChanges(string $file_path): void
{
unset($this->temp_files[strtolower($file_path)]);
}
public function openFile(string $file_path): void
{
$this->open_files[strtolower($file_path)] = $this->getContents($file_path, true);
}
public function isOpen(string $file_path): bool
{
return isset($this->temp_files[strtolower($file_path)]) || isset($this->open_files[strtolower($file_path)]);
}
public function closeFile(string $file_path): void
{
2019-07-05 22:24:00 +02:00
unset($this->temp_files[strtolower($file_path)], $this->open_files[strtolower($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
*
2020-10-17 18:36:44 +02:00
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions): array
{
$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;
}
}