Example #1
0
 /**
  * Returns the root property.
  *
  * For example, given an expression like $foo->bar->baz this method will
  * return the identifier (T_STRING TokenNode) 'bar'.
  *
  * @return Node
  *   The node for the root property.
  */
 public function getRootProperty()
 {
     if ($this->object instanceof ObjectPropertyNode) {
         return $this->object->getRootProperty();
     } else {
         return $this->property;
     }
 }
Example #2
0
 /**
  * @param string|Node $method_name
  * @return $this
  */
 public function setMethodName($method_name)
 {
     if (is_string($method_name)) {
         $method_name = Token::identifier($method_name);
     }
     $this->methodName->replaceWith($method_name);
     $this->methodName = $method_name;
     return $this;
 }
 /**
  * @return boolean
  */
 public function __invoke(Node $node)
 {
     if ($node instanceof ArrayLookupNode) {
         $root = $node->getRootArray();
         if ($root instanceof ObjectPropertyNode) {
             $object = $root->getObject();
             if ($object instanceof VariableNode && $object->getName() == $this->variable) {
                 return sizeof($node->getKeys()) >= 3;
             }
         }
     }
     return FALSE;
 }
Example #4
0
 private function updateArray(ArrayNode $node, $data)
 {
     $i = 0;
     foreach ($node->getElements() as $el) {
         if ($el instanceof ArrayPairNode) {
             $k = (string) $el->getKey();
             $k = trim($k, '"\'');
             $v = $el->getValue();
             if (!isset($data[$k])) {
                 $this->cleanAround($el);
             } else {
                 if ($v instanceof ArrayNode && is_array($data[$k])) {
                     $v = $this->updateArray($v, $data[$k]);
                     unset($data[$k]);
                 } else {
                     $v->replaceWith(Parser::parseExpression($data[$k]));
                     unset($data[$k]);
                 }
             }
         } elseif ($el instanceof ArrayNode && (!isset($data[$i]) || is_array($data[$i]))) {
             if (!isset($data[$i])) {
                 $this->cleanAround($el);
             } else {
                 $this->updateArray($el, $data[$i]);
                 unset($data[$i]);
                 $i++;
             }
         } else {
             if (!isset($data[$i])) {
                 $this->cleanAround($el);
             } else {
                 $el->replaceWith(Parser::parseExpression($data[$i]));
                 unset($data[$i]);
                 $i++;
             }
         }
     }
     foreach ($data as $key => $val) {
         $v = Parser::parseExpression(self::var_codify($val));
         if (!is_integer($key)) {
             $v = ArrayPairNode::create(Node::fromValue($key), $v);
         }
         $comma = false;
         $list = $node->getElementList();
         $children = [];
         foreach ($list->children() as $child) {
             $children[] = $child;
         }
         $prev = end($children);
         if ($prev) {
             do {
                 if ((string) $prev === ',') {
                     $comma = true;
                     break;
                 }
             } while (is_object($prev) && ($prev = $prev->previous()) instanceof WhitespaceNode);
         } else {
             $comma = true;
         }
         $indent = 0;
         $prev = end($children);
         while ($prev && strpos($prev, "\n") === false) {
             $prev = $prev->previous();
         }
         $indent = '';
         if ($prev) {
             $prev = explode("\n", (string) $prev);
             $prev = array_pop($prev);
             for ($i = 0; $i < strlen($prev); $i++) {
                 if (in_array($prev[$i], ["\t", ' '])) {
                     $indent .= $prev[$i];
                 } else {
                     break;
                 }
             }
         }
         if (!$comma) {
             $list->append(Token::comma());
         }
         $list->append(Token::newline());
         if ($indent) {
             $list->append(WhitespaceNode::create($indent));
         }
         $list->append($v);
     }
 }
