[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/Assertion/ -> CanProxyAssertion.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator\Assertion;
   6  
   7  use BadMethodCallException;
   8  use ProxyManager\Exception\InvalidProxiedClassException;
   9  use ReflectionClass;
  10  use ReflectionMethod;
  11  
  12  /**
  13   * Assertion that verifies that a class can be proxied
  14   *
  15   * @author Marco Pivetta <ocramius@gmail.com>
  16   * @license MIT
  17   */
  18  final class CanProxyAssertion
  19  {
  20      /**
  21       * Disabled constructor: not meant to be instantiated
  22       *
  23       * @throws BadMethodCallException
  24       */
  25      public function __construct()
  26      {
  27          throw new BadMethodCallException('Unsupported constructor.');
  28      }
  29  
  30      /**
  31       * @param ReflectionClass $originalClass
  32       * @param bool            $allowInterfaces
  33       *
  34       * @throws InvalidProxiedClassException
  35       */
  36      public static function assertClassCanBeProxied(ReflectionClass $originalClass, bool $allowInterfaces = true) : void
  37      {
  38          self::isNotFinal($originalClass);
  39          self::hasNoAbstractProtectedMethods($originalClass);
  40  
  41          if (! $allowInterfaces) {
  42              self::isNotInterface($originalClass);
  43          }
  44      }
  45  
  46      /**
  47       * @param ReflectionClass $originalClass
  48       *
  49       * @throws InvalidProxiedClassException
  50       */
  51      private static function isNotFinal(ReflectionClass $originalClass) : void
  52      {
  53          if ($originalClass->isFinal()) {
  54              throw InvalidProxiedClassException::finalClassNotSupported($originalClass);
  55          }
  56      }
  57  
  58      /**
  59       * @param ReflectionClass $originalClass
  60       *
  61       * @throws InvalidProxiedClassException
  62       */
  63      private static function hasNoAbstractProtectedMethods(ReflectionClass $originalClass) : void
  64      {
  65          $protectedAbstract = array_filter(
  66              $originalClass->getMethods(),
  67              function (ReflectionMethod $method) : bool {
  68                  return $method->isAbstract() && $method->isProtected();
  69              }
  70          );
  71  
  72          if ($protectedAbstract) {
  73              throw InvalidProxiedClassException::abstractProtectedMethodsNotSupported($originalClass);
  74          }
  75      }
  76  
  77      /**
  78       * @param ReflectionClass $originalClass
  79       *
  80       * @throws InvalidProxiedClassException
  81       */
  82      private static function isNotInterface(ReflectionClass $originalClass) : void
  83      {
  84          if ($originalClass->isInterface()) {
  85              throw InvalidProxiedClassException::interfaceNotSupported($originalClass);
  86          }
  87      }
  88  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1