Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ChangedFilesDetector #6662

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 2 additions & 10 deletions src/Caching/Detector/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function cacheFile(string $filePath): void
return;
}

$hash = $this->hashFile($filePath);

$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash);
$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $filePathCacheKey);
}

public function addCachableFile(string $filePath): void
Expand All @@ -53,8 +51,7 @@ public function hasFileChanged(string $filePath): bool
$cachedValue = $this->cache->load($fileInfoCacheKey, CacheKey::FILE_HASH_KEY);

if ($cachedValue !== null) {
$currentFileHash = $this->hashFile($filePath);
return $currentFileHash !== $cachedValue;
return $fileInfoCacheKey !== $cachedValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileInfoCacheKey is cache key, while file hash is hashed of file, seems different

Copy link
Contributor Author

@staabm staabm Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has the same content though - see the impl is equivalent

private function getFilePathCacheKey(string $filePath): string
{
return $this->fileHasher->hash($this->resolvePath($filePath));
}
private function hashFile(string $filePath): string
{
return $this->fileHasher->hashFiles([$this->resolvePath($filePath)]);
}

both just contain the hash of the file-path-content

Copy link
Member

@samsonasik samsonasik Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the hash() method using hash() function.

public function hash(string $string): string
{
return hash($this->getAlgo(), $string);

  • the hashFiles() method using hash_file() function.

public function hashFiles(array $files): string
{
$configHash = '';
$algo = $this->getAlgo();
foreach ($files as $file) {
$hash = hash_file($algo, $file);

so the usage seems different ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested that hash() have different result vs hash_file(), see below:

➜  rector-src git:(main) php -a
Interactive shell

php > echo hash('xxh128', 'vendor/autoload.php');
ed4ee810129ade2015db3142c912be83

php > echo hash_file('xxh128', 'vendor/autoload.php');
d69f0ce7c725ec2c59342d08f047a9ad

so the change seems different.

}

// we don't have a value to compare against. Be defensive and assume its changed
Expand Down Expand Up @@ -98,11 +95,6 @@ private function getFilePathCacheKey(string $filePath): string
return $this->fileHasher->hash($this->resolvePath($filePath));
}

private function hashFile(string $filePath): string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method did the same thing as getFilePathCacheKey, therefore just drop it and save unnecessary re-hash'ing of files, we already hashed before

{
return $this->fileHasher->hashFiles([$this->resolvePath($filePath)]);
}

private function storeConfigurationDataHash(string $filePath, string $configurationHash): void
{
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . $this->getFilePathCacheKey($filePath);
Expand Down
Loading