Skip to content

Commit

Permalink
misc: Replace sha1() by hash('sha1', ...)
Browse files Browse the repository at this point in the history
The dedicated `sha1()` function is proposed for deprecation with PHP 8.4:
https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_md5_sha1_md5_file_and_sha1_file

For a future change it might also be considered to move to a cheaper hash
function. The xxHash family (available as `xxh*`) is faster and should be
sufficiently collision resistant for use within the cache.
  • Loading branch information
TimWolla committed Jul 5, 2024
1 parent 2ff1d02 commit 0420306
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Cache/KeySanitizerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\SimpleCache\CacheInterface;
use Traversable;

use function sha1;
use function hash;

/**
* @internal
Expand All @@ -32,10 +32,10 @@ public function __construct(
// 1. We append the current version of the package to the cache key in
// order to avoid collisions between entries from different versions
// of the library.
// 2. The key is sha1'd so that it does not contain illegal characters.
// 2. The key is hashed so that it does not contain illegal characters.
// @see https://www.php-fig.org/psr/psr-16/#12-definitions
// @infection-ignore-all
$this->sanitize = static fn (string $key) => $key . sha1(self::$version ??= PHP_VERSION . '/' . Package::version());
$this->sanitize = static fn (string $key) => $key . hash('sha1', self::$version ??= PHP_VERSION . '/' . Package::version());
}

public function warmup(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use CuyZ\Valinor\Type\ObjectType;
use Psr\SimpleCache\CacheInterface;

use function sha1;
use function hash;

/** @internal */
final class CacheClassDefinitionRepository implements ClassDefinitionRepository
Expand All @@ -23,7 +23,7 @@ public function __construct(
public function for(ObjectType $type): ClassDefinition
{
// @infection-ignore-all
$key = 'class-definition' . sha1($type->toString());
$key = 'class-definition' . hash('sha1', $type->toString());

$entry = $this->cache->get($key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use CuyZ\Valinor\Utility\Reflection\Reflection;
use Psr\SimpleCache\CacheInterface;

use function sha1;
use function hash;

/** @internal */
final class CacheFunctionDefinitionRepository implements FunctionDefinitionRepository
Expand All @@ -25,7 +25,7 @@ public function for(callable $function): FunctionDefinition
$reflection = Reflection::function($function);

// @infection-ignore-all
$key = 'function-definition-' . sha1($reflection->getFileName() . $reflection->getStartLine() . $reflection->getEndLine());
$key = 'function-definition-' . hash('sha1', $reflection->getFileName() . $reflection->getStartLine() . $reflection->getEndLine());

$entry = $this->cache->get($key);

Expand Down

0 comments on commit 0420306

Please sign in to comment.