/** * @depends testConstruct */ public function testGetDocComment(PHPParser_Node $node) { $this->assertEquals('/** doc comment */', $node->getDocComment()); array_pop($node->getAttribute('comments')); // remove doc comment $this->assertNull($node->getDocComment()); array_pop($node->getAttribute('comments')); // remove comment $this->assertNull($node->getDocComment()); }
/** * @depends testConstruct */ public function testChange(PHPParser_Node $node) { // change of line $node->setLine(15); $this->assertEquals(15, $node->getLine()); // change of doc comment $node->setDocComment('/** other doc comment */'); $this->assertEquals('/** other doc comment */', $node->getDocComment()); // direct modification $node->subNode = 'newValue'; $this->assertEquals('newValue', $node->subNode); // indirect modification $subNode =& $node->subNode; $subNode = 'newNewValue'; $this->assertEquals('newNewValue', $node->subNode); // removal unset($node->subNode); $this->assertFalse(isset($node->subNode)); }
private function getDocCommentForNode(\PHPParser_Node $node) { // check if there is a doc comment for the ID argument // ->trans(/** @Desc("FOO") */ 'my.id') if (null !== ($comment = $node->args[0]->getDocComment())) { return $comment->getText(); } // this may be placed somewhere up in the hierarchy, // -> /** @Desc("FOO") */ trans('my.id') // /** @Desc("FOO") */ ->trans('my.id') // /** @Desc("FOO") */ $translator->trans('my.id') if (null !== ($comment = $node->getDocComment())) { return $comment->getText(); } elseif (null !== $this->previousNode && $this->previousNode->getDocComment() !== null) { $comment = $this->previousNode->getDocComment(); return is_object($comment) ? $comment->getText() : $comment; } return null; }
public function enterNode(\PHPParser_Node $node) { if ($node instanceof \PHPParser_Node_Stmt_Namespace) { $this->namespace = implode('\\', $node->name->parts); return; } if ($node instanceof \PHPParser_Node_Stmt_Class) { $name = '' === $this->namespace ? $node->name : $this->namespace . '\\' . $node->name; if (!class_exists($name)) { return; } $ref = new \ReflectionClass($name); if (!$ref->isSubclassOf('Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException') && $ref->name !== 'Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException') { return; } if (!$ref->hasMethod('getMessageKey')) { return; } $this->inAuthException = true; return; } if (!$this->inAuthException) { return; } if ($node instanceof \PHPParser_Node_Stmt_ClassMethod) { if ('getmessagekey' === strtolower($node->name)) { $this->inGetMessageKey = true; } return; } if (!$this->inGetMessageKey) { return; } if (!$node instanceof \PHPParser_Node_Stmt_Return) { return; } $ignore = false; $desc = $meaning = null; if ($docComment = $node->getDocComment()) { foreach ($this->docParser->parse($docComment->getText(), 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) { if ($annot instanceof Ignore) { $ignore = true; } else { if ($annot instanceof Desc) { $desc = $annot->text; } else { if ($annot instanceof Meaning) { $meaning = $annot->text; } } } } } if (!$node->expr instanceof \PHPParser_Node_Scalar_String) { if ($ignore) { return; } $message = sprintf('Could not extract id from return value, expected scalar string but got %s (in %s on line %d).', get_class($node->expr), $this->file, $node->expr->getLine()); if ($this->logger) { $this->logger->err($message); return; } throw new RuntimeException($message); } $message = Message::create($node->expr->value, $this->domain)->setDesc($desc)->setMeaning($meaning)->addSource(new FileSource((string) $this->file, $node->expr->getLine())); $this->catalogue->add($message); }
public function leaveNode(\PHPParser_Node $node) { switch (true) { case $node instanceof \PHPParser_Node_Stmt_Function: case $node instanceof \PHPParser_Node_Stmt_ClassMethod: if (0 === count($node->stmts)) { break; } if (null === ($comment = $node->getDocComment()) || !preg_match('#@Assertions\\(([0-9]+)\\)#', $comment, $match)) { $this->testCase->fail(sprintf('Each method, and function must have an @Assertions() annotation, but found none on line %d.', $node->getLine())); } $this->testCase->assertEquals($match[1], $this->curAssertions, sprintf('The number of expected assertions does not equal the actually performed number of assertions for function/method on line %d.', $node->getLine())); break; } }
public function getTypeFromReturnAnnotation(\PHPParser_Node $node, $asString = false) { if (false === stripos($comment = $node->getDocComment(), '@return')) { return null; } if (!preg_match('/@return\\s+([^\\s]+)/i', $comment, $match)) { return null; } $this->currentComment = $comment; if (null === ($resolvedType = $this->tryGettingType($match[1]))) { return null; } if ($asString) { if ($this->isThisReference(strtolower($match[1]))) { return $match[1]; } // We do return fully qualified names. return $resolvedType->getDocType(); } return $resolvedType; }
private function getDocCommentForNode(\PHPParser_Node $node) { // check if there is a doc comment for the ID argument // ->trans(/** @Desc("FOO") */ 'my.id') if (null !== ($comment = $node->args[0]->getDocComment())) { return $comment; } // this may be placed somewhere up in the hierarchy, // -> /** @Desc("FOO") */ trans('my.id') // /** @Desc("FOO") */ ->trans('my.id') // /** @Desc("FOO") */ $translator->trans('my.id') return $node->getDocComment(); }
private function traverse(\PHPParser_Node $node, LinkedFlowScope $scope) { if (null !== ($docComment = $node->getDocComment())) { foreach ($this->commentParser->getTypesFromInlineComment($docComment) as $name => $type) { $scope->inferSlotType($name, $type); } } switch (true) { case $node instanceof \PHPParser_Node_Expr_Clone: $scope = $this->traverseClone($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_Assign: case $node instanceof \PHPParser_Node_Expr_AssignRef: $scope = $this->traverseAssign($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_AssignList: $scope = $this->traverseAssignList($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_StaticPropertyFetch: case $node instanceof \PHPParser_Node_Expr_PropertyFetch: $scope = $this->traverseGetProp($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_LogicalAnd: case $node instanceof \PHPParser_Node_Expr_BooleanAnd: $scope = $this->traverseAnd($node, $scope)->getJoinedFlowScope()->createChildFlowScope(); break; case $node instanceof \PHPParser_Node_Expr_Array: $scope = $this->traverseArray($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_LogicalOr: case $node instanceof \PHPParser_Node_Expr_BooleanOr: $scope = $this->traverseOr($node, $scope)->getJoinedFlowScope()->createChildFlowScope(); break; case $node instanceof \PHPParser_Node_Expr_Ternary: $scope = $this->traverseTernary($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_FuncCall: case $node instanceof \PHPParser_Node_Expr_MethodCall: case $node instanceof \PHPParser_Node_Expr_StaticCall: $scope = $this->traverseCall($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_New: $scope = $this->traverseNew($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_UnaryPlus: case $node instanceof \PHPParser_Node_Expr_UnaryMinus: $scope = $this->traverseUnaryPlusMinus($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_BooleanNot: $scope = $this->traverse($node->expr, $scope); $node->setAttribute('type', $this->typeRegistry->getNativeType('boolean')); break; case $node instanceof \PHPParser_Node_Expr_Identical: case $node instanceof \PHPParser_Node_Expr_Equal: case $node instanceof \PHPParser_Node_Expr_Greater: case $node instanceof \PHPParser_Node_Expr_GreaterOrEqual: case $node instanceof \PHPParser_Node_Expr_NotEqual: case $node instanceof \PHPParser_Node_Expr_NotIdentical: case $node instanceof \PHPParser_Node_Expr_Smaller: case $node instanceof \PHPParser_Node_Expr_SmallerOrEqual: case $node instanceof \PHPParser_Node_Expr_LogicalXor: case $node instanceof \PHPParser_Node_Expr_Empty: case $node instanceof \PHPParser_Node_Expr_Cast_Bool: case $node instanceof \PHPParser_Node_Expr_Isset: case $node instanceof \PHPParser_Node_Expr_Instanceof: $scope = $this->traverseChildren($node, $scope); $node->setAttribute('type', $this->typeRegistry->getNativeType('boolean')); break; case $node instanceof \PHPParser_Node_Expr_Cast_Int: case $node instanceof \PHPParser_Node_Expr_Mod: case $node instanceof \PHPParser_Node_Expr_BitwiseXor: case $node instanceof \PHPParser_Node_Expr_BitwiseOr: case $node instanceof \PHPParser_Node_Expr_BitwiseNot: case $node instanceof \PHPParser_Node_Expr_BitwiseAnd: case $node instanceof \PHPParser_Node_Expr_ShiftLeft: case $node instanceof \PHPParser_Node_Expr_ShiftRight: $scope = $this->traverseChildren($node, $scope); $node->setAttribute('type', $this->typeRegistry->getNativeType('integer')); break; case $node instanceof \PHPParser_Node_Expr_Cast_Double: $scope = $this->traverseChildren($node, $scope); $node->setAttribute('type', $this->typeRegistry->getNativeType('double')); break; case $node instanceof \PHPParser_Node_Expr_AssignPlus: $scope = $this->traversePlus($node, $node->var, $node->expr, $scope); break; case $node instanceof \PHPParser_Node_Expr_Plus: $scope = $this->traversePlus($node, $node->left, $node->right, $scope); break; case $node instanceof \PHPParser_Node_Expr_AssignDiv: case $node instanceof \PHPParser_Node_Expr_AssignMinus: case $node instanceof \PHPParser_Node_Expr_AssignMul: case $node instanceof \PHPParser_Node_Expr_Div: case $node instanceof \PHPParser_Node_Expr_Minus: case $node instanceof \PHPParser_Node_Expr_Mul: $scope = $this->traverseChildren($node, $scope); if (isset($node->left, $node->right)) { $left = $node->left; $right = $node->right; } else { if (isset($node->var, $node->expr)) { $left = $node->var; $right = $node->expr; } else { throw new \LogicException('Previous statements were exhaustive.'); } } if ($this->getType($left)->isIntegerType() && $this->getType($right)->isIntegerType()) { $type = $this->typeRegistry->getNativeType('integer'); } else { if ($this->getType($left)->isDoubleType() || $this->getType($right)->isDoubleType()) { $type = $this->typeRegistry->getNativeType('double'); } else { $type = $this->typeRegistry->getNativeType('number'); } } $node->setAttribute('type', $type); $this->updateScopeForTypeChange($scope, $left, $this->getType($left), $type); break; case $node instanceof \PHPParser_Node_Expr_PostDec: case $node instanceof \PHPParser_Node_Expr_PostInc: case $node instanceof \PHPParser_Node_Expr_PreDec: case $node instanceof \PHPParser_Node_Expr_PreInc: $scope = $this->traverseChildren($node, $scope); if ($this->getType($node->var)->isIntegerType()) { $node->setAttribute('type', $this->typeRegistry->getNativeType('integer')); } else { if ($this->getType($node->var)->isDoubleType()) { $node->setAttribute('type', $this->typeRegistry->getNativeType('double')); } else { $node->setAttribute('type', $this->typeRegistry->getNativeType('number')); } } break; case $node instanceof \PHPParser_Node_Expr_Cast_String: case $node instanceof \PHPParser_Node_Expr_Concat: $scope = $this->traverseChildren($node, $scope); $node->setAttribute('type', $this->typeRegistry->getNativeType('string')); break; case $node instanceof \PHPParser_Node_Stmt_Return: $scope = $this->traverseReturn($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_Variable: $scope = $this->traverseVariable($node, $scope); break; case $node instanceof \PHPParser_Node_Stmt_Echo: case $node instanceof \PHPParser_Node_Expr_ArrayItem: case $node instanceof \PHPParser_Node_Stmt_Throw: $scope = $this->traverseChildren($node, $scope); break; case $node instanceof \PHPParser_Node_Arg: $scope = $this->traverseChildren($node, $scope); $node->setAttribute('type', $node->value->getAttribute('type')); break; case $node instanceof \PHPParser_Node_Stmt_Switch: $scope = $this->traverse($node->cond, $scope); break; case $node instanceof \PHPParser_Node_Stmt_Catch: $scope = $this->traverseCatch($node, $scope); break; case $node instanceof \PHPParser_Node_Expr_ArrayDimFetch: $scope = $this->traverseArrayDimFetch($node, $scope); break; } return $scope; }
private function getDocComment(\PHPParser_Node $node) { $docComment = $node->getDocComment(); if (empty($docComment)) { return; } $docParser = new \Sami\Parser\DocBlockParser(); $parsedDoc = $docParser->parse($docComment->getText()); $ignore = $parsedDoc->getTag('ignore'); return array('raw' => $docComment->getText(), 'formatted' => $docComment->getReformattedText(), 'shortDesc' => trim($parsedDoc->getShortDesc()), 'longDesc' => trim($parsedDoc->getLongDesc()), 'ignore' => !empty($ignore), 'params' => $parsedDoc->getTag('param')); }
private function getLineOfReturn(\PHPParser_Node $node) { $comment = $node->getDocComment(); return $node->getLine() - substr_count($comment, "\n", strripos($comment, '@return')) - 1; }