You are here

class ContainsLogicFilter in Drupal 7 to 8/9 Module Upgrader 8

Hierarchy

Expanded class hierarchy of ContainsLogicFilter

6 files declare their use of ContainsLogicFilter
ContainsLogicFilterTest.php in tests/src/Unit/Utility/Filter/ContainsLogicFilterTest.php
ConverterBase.php in src/ConverterBase.php
Functions.php in src/Plugin/DMU/Indexer/Functions.php
Links.php in src/Plugin/DMU/Converter/Links.php
Routing.php in src/Plugin/DMU/Converter/Routing.php

... See full list

File

src/Utility/Filter/ContainsLogicFilter.php, line 9

Namespace

Drupal\drupalmoduleupgrader\Utility\Filter
View source
class ContainsLogicFilter {

  /**
   * Function calls which should not be considered logic.
   *
   * @var string[]
   */
  protected $whitelist = [];

  /**
   * Pharborist node types which are considered logic.
   *
   * @var string[]
   */
  protected static $logic = [
    '\\Pharborist\\ControlStructures\\IfNode',
    '\\Pharborist\\ControlStructures\\SwitchNode',
    '\\Pharborist\\Objects\\ClassMethodCallNode',
    '\\Pharborist\\Objects\\ObjectMethodCallNode',
    '\\Pharborist\\Objects\\NewNode',
    '\\Pharborist\\Objects\\ClassConstantLookupNode',
  ];

  /**
   * Specify a function to be whitelisted so that it will not be considered
   * logic.
   *
   * @param string ... $function
   *   At least one function to add to the whitelist.
   */
  public function whitelist() {
    $this->whitelist = array_unique(array_merge($this->whitelist, func_get_args()));
  }

  /**
   * Tests if a function contains logic: any branching operator, function
   * call, or object instantiation.
   *
   * @param \Pharborist\ParentNode $node
   *   The node to test.
   *
   * @return bool
   */
  public function __invoke(ParentNode $node) {
    $function_calls = $node
      ->find(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionCallNode'))
      ->not(function (FunctionCallNode $call) {
      return in_array($call
        ->getName()
        ->getText(), $this->whitelist);
    });
    if ($function_calls
      ->isEmpty()) {
      $filter = call_user_func_array('\\Pharborist\\Filter::isInstanceOf', static::$logic);
      return (bool) $node
        ->find($filter)
        ->count();
    }
    else {
      return TRUE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainsLogicFilter::$logic protected static property Pharborist node types which are considered logic.
ContainsLogicFilter::$whitelist protected property Function calls which should not be considered logic.
ContainsLogicFilter::whitelist public function Specify a function to be whitelisted so that it will not be considered logic.
ContainsLogicFilter::__invoke public function Tests if a function contains logic: any branching operator, function call, or object instantiation.