vendor/friendsofphp/proxy-manager-lts/src/ProxyManager/Generator/Util/IdentifierSuffixer.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ProxyManager\Generator\Util;
  4. use Composer\InstalledVersions;
  5. use function class_exists;
  6. use function preg_match;
  7. use function serialize;
  8. use function sha1;
  9. use function substr;
  10. /**
  11.  * Utility class capable of generating
  12.  * valid class/property/method identifiers
  13.  * with a deterministic attached suffix,
  14.  * in order to prevent property name collisions
  15.  * and tampering from userland
  16.  */
  17. abstract class IdentifierSuffixer
  18. {
  19.     public const VALID_IDENTIFIER_FORMAT '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/';
  20.     public const DEFAULT_IDENTIFIER      'g';
  21.     final private function __construct()
  22.     {
  23.     }
  24.     /**
  25.      * Generates a valid unique identifier from the given name,
  26.      * with a suffix attached to it
  27.      */
  28.     public static function getIdentifier(string $name): string
  29.     {
  30.         /** @var string|null $salt */
  31.         static $salt;
  32.         $salt $salt ?? self::loadBaseHashSalt();
  33.         $suffix substr(sha1($name $salt), 05);
  34.         if (! preg_match(self::VALID_IDENTIFIER_FORMAT$name)) {
  35.             return self::DEFAULT_IDENTIFIER $suffix;
  36.         }
  37.         return $name $suffix;
  38.     }
  39.     private static function loadBaseHashSalt(): string
  40.     {
  41.         if (! class_exists(InstalledVersions::class)) {
  42.             return self::class;
  43.         }
  44.         return sha1(serialize(InstalledVersions::getRawData()));
  45.     }
  46. }