function LockFileValidator::getHash

Returns the XXH64 hash of a file.

This method is a thin wrapper around hash_file() to facilitate testing. On failure, hash_file() emits a warning but doesn't throw an exception. In tests, however, PHPUnit converts warnings to exceptions, so we need to catch those and convert them to the value hash_file() will actually return on error, which is FALSE. We could also just call `hash_file` directly and use @ to suppress warnings, but those would be unclear and likely to be accidentally removed later.

Parameters

string $path: Path of the file to hash.

Return value

string|false The hash of the given file, or FALSE if the file doesn't exist or cannot be hashed.

File

core/modules/package_manager/src/Validator/LockFileValidator.php, line 70

Class

LockFileValidator
Checks that the active lock file is unchanged during stage operations.

Namespace

Drupal\package_manager\Validator

Code

private function getHash(string $path) : string|false {
    try {
        return @hash_file('xxh64', $path);
    } catch (\Throwable) {
        return FALSE;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.