[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

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

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  declare(strict_types=1);
  20  
  21  namespace ProxyManager\ProxyGenerator\Util;
  22  
  23  /**
  24   * Generates code necessary to unset all the given properties from a particular given instance string name
  25   *
  26   * @author Marco Pivetta <ocramius@gmail.com>
  27   * @license MIT
  28   */
  29  final class UnsetPropertiesGenerator
  30  {
  31      private static $closureTemplate = <<<'PHP'
  32  \Closure::bind(function (\%s $instance) {
  33      %s
  34  }, $%s, %s)->__invoke($%s);
  35  PHP;
  36  
  37      public static function generateSnippet(Properties $properties, string $instanceName) : string
  38      {
  39          return self::generateUnsetAccessiblePropertiesCode($properties, $instanceName)
  40              . self::generateUnsetPrivatePropertiesCode($properties, $instanceName);
  41      }
  42  
  43      private static function generateUnsetAccessiblePropertiesCode(Properties $properties, string $instanceName) : string
  44      {
  45          $accessibleProperties = $properties->getAccessibleProperties();
  46  
  47          if (! $accessibleProperties) {
  48              return '';
  49          }
  50  
  51          return  self::generateUnsetStatement($accessibleProperties, $instanceName) . "\n\n";
  52      }
  53  
  54      private static function generateUnsetPrivatePropertiesCode(Properties $properties, string $instanceName) : string
  55      {
  56          $groups = $properties->getGroupedPrivateProperties();
  57  
  58          if (! $groups) {
  59              return '';
  60          }
  61  
  62          $unsetClosureCalls = [];
  63  
  64          /* @var $privateProperties \ReflectionProperty[] */
  65          foreach ($groups as $privateProperties) {
  66              /* @var $firstProperty \ReflectionProperty */
  67              $firstProperty  = reset($privateProperties);
  68  
  69              $unsetClosureCalls[] = self::generateUnsetClassPrivatePropertiesBlock(
  70                  $firstProperty->getDeclaringClass(),
  71                  $privateProperties,
  72                  $instanceName
  73              );
  74          }
  75  
  76          return implode("\n\n", $unsetClosureCalls) . "\n\n";
  77      }
  78  
  79      private static function generateUnsetClassPrivatePropertiesBlock(
  80          \ReflectionClass $declaringClass,
  81          array $properties,
  82          string $instanceName
  83      ) : string {
  84          $declaringClassName = $declaringClass->getName();
  85  
  86          return sprintf(
  87              self::$closureTemplate,
  88              $declaringClassName,
  89              self::generateUnsetStatement($properties, 'instance'),
  90              $instanceName,
  91              var_export($declaringClassName, true),
  92              $instanceName
  93          );
  94      }
  95  
  96      private static function generateUnsetStatement(array $properties, string $instanceName) : string
  97      {
  98          return 'unset('
  99              . implode(
 100                  ', ',
 101                  array_map(
 102                      function (\ReflectionProperty $property) use ($instanceName) : string {
 103                          return '$' . $instanceName . '->' . $property->getName();
 104                      },
 105                      $properties
 106                  )
 107              )
 108              . ');';
 109      }
 110  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1