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

Simplify ChangedFilesDetector #6662

wants to merge 1 commit into from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jan 9, 2025

remove unnecessary/redundant IO operations.

drive-by cleanup while looking into rectorphp/rector#8966

@@ -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

@staabm staabm marked this pull request as ready for review January 9, 2025 10:31
@@ -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.

@samsonasik
Copy link
Member

samsonasik commented Jan 9, 2025

I am closing this as hash() vs hash_file() has different result, which seems on purpose to get key path vs value content of file, see

#6662 (comment)

@samsonasik samsonasik closed this Jan 9, 2025
@staabm staabm deleted the simpl branch January 9, 2025 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants