3 namespace Drupal\drupalmoduleupgrader\Utility\Filter;
6 use Pharborist\Functions\FunctionCallNode;
7 use Pharborist\ParentNode;
9 class ContainsLogicFilter {
12 * Function calls which should not be considered logic.
16 protected $whitelist = [];
19 * Pharborist node types which are considered logic.
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',
33 * Specify a function to be whitelisted so that it will not be considered
36 * @param string ... $function
37 * At least one function to add to the whitelist.
39 public function whitelist() {
40 $this->whitelist = array_unique(array_merge($this->whitelist, func_get_args()));
44 * Tests if a function contains logic: any branching operator, function
45 * call, or object instantiation.
47 * @param \Pharborist\ParentNode $node
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);
59 if ($function_calls->isEmpty()) {
60 $filter = call_user_func_array('\Pharborist\Filter::isInstanceOf', static::$logic);
61 return (boolean) $node->find($filter)->count();