Skip to content

Commit bd3f7f3

Browse files
authored
Optimize memory consumption
1 parent 7c4ef6b commit bd3f7f3

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use PHPStan\Command\Output;
1111
use PHPStan\Dependency\ExportedNodeFetcher;
1212
use PHPStan\Dependency\RootExportedNode;
13+
use PHPStan\File\CouldNotReadFileException;
1314
use PHPStan\File\FileFinder;
14-
use PHPStan\File\FileReader;
1515
use PHPStan\File\FileWriter;
1616
use PHPStan\Internal\ComposerHelper;
1717
use PHPStan\PhpDoc\StubFilesProvider;
@@ -33,10 +33,9 @@
3333
use function is_file;
3434
use function is_string;
3535
use function ksort;
36-
use function sha1;
36+
use function sha1_file;
3737
use function sort;
3838
use function sprintf;
39-
use function str_replace;
4039
use function time;
4140
use function unlink;
4241
use function var_export;
@@ -789,10 +788,10 @@ private function getFileHash(string $path): string
789788
return $this->fileHashes[$path];
790789
}
791790

792-
$contents = FileReader::read($path);
793-
$contents = str_replace("\r\n", "\n", $contents);
794-
795-
$hash = sha1($contents);
791+
$hash = sha1_file($path);
792+
if ($hash === false) {
793+
throw new CouldNotReadFileException($path);
794+
}
796795
$this->fileHashes[$path] = $hash;
797796

798797
return $hash;

src/DependencyInjection/Configurator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use Nette\DI\Config\Loader;
66
use Nette\DI\Container as OriginalNetteContainer;
77
use Nette\DI\ContainerLoader;
8-
use PHPStan\File\FileReader;
8+
use PHPStan\File\CouldNotReadFileException;
99
use function array_keys;
1010
use function error_reporting;
1111
use function restore_error_handler;
1212
use function set_error_handler;
13-
use function sha1;
13+
use function sha1_file;
1414
use const E_USER_DEPRECATED;
1515
use const PHP_RELEASE_VERSION;
1616
use const PHP_VERSION_ID;
@@ -92,7 +92,13 @@ private function getAllConfigFilesHashes(): array
9292
{
9393
$hashes = [];
9494
foreach ($this->allConfigFiles as $file) {
95-
$hashes[$file] = sha1(FileReader::read($file));
95+
$hash = sha1_file($file);
96+
97+
if ($hash === false) {
98+
throw new CouldNotReadFileException($file);
99+
}
100+
101+
$hashes[$file] = $hash;
96102
}
97103

98104
return $hashes;

src/File/FileMonitor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use function array_key_exists;
77
use function array_keys;
88
use function count;
9-
use function sha1;
9+
use function sha1_file;
1010

1111
class FileMonitor
1212
{
@@ -81,7 +81,13 @@ public function getChanges(): FileMonitorResult
8181

8282
private function getFileHash(string $filePath): string
8383
{
84-
return sha1(FileReader::read($filePath));
84+
$hash = sha1_file($filePath);
85+
86+
if ($hash === false) {
87+
throw new CouldNotReadFileException($filePath);
88+
}
89+
90+
return $hash;
8591
}
8692

8793
}

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
44

55
use PHPStan\Cache\Cache;
6-
use PHPStan\File\CouldNotReadFileException;
76
use PHPStan\File\FileFinder;
8-
use PHPStan\File\FileReader;
97
use PHPStan\Php\PhpVersion;
108
use PHPStan\Reflection\ConstantNameHelper;
119
use function array_key_exists;
@@ -15,7 +13,7 @@
1513
use function php_strip_whitespace;
1614
use function preg_match_all;
1715
use function preg_replace;
18-
use function sha1;
16+
use function sha1_file;
1917
use function sprintf;
2018
use function strtolower;
2119

@@ -42,12 +40,11 @@ public function createByDirectory(string $directory): NewOptimizedDirectorySourc
4240
$files = $this->fileFinder->findFiles([$directory])->getFiles();
4341
$fileHashes = [];
4442
foreach ($files as $file) {
45-
try {
46-
$contents = FileReader::read($file);
47-
} catch (CouldNotReadFileException) {
43+
$hash = sha1_file($file);
44+
if ($hash === false) {
4845
continue;
4946
}
50-
$fileHashes[$file] = sha1($contents);
47+
$fileHashes[$file] = $hash;
5148
}
5249

5350
$cacheKey = sprintf('odsl-%s', $directory);

src/Type/Constant/ConstantStringType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ function (): string {
135135

136136
private function export(string $value): string
137137
{
138-
if (Strings::match($value, '([\000-\037])') !== null) {
138+
$escapedValue = addcslashes($value, "\0..\37");
139+
if ($escapedValue !== $value) {
139140
return '"' . addcslashes($value, "\0..\37\\\"") . '"';
140141
}
141142

0 commit comments

Comments
 (0)