Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Utility / Filter / ContainsLogicFilter.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Utility\Filter;
4
5 use Pharborist\Filter;
6 use Pharborist\Functions\FunctionCallNode;
7 use Pharborist\ParentNode;
8
9 class ContainsLogicFilter {
10
11   /**
12    * Function calls which should not be considered logic.
13    *
14    * @var string[]
15    */
16   protected $whitelist = [];
17
18   /**
19    * Pharborist node types which are considered logic.
20    *
21    * @var string[]
22    */
23   protected static $logic = [
24     '\Pharborist\ControlStructures\IfNode',
25     '\Pharborist\ControlStructures\SwitchNode',
26     '\Pharborist\Objects\ClassMethodCallNode',
27     '\Pharborist\Objects\ObjectMethodCallNode',
28     '\Pharborist\Objects\NewNode',
29     '\Pharborist\Objects\ClassConstantLookupNode',
30   ];
31
32   /**
33    * Specify a function to be whitelisted so that it will not be considered
34    * logic.
35    *
36    * @param string ... $function
37    *  At least one function to add to the whitelist.
38    */
39   public function whitelist() {
40     $this->whitelist = array_unique(array_merge($this->whitelist, func_get_args()));
41   }
42
43   /**
44    * Tests if a function contains logic: any branching operator, function
45    * call, or object instantiation.
46    *
47    * @param \Pharborist\ParentNode $node
48    *  The node to test.
49    *
50    * @return boolean
51    */
52   public function __invoke(ParentNode $node) {
53     $function_calls = $node
54       ->find(Filter::isInstanceOf('\Pharborist\Functions\FunctionCallNode'))
55       ->not(function(FunctionCallNode $call) {
56         return in_array($call->getName()->getText(), $this->whitelist);
57       });
58
59     if ($function_calls->isEmpty()) {
60       $filter = call_user_func_array('\Pharborist\Filter::isInstanceOf', static::$logic);
61       return (boolean) $node->find($filter)->count();
62     }
63     else {
64       return TRUE;
65     }
66   }
67
68 }