Skip to content

Commit

Permalink
only load files once
Browse files Browse the repository at this point in the history
* use static to keep opened files with content
* move position of file cache population to the place where we read files to ensure cache always gets populated and not on open only (since it's called directly in some places)
  • Loading branch information
kkmuffme committed May 25, 2022
1 parent ee6c105 commit 06178d0
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Psalm/Internal/Provider/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FileProvider
/**
* @var array<lowercase-string, string>
*/
protected $open_files = [];
protected static $open_files = [];

public function getContents(string $file_path, bool $go_to_source = false): string
{
Expand All @@ -38,8 +38,8 @@ public function getContents(string $file_path, bool $go_to_source = false): stri
return $this->temp_files[$file_path_lc];
}

if (isset($this->open_files[$file_path_lc])) {
return $this->open_files[$file_path_lc];
if (isset(self::$open_files[$file_path_lc])) {
return self::$open_files[$file_path_lc];
}

if (!file_exists($file_path)) {
Expand All @@ -50,14 +50,18 @@ public function getContents(string $file_path, bool $go_to_source = false): stri
throw new UnexpectedValueException('File ' . $file_path . ' is a directory');
}

return (string)file_get_contents($file_path);
$file_contents = (string) file_get_contents($file_path);

self::$open_files[$file_path_lc] = $file_contents;

return $file_contents;
}

public function setContents(string $file_path, string $file_contents): void
{
$file_path_lc = strtolower($file_path);
if (isset($this->open_files[$file_path_lc])) {
$this->open_files[$file_path_lc] = $file_contents;
if (isset(self::$open_files[$file_path_lc])) {
self::$open_files[$file_path_lc] = $file_contents;
}

if (isset($this->temp_files[$file_path_lc])) {
Expand All @@ -70,8 +74,8 @@ public function setContents(string $file_path, string $file_contents): void
public function setOpenContents(string $file_path, string $file_contents): void
{
$file_path_lc = strtolower($file_path);
if (isset($this->open_files[$file_path_lc])) {
$this->open_files[$file_path_lc] = $file_contents;
if (isset(self::$open_files[$file_path_lc])) {
self::$open_files[$file_path_lc] = $file_contents;
}
}

Expand All @@ -81,7 +85,7 @@ public function getModifiedTime(string $file_path): int
throw new UnexpectedValueException('File should exist to get modified time');
}

return (int)filemtime($file_path);
return (int) filemtime($file_path);
}

public function addTemporaryFileChanges(string $file_path, string $new_content): void
Expand All @@ -96,19 +100,19 @@ public function removeTemporaryFileChanges(string $file_path): void

public function openFile(string $file_path): void
{
$this->open_files[strtolower($file_path)] = $this->getContents($file_path, true);
self::$open_files[strtolower($file_path)] = $this->getContents($file_path, true);
}

public function isOpen(string $file_path): bool
{
$file_path_lc = strtolower($file_path);
return isset($this->temp_files[$file_path_lc]) || isset($this->open_files[$file_path_lc]);
return isset($this->temp_files[$file_path_lc]) || isset(self::$open_files[$file_path_lc]);
}

public function closeFile(string $file_path): void
{
$file_path_lc = strtolower($file_path);
unset($this->temp_files[$file_path_lc], $this->open_files[$file_path_lc]);
unset($this->temp_files[$file_path_lc], self::$open_files[$file_path_lc]);
}

public function fileExists(string $file_path): bool
Expand Down

0 comments on commit 06178d0

Please sign in to comment.