Example #5
0
 public function matchReflector(\ReflectionParameter $reflector)
 {
     $this->setReference($reflector->isPassedByReference());
     // Match the reflector's type hint.
     if ($reflector->isArray()) {
         $this->setTypeHint('array');
     } elseif ($reflector->isCallable()) {
         $this->setTypeHint('callable');
     } elseif ($class = $reflector->getClass()) {
         $this->setTypeHint($class->getName());
     }
     // Match the reflector's default value, if there is one. It will be a
     // scalar value, an array of scalar values, or a constant.
     if ($reflector->isDefaultValueAvailable()) {
         if ($reflector->isDefaultValueConstant()) {
             $this->setValue(ConstantNode::create($reflector->getDefaultValueConstantName()));
         } else {
             $this->setValue(Node::fromValue($reflector->getDefaultValue()));
         }
     }
     return $this;
 }
Example #6
0
 /**
  * Calculate the column start position of the node.
  *
  * @param Node $node
  *   Node to calculate column position for.
  *
  * @return int
  *   Column position.
  */
 protected function calculateColumnPosition(Node $node)
 {
     // Add tokens until have whitespace containing newline.
     $column_position = 1;
     $start_token = $node instanceof ParentNode ? $node->firstToken() : $node;
     $token = $start_token;
     while ($token = $token->previousToken()) {
         if ($token instanceof WhitespaceNode && $token->getNewlineCount() > 0) {
             $lines = explode($this->config['nl'], $token->getText());
             $last_line = end($lines);
             $column_position += strlen($last_line);
             break;
         }
         $column_position += strlen($token->getText());
     }
     return $column_position;
 }
 /**
  * Determines which currently-open file a node belongs to, if any. Nodes
  * which are not part of any open syntax tree will return NULL.
  *
  * @return string|NULL
  */
 public function getFileOf(Node $node)
 {
     if ($node instanceof RootNode) {
         $root = $node;
     } else {
         $parents = $node->parents();
         if ($parents->isEmpty()) {
             return NULL;
         }
         $root = $parents->get(0);
     }
     foreach ($this->documents as $file => $doc) {
         if ($root === $doc) {
             return $file;
         }
     }
 }
 /**
  * Checks if a field lookup key is translated. This will be TRUE unless one
  * of the following conditions applies:
  *
  * - The key is the Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED
  *   constant.
  * - The key is the LANGUAGE_NONE constant from Drupal 7.
  * - The key is the string 'und'.
  *
  * @param Node $key
  *  The key to check.
  *
  * @return boolean
  */
 public static function isTranslation(Node $key)
 {
     if ($key instanceof ClassConstantLookupNode) {
         $constant = $key->getClassName() . '::' . $key->getConstantName();
         return $constant != '\\Drupal\\Core\\Language\\Language::LANGCODE_NOT_SPECIFIED';
     } elseif ($key instanceof ConstantNode) {
         return $key->getConstantName() != 'LANGUAGE_NONE';
     } elseif ($key instanceof StringNode) {
         return $key->toValue() != 'und';
     } else {
         return TRUE;
     }
 }
Example #9
0
 /**
  * Returns the root of the lookup.
  *
  * For example, given an expression like $foo['bar']['baz'],
  * this method will return a VariableNode for $foo.
  *
  * @return Node
  */
 public function getRootArray()
 {
     return $this->array instanceof ArrayLookupNode ? $this->array->getRootArray() : $this->array;
 }
 /**
  * Tests if the given node is on the left side of an assignment.
  *
  * @param \Pharborist\Node $node
  *  The node to test.
  *
  * @return boolean
  */
 public function __invoke(Node $node)
 {
     /** @var \Pharborist\Operators\AssignNode $assignment */
     $assignment = $node->closest(Filter::isInstanceOf('\\Pharborist\\Operators\\AssignNode'));
     return $assignment ? $assignment->getLeftOperand() === $node : FALSE;
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function addViolation(Node $node, AnalyzerInterface $detector)
 {
     $file = $node->getFilename();
     if ($file) {
         $this->violations[$file][] = ['line_number' => $node->getLineNumber()];
     } else {
         throw new \DomainException('Cannot record an issue violation from a detached node.');
     }
     $this->addDetector($detector);
     return $this;
 }
 /**
  * @return boolean
  */
 public function hasArgument(Node $argument)
 {
     return $argument instanceof VariableNode && $argument->getName() == $this->variable;
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testFromScalarInvalidArgument()
 {
     $node = Node::fromValue(new \stdClass());
 }
 private function toScalar(Node $node)
 {
     return $node->toValue();
 }