public function testCreate()
 {
     $expr = Parser::parseExpression('empty($foo)');
     $not = BooleanNotNode::fromExpression($expr);
     $this->assertInstanceOf('\\Pharborist\\Operators\\BooleanNotNode', $not);
     $this->assertSame($expr, $not->getOperand());
     $this->assertEquals('!empty($foo)', $not->getText());
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(ParameterNode $parameter)
 {
     // Don't even try to rewrite the function if the parameter is reassigned.
     if ($this->isReassigned($parameter)) {
         $error = $this->t('@function() cannot be parametrically rewritten because @parameter is reassigned.', ['@parameter' => $parameter->getName(), '@function' => $parameter->getFunction()->getName()->getText()]);
         throw new \LogicException($error);
     }
     foreach ($this->getExpressions($parameter)->not($this->isAssigned) as $expr) {
         $property = $this->getProperty($expr);
         if (empty($property)) {
             continue;
         }
         $getter = $this->rewriteAsGetter($expr, $property);
         if ($getter) {
             $empty = $expr->closest(Filter::isFunctionCall('empty', 'isset'));
             // If the original expression was wrapped by a call to isset() or
             // empty(), we need to replace it entirely.
             if ($getter instanceof CallNode && $empty instanceof CallNode) {
                 // If the isset() or empty() call was negated, reverse that logic.
                 $parent = $empty->parent();
                 if ($parent instanceof BooleanNotNode) {
                     $parent->replaceWith($getter);
                 } else {
                     $empty->replaceWith(BooleanNotNode::fromExpression($getter));
                 }
             } else {
                 $expr->replaceWith($getter);
             }
         }
     }
     foreach ($this->getExpressions($parameter)->filter($this->isAssigned) as $expr) {
         // If the property cannot be determined, don't even try to rewrite the
         // expression.
         $property = $this->getProperty($expr);
         if (empty($property)) {
             continue;
         }
         $assignment = $expr->closest(Filter::isInstanceOf('\\Pharborist\\Operators\\AssignNode'));
         $setter = $this->rewriteAsSetter($expr, $property, $assignment);
         if ($setter) {
             $assignment->replaceWith($setter);
         }
     }
     // Set the type hint, if one is defined.
     if (isset($this->pluginDefinition['type_hint'])) {
         $parameter->setTypeHint($this->pluginDefinition['type_hint']);
         // If the type hint extends FieldableEntityInterface, rewrite any field
         // lookups (e.g. $node->body[LANGUAGE_NONE][0]['value']).
         if (in_array('Drupal\\Core\\Entity\\FieldableEntityInterface', class_implements($this->pluginDefinition['type_hint']))) {
             $filter = new FieldValueFilter($parameter->getName());
             foreach ($parameter->getFunction()->find($filter) as $lookup) {
                 $lookup->replaceWith(self::rewriteFieldLookup($lookup));
             }
         }
     }
 }