[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/Util/ -> UnsetPropertiesGenerator.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator\Util;
   6  
   7  /**
   8   * Generates code necessary to unset all the given properties from a particular given instance string name
   9   *
  10   * @author Marco Pivetta <ocramius@gmail.com>
  11   * @license MIT
  12   */
  13  final class UnsetPropertiesGenerator
  14  {
  15      private static $closureTemplate = <<<'PHP'
  16  \Closure::bind(function (\%s $instance) {
  17      %s
  18  }, $%s, %s)->__invoke($%s);
  19  PHP;
  20  
  21      public static function generateSnippet(Properties $properties, string $instanceName) : string
  22      {
  23          return self::generateUnsetAccessiblePropertiesCode($properties, $instanceName)
  24              . self::generateUnsetPrivatePropertiesCode($properties, $instanceName);
  25      }
  26  
  27      private static function generateUnsetAccessiblePropertiesCode(Properties $properties, string $instanceName) : string
  28      {
  29          $accessibleProperties = $properties->getAccessibleProperties();
  30  
  31          if (! $accessibleProperties) {
  32              return '';
  33          }
  34  
  35          return  self::generateUnsetStatement($accessibleProperties, $instanceName) . "\n\n";
  36      }
  37  
  38      private static function generateUnsetPrivatePropertiesCode(Properties $properties, string $instanceName) : string
  39      {
  40          $groups = $properties->getGroupedPrivateProperties();
  41  
  42          if (! $groups) {
  43              return '';
  44          }
  45  
  46          $unsetClosureCalls = [];
  47  
  48          /* @var $privateProperties \ReflectionProperty[] */
  49          foreach ($groups as $privateProperties) {
  50              /* @var $firstProperty \ReflectionProperty */
  51              $firstProperty  = reset($privateProperties);
  52  
  53              $unsetClosureCalls[] = self::generateUnsetClassPrivatePropertiesBlock(
  54                  $firstProperty->getDeclaringClass(),
  55                  $privateProperties,
  56                  $instanceName
  57              );
  58          }
  59  
  60          return implode("\n\n", $unsetClosureCalls) . "\n\n";
  61      }
  62  
  63      private static function generateUnsetClassPrivatePropertiesBlock(
  64          \ReflectionClass $declaringClass,
  65          array $properties,
  66          string $instanceName
  67      ) : string {
  68          $declaringClassName = $declaringClass->getName();
  69  
  70          return sprintf(
  71              self::$closureTemplate,
  72              $declaringClassName,
  73              self::generateUnsetStatement($properties, 'instance'),
  74              $instanceName,
  75              var_export($declaringClassName, true),
  76              $instanceName
  77          );
  78      }
  79  
  80      private static function generateUnsetStatement(array $properties, string $instanceName) : string
  81      {
  82          return 'unset('
  83              . implode(
  84                  ', ',
  85                  array_map(
  86                      function (\ReflectionProperty $property) use ($instanceName) : string {
  87                          return '$' . $instanceName . '->' . $property->getName();
  88                      },
  89                      $properties
  90                  )
  91              )
  92              . ');';
  93      }
  94  }


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