Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\File\FileWriter;
use PHPStan\Internal\ArrayHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\FileHashing;
use PHPStan\PhpDoc\StubFilesProvider;
use PHPStan\ShouldNotHappenException;
use ReflectionClass;
Expand Down Expand Up @@ -1206,7 +1207,7 @@ private function getFileHash(string $path): string
return $this->fileHashes[$path];
}

$hash = hash_file('sha256', $path);
$hash = hash_file(FileHashing::ALGORITHM, $path);
if ($hash === false) {
throw new CouldNotReadFileException($path);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Command/AnalyseApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Collectors\CollectedData;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Internal\BytesHelper;
use PHPStan\Internal\FileHashing;
use PHPStan\PhpDoc\StubFilesProvider;
use PHPStan\PhpDoc\StubValidator;
use PHPStan\ShouldNotHappenException;
Expand Down Expand Up @@ -160,7 +161,7 @@ public function analyse(
continue;
}

$newHash = hash_file('sha256', $file);
$newHash = hash_file(FileHashing::ALGORITHM, $file);
if ($newHash === $hash) {
continue;
}
Expand Down
5 changes: 3 additions & 2 deletions src/DependencyInjection/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\File\CouldNotWriteFileException;
use PHPStan\File\FileReader;
use PHPStan\File\FileWriter;
use PHPStan\Internal\FileHashing;
use PHPStan\Turbo\TurboExtensionEnabler;
use function array_keys;
use function count;
Expand Down Expand Up @@ -106,7 +107,7 @@ public function loadContainer(): string
array_keys($this->dynamicParameters),
$this->configs,
PHP_VERSION_ID - PHP_RELEASE_VERSION,
is_file($attributesPhp) ? hash_file('sha256', $attributesPhp) : 'attributes-missing',
is_file($attributesPhp) ? hash_file(FileHashing::ALGORITHM, $attributesPhp) : 'attributes-missing',
NeonAdapter::CACHE_KEY,
$this->getAllConfigFilesHashes(),
var_export(TurboExtensionEnabler::isLoaded(), true),
Expand Down Expand Up @@ -242,7 +243,7 @@ private function getAllConfigFilesHashes(): array
{
$hashes = [];
foreach ($this->allConfigFiles as $file) {
$hash = hash_file('sha256', $file);
$hash = hash_file(FileHashing::ALGORITHM, $file);

if ($hash === false) {
throw new CouldNotReadFileException($file);
Expand Down
3 changes: 2 additions & 1 deletion src/File/FileMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Internal\FileHashing;
use PHPStan\ShouldNotHappenException;
use function array_diff;
use function array_key_exists;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function getChanges(): FileMonitorResult

private function getFileHash(string $filePath): string
{
$hash = hash_file('sha256', $filePath);
$hash = hash_file(FileHashing::ALGORITHM, $filePath);

if ($hash === false) {
throw new CouldNotReadFileException($filePath);
Expand Down
17 changes: 17 additions & 0 deletions src/Internal/FileHashing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace PHPStan\Internal;

use const PHP_VERSION_ID;

final class FileHashing
{

/**
* Algorithm for file content hashes used as cache-invalidation keys.
*
* xxh128 is ~8.5x faster than sha256 but only available since PHP 8.1.
*/
public const ALGORITHM = PHP_VERSION_ID >= 80100 ? 'xxh128' : 'sha256';

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Cache\Cache;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\FileHashing;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstantNameHelper;
use PHPStan\ShouldNotHappenException;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function __construct(
*/
private function getCacheKeys(string $file, Identifier $identifier): array
{
$fileHash = hash_file('sha256', $file);
$fileHash = hash_file(FileHashing::ALGORITHM, $file);
if ($fileHash === false) {
throw new CouldNotReadFileException($file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\FileFinder;
use PHPStan\Internal\FileHashing;
use PHPStan\Php\PhpVersion;
use function array_key_exists;
use function array_keys;
Expand All @@ -32,7 +33,7 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
$files = $this->fileFinder->findFiles([$directory])->getFiles();
$fileHashes = [];
foreach ($files as $file) {
$hash = hash_file('sha256', $file);
$hash = hash_file(FileHashing::ALGORITHM, $file);
if ($hash === false) {
continue;
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public function createByFiles(array $files, string $uniqueCacheIdentifier): Opti
{
$fileHashes = [];
foreach ($files as $file) {
$hash = hash_file('sha256', $file);
$hash = hash_file(FileHashing::ALGORITHM, $file);
if ($hash === false) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\DependencyInjection\GenerateFactory;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\FileHashing;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstantNameHelper;
use PHPStan\ShouldNotHappenException;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct(

private function getVariableCacheKey(string $file): string
{
$fileHash = hash_file('sha256', $file);
$fileHash = hash_file(FileHashing::ALGORITHM, $file);
if ($fileHash === false) {
throw new CouldNotReadFileException($file);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\FileHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\FileHashing;
use PHPStan\Parser\Parser;
use PHPStan\PhpDoc\NameScopeAlreadyBeingCreatedException;
use PHPStan\PhpDoc\PhpDocNodeResolver;
Expand Down Expand Up @@ -344,7 +345,7 @@ private function getNameScopeMap(string $fileName): array
[$nameScopeMap, $files] = $this->createPhpDocNodeMap($fileName, null, null, [], $fileName);
$filesWithHashes = [];
foreach ($files as $file) {
$newHash = hash_file('sha256', $file);
$newHash = hash_file(FileHashing::ALGORITHM, $file);
$filesWithHashes[$file] = $newHash;
}
$this->cache->save($cacheKey, $variableCacheKey, [$nameScopeMap, $filesWithHashes]);
Expand Down Expand Up @@ -381,7 +382,7 @@ private function loadCachedPhpDocNodeMap(string $cacheKey, string $variableCache
[$nameScopeMap, $filesWithHashes] = $cached;
$useCache = true;
foreach ($filesWithHashes as $file => $hash) {
$newHash = @hash_file('sha256', $file);
$newHash = @hash_file(FileHashing::ALGORITHM, $file);
if ($newHash === false) {
$useCache = false;
break;
Expand Down
Loading