vendor/doctrine/deprecations/src/Deprecation.php line 177

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Deprecations;
  4. use Psr\Log\LoggerInterface;
  5. use function array_key_exists;
  6. use function array_reduce;
  7. use function assert;
  8. use function debug_backtrace;
  9. use function sprintf;
  10. use function str_replace;
  11. use function strpos;
  12. use function strrpos;
  13. use function substr;
  14. use function trigger_error;
  15. use const DEBUG_BACKTRACE_IGNORE_ARGS;
  16. use const DIRECTORY_SEPARATOR;
  17. use const E_USER_DEPRECATED;
  18. /**
  19.  * Manages Deprecation logging in different ways.
  20.  *
  21.  * By default triggered exceptions are not logged.
  22.  *
  23.  * To enable different deprecation logging mechanisms you can call the
  24.  * following methods:
  25.  *
  26.  *  - Minimal collection of deprecations via getTriggeredDeprecations()
  27.  *    \Doctrine\Deprecations\Deprecation::enableTrackingDeprecations();
  28.  *
  29.  *  - Uses @trigger_error with E_USER_DEPRECATED
  30.  *    \Doctrine\Deprecations\Deprecation::enableWithTriggerError();
  31.  *
  32.  *  - Sends deprecation messages via a PSR-3 logger
  33.  *    \Doctrine\Deprecations\Deprecation::enableWithPsrLogger($logger);
  34.  *
  35.  * Packages that trigger deprecations should use the `trigger()` or
  36.  * `triggerIfCalledFromOutside()` methods.
  37.  */
  38. class Deprecation
  39. {
  40.     private const TYPE_NONE               0;
  41.     private const TYPE_TRACK_DEPRECATIONS 1;
  42.     private const TYPE_TRIGGER_ERROR      2;
  43.     private const TYPE_PSR_LOGGER         4;
  44.     /** @var int-mask-of<self::TYPE_*>|null */
  45.     private static $type;
  46.     /** @var LoggerInterface|null */
  47.     private static $logger;
  48.     /** @var array<string,bool> */
  49.     private static $ignoredPackages = [];
  50.     /** @var array<string,int> */
  51.     private static $triggeredDeprecations = [];
  52.     /** @var array<string,bool> */
  53.     private static $ignoredLinks = [];
  54.     /** @var bool */
  55.     private static $deduplication true;
  56.     /**
  57.      * Trigger a deprecation for the given package and identfier.
  58.      *
  59.      * The link should point to a Github issue or Wiki entry detailing the
  60.      * deprecation. It is additionally used to de-duplicate the trigger of the
  61.      * same deprecation during a request.
  62.      *
  63.      * @param float|int|string $args
  64.      */
  65.     public static function trigger(string $packagestring $linkstring $message, ...$args): void
  66.     {
  67.         $type self::$type ?? self::getTypeFromEnv();
  68.         if ($type === self::TYPE_NONE) {
  69.             return;
  70.         }
  71.         if (isset(self::$ignoredLinks[$link])) {
  72.             return;
  73.         }
  74.         if (array_key_exists($linkself::$triggeredDeprecations)) {
  75.             self::$triggeredDeprecations[$link]++;
  76.         } else {
  77.             self::$triggeredDeprecations[$link] = 1;
  78.         }
  79.         if (self::$deduplication === true && self::$triggeredDeprecations[$link] > 1) {
  80.             return;
  81.         }
  82.         if (isset(self::$ignoredPackages[$package])) {
  83.             return;
  84.         }
  85.         $backtrace debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS2);
  86.         $message sprintf($message, ...$args);
  87.         self::delegateTriggerToBackend($message$backtrace$link$package);
  88.     }
  89.     /**
  90.      * Trigger a deprecation for the given package and identifier when called from outside.
  91.      *
  92.      * "Outside" means we assume that $package is currently installed as a
  93.      * dependency and the caller is not a file in that package. When $package
  94.      * is installed as a root package then deprecations triggered from the
  95.      * tests folder are also considered "outside".
  96.      *
  97.      * This deprecation method assumes that you are using Composer to install
  98.      * the dependency and are using the default /vendor/ folder and not a
  99.      * Composer plugin to change the install location. The assumption is also
  100.      * that $package is the exact composer packge name.
  101.      *
  102.      * Compared to {@link trigger()} this method causes some overhead when
  103.      * deprecation tracking is enabled even during deduplication, because it
  104.      * needs to call {@link debug_backtrace()}
  105.      *
  106.      * @param float|int|string $args
  107.      */
  108.     public static function triggerIfCalledFromOutside(string $packagestring $linkstring $message, ...$args): void
  109.     {
  110.         $type self::$type ?? self::getTypeFromEnv();
  111.         if ($type === self::TYPE_NONE) {
  112.             return;
  113.         }
  114.         $backtrace debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS2);
  115.         // first check that the caller is not from a tests folder, in which case we always let deprecations pass
  116.         if (isset($backtrace[1]['file'], $backtrace[0]['file']) && strpos($backtrace[1]['file'], DIRECTORY_SEPARATOR 'tests' DIRECTORY_SEPARATOR) === false) {
  117.             $path DIRECTORY_SEPARATOR 'vendor' DIRECTORY_SEPARATOR str_replace('/'DIRECTORY_SEPARATOR$package) . DIRECTORY_SEPARATOR;
  118.             if (strpos($backtrace[0]['file'], $path) === false) {
  119.                 return;
  120.             }
  121.             if (strpos($backtrace[1]['file'], $path) !== false) {
  122.                 return;
  123.             }
  124.         }
  125.         if (isset(self::$ignoredLinks[$link])) {
  126.             return;
  127.         }
  128.         if (array_key_exists($linkself::$triggeredDeprecations)) {
  129.             self::$triggeredDeprecations[$link]++;
  130.         } else {
  131.             self::$triggeredDeprecations[$link] = 1;
  132.         }
  133.         if (self::$deduplication === true && self::$triggeredDeprecations[$link] > 1) {
  134.             return;
  135.         }
  136.         if (isset(self::$ignoredPackages[$package])) {
  137.             return;
  138.         }
  139.         $message sprintf($message, ...$args);
  140.         self::delegateTriggerToBackend($message$backtrace$link$package);
  141.     }
  142.     /** @param list<array{function: string, line?: int, file?: string, class?: class-string, type?: string, args?: mixed[], object?: object}> $backtrace */
  143.     private static function delegateTriggerToBackend(string $message, array $backtracestring $linkstring $package): void
  144.     {
  145.         $type self::$type ?? self::getTypeFromEnv();
  146.         if (($type self::TYPE_PSR_LOGGER) > 0) {
  147.             $context = [
  148.                 'file' => $backtrace[0]['file'] ?? null,
  149.                 'line' => $backtrace[0]['line'] ?? null,
  150.                 'package' => $package,
  151.                 'link' => $link,
  152.             ];
  153.             assert(self::$logger !== null);
  154.             self::$logger->notice($message$context);
  155.         }
  156.         if (! (($type self::TYPE_TRIGGER_ERROR) > 0)) {
  157.             return;
  158.         }
  159.         $message .= sprintf(
  160.             ' (%s:%d called by %s:%d, %s, package %s)',
  161.             self::basename($backtrace[0]['file'] ?? 'native code'),
  162.             $backtrace[0]['line'] ?? 0,
  163.             self::basename($backtrace[1]['file'] ?? 'native code'),
  164.             $backtrace[1]['line'] ?? 0,
  165.             $link,
  166.             $package
  167.         );
  168.         @trigger_error($messageE_USER_DEPRECATED);
  169.     }
  170.     /**
  171.      * A non-local-aware version of PHPs basename function.
  172.      */
  173.     private static function basename(string $filename): string
  174.     {
  175.         $pos strrpos($filenameDIRECTORY_SEPARATOR);
  176.         if ($pos === false) {
  177.             return $filename;
  178.         }
  179.         return substr($filename$pos 1);
  180.     }
  181.     public static function enableTrackingDeprecations(): void
  182.     {
  183.         self::$type  self::$type ?? self::getTypeFromEnv();
  184.         self::$type |= self::TYPE_TRACK_DEPRECATIONS;
  185.     }
  186.     public static function enableWithTriggerError(): void
  187.     {
  188.         self::$type  self::$type ?? self::getTypeFromEnv();
  189.         self::$type |= self::TYPE_TRIGGER_ERROR;
  190.     }
  191.     public static function enableWithPsrLogger(LoggerInterface $logger): void
  192.     {
  193.         self::$type   self::$type ?? self::getTypeFromEnv();
  194.         self::$type  |= self::TYPE_PSR_LOGGER;
  195.         self::$logger $logger;
  196.     }
  197.     public static function withoutDeduplication(): void
  198.     {
  199.         self::$deduplication false;
  200.     }
  201.     public static function disable(): void
  202.     {
  203.         self::$type          self::TYPE_NONE;
  204.         self::$logger        null;
  205.         self::$deduplication true;
  206.         self::$ignoredLinks  = [];
  207.         foreach (self::$triggeredDeprecations as $link => $count) {
  208.             self::$triggeredDeprecations[$link] = 0;
  209.         }
  210.     }
  211.     public static function ignorePackage(string $packageName): void
  212.     {
  213.         self::$ignoredPackages[$packageName] = true;
  214.     }
  215.     public static function ignoreDeprecations(string ...$links): void
  216.     {
  217.         foreach ($links as $link) {
  218.             self::$ignoredLinks[$link] = true;
  219.         }
  220.     }
  221.     public static function getUniqueTriggeredDeprecationsCount(): int
  222.     {
  223.         return array_reduce(self::$triggeredDeprecations, static function (int $carryint $count) {
  224.             return $carry $count;
  225.         }, 0);
  226.     }
  227.     /**
  228.      * Returns each triggered deprecation link identifier and the amount of occurrences.
  229.      *
  230.      * @return array<string,int>
  231.      */
  232.     public static function getTriggeredDeprecations(): array
  233.     {
  234.         return self::$triggeredDeprecations;
  235.     }
  236.     /** @return int-mask-of<self::TYPE_*> */
  237.     private static function getTypeFromEnv(): int
  238.     {
  239.         switch ($_SERVER['DOCTRINE_DEPRECATIONS'] ?? $_ENV['DOCTRINE_DEPRECATIONS'] ?? null) {
  240.             case 'trigger':
  241.                 self::$type self::TYPE_TRIGGER_ERROR;
  242.                 break;
  243.             case 'track':
  244.                 self::$type self::TYPE_TRACK_DEPRECATIONS;
  245.                 break;
  246.             default:
  247.                 self::$type self::TYPE_NONE;
  248.                 break;
  249.         }
  250.         return self::$type;
  251.     }
  252. }