[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> OptimizeChooseDeadBranches.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
   9  
  10  use DOMElement;
  11  
  12  class OptimizeChooseDeadBranches extends AbstractChooseOptimization
  13  {
  14      /**
  15      * Test whether given XPath expression is always false
  16      *
  17      * @param  string $expr
  18      * @return bool
  19      */
  20  	protected function isAlwaysFalse($expr)
  21      {
  22          // Always false: empty strings, 0, or false()
  23          $regexp = '(^(?:""|\'\'|(?:0*\\.)?0+|false\\s*\\(\\s*\\))$)';
  24  
  25          return (bool) preg_match($regexp, trim($expr));
  26      }
  27  
  28      /**
  29      * Test whether given XPath expression is always true
  30      *
  31      * @param  string $expr
  32      * @return bool
  33      */
  34  	protected function isAlwaysTrue($expr)
  35      {
  36          // Always true: non-empty strings, non-0 numbers, or true()
  37          $regexp = '(^(?:"[^"]++"|\'[^\']++\'|0*[1-9][0-9]*(?:\\.[0-9]*)?|0*\\.0*[1-9][0-9]*|true\\s*\\(\\s*\\))$)';
  38  
  39          return (bool) preg_match($regexp, trim($expr));
  40      }
  41  
  42      /**
  43      * Convert given xsl:when element into an xsl:otherwise element
  44      *
  45      * @param  DOMElement $when
  46      * @return void
  47      */
  48  	protected function makeOtherwise(DOMElement $when)
  49      {
  50          $otherwise = $this->createElement('xsl:otherwise');
  51          while ($when->firstChild)
  52          {
  53              $otherwise->appendChild($when->firstChild);
  54          }
  55  
  56          $when->parentNode->replaceChild($otherwise, $when);
  57      }
  58  
  59      /**
  60      * {@inheritdoc}
  61      */
  62  	protected function optimizeChoose()
  63      {
  64          $removeAll = false;
  65          $tests     = [];
  66          foreach ($this->getBranches() as $branch)
  67          {
  68              $test = trim($branch->getAttribute('test'));
  69  
  70              if ($removeAll || isset($tests[$test]) || $this->isAlwaysFalse($test))
  71              {
  72                  $branch->parentNode->removeChild($branch);
  73              }
  74              elseif ($this->isAlwaysTrue($test))
  75              {
  76                  $removeAll = true;
  77                  $this->makeOtherwise($branch);
  78              }
  79  
  80              $tests[$test] = 1;
  81          }
  82      }
  83  }


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