1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Add non-void return types

This commit is contained in:
Matthew Brown 2017-01-24 00:38:55 -07:00 committed by Nikita Popov
parent 70f86cb6cb
commit e3b87f40aa
17 changed files with 146 additions and 3 deletions

View File

@ -114,6 +114,9 @@ class Comment implements \JsonSerializable
return $text;
}
/**
* @return float
*/
private function getShortestWhitespacePrefixLen($str) {
$lines = explode("\n", $str);
$shortestPrefixLen = INF;
@ -127,6 +130,10 @@ class Comment implements \JsonSerializable
return $shortestPrefixLen;
}
/**
* @return array
* @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
*/
public function jsonSerialize() {
// Technically not a node, but we make it look like one anyway
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';

View File

@ -130,6 +130,9 @@ class Error extends \RuntimeException
return $this->toColumn($code, $this->attributes['endFilePos']);
}
/**
* @return string
*/
public function getMessageWithColumnInfo($code) {
return sprintf(
'%s from %d:%d to %d:%d', $this->getRawMessage(),

View File

@ -117,12 +117,18 @@ class Lexer
}
}
/**
* @return bool
*/
private function isUnterminatedComment($token) {
return ($token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)
&& substr($token[1], 0, 2) === '/*'
&& substr($token[1], -2) !== '*/';
}
/**
* @return bool
*/
private function errorMayHaveOccurred() {
if (defined('HHVM_VERSION')) {
// In HHVM token_get_all() does not throw warnings, so we need to conservatively

View File

@ -52,6 +52,9 @@ class Emulative extends \PhpParser\Lexer
/*
* Checks if the code is potentially using features that require emulation.
*/
/**
* @return int|false
*/
protected function requiresEmulation($code) {
if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
return false;

View File

@ -111,6 +111,9 @@ class String_ extends Scalar
);
}
/**
* @return string
*/
private static function codePointToUtf8($num) {
if ($num <= 0x7F) {
return chr($num);

View File

@ -28,19 +28,31 @@ class ClassConst extends Node\Stmt
return array('flags', 'consts');
}
/**
* @return bool
*/
public function isPublic() {
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
}
/**
* @return bool
*/
public function isProtected() {
return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
}
/**
* @return bool
*/
public function isPrivate() {
return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
}
/**
* @return bool
*/
public function isStatic() {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}

View File

@ -63,27 +63,45 @@ class ClassMethod extends Node\Stmt implements FunctionLike
return $this->stmts;
}
/**
* @return bool
*/
public function isPublic() {
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
}
/**
* @return bool
*/
public function isProtected() {
return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
}
/**
* @return bool
*/
public function isPrivate() {
return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
}
/**
* @return bool
*/
public function isAbstract() {
return (bool) ($this->flags & Class_::MODIFIER_ABSTRACT);
}
/**
* @return bool
*/
public function isFinal() {
return (bool) ($this->flags & Class_::MODIFIER_FINAL);
}
/**
* @return bool
*/
public function isStatic() {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}

View File

@ -54,14 +54,23 @@ class Class_ extends ClassLike
return array('flags', 'name', 'extends', 'implements', 'stmts');
}
/**
* @return bool
*/
public function isAbstract() {
return (bool) ($this->flags & self::MODIFIER_ABSTRACT);
}
/**
* @return bool
*/
public function isFinal() {
return (bool) ($this->flags & self::MODIFIER_FINAL);
}
/**
* @return bool
*/
public function isAnonymous() {
return null === $this->name;
}

View File

@ -28,19 +28,31 @@ class Property extends Node\Stmt
return array('flags', 'props');
}
/**
* @return bool
*/
public function isPublic() {
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
}
/**
* @return bool
*/
public function isProtected() {
return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
}
/**
* @return bool
*/
public function isPrivate() {
return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
}
/**
* @return bool
*/
public function isStatic() {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}

View File

@ -109,6 +109,9 @@ abstract class NodeAbstract implements Node, \JsonSerializable
$this->attributes = $attributes;
}
/**
* @return array
*/
public function jsonSerialize() {
return ['nodeType' => $this->getType()] + get_object_vars($this);
}

View File

@ -164,6 +164,9 @@ class NodeDumper
return $map[$type] . ' (' . $type . ')';
}
/**
* @return string|null
*/
protected function dumpPosition(Node $node) {
if (!$node->hasAttribute('startLine') || !$node->hasAttribute('endLine')) {
return null;

View File

@ -58,9 +58,8 @@ class NodeTraverser implements NodeTraverserInterface
/**
* Traverses an array of nodes using the registered visitors.
*
* @param Node[] $nodes Array of nodes
*
* @return Node[] Traversed array of nodes
* @param Node[] $nodes Array of nodes
* @return array
*/
public function traverse(array $nodes) {
foreach ($this->visitors as $visitor) {
@ -80,6 +79,9 @@ class NodeTraverser implements NodeTraverserInterface
return $nodes;
}
/**
* @return \PhpParser\Node
*/
protected function traverseNode(Node $node) {
foreach ($node->getSubNodeNames() as $name) {
$subNode =& $node->$name;
@ -118,6 +120,9 @@ class NodeTraverser implements NodeTraverserInterface
return $node;
}
/**
* @return array
*/
protected function traverseArray(array $nodes) {
$doNodes = array();

View File

@ -192,6 +192,9 @@ class NameResolver extends NodeVisitorAbstract
}
}
/**
* @return null|Name
*/
protected function resolveClassName(Name $name) {
if (!$this->replaceNodes) {
$name->setAttribute('resolvedName', $this->getResolvedClassName($name));
@ -208,6 +211,9 @@ class NameResolver extends NodeVisitorAbstract
return $this->getResolvedClassName($name);
}
/**
* @return Name
*/
protected function resolveOtherName(Name $name, $type) {
if (!$this->replaceNodes) {
$resolvedName = $this->getResolvedOtherName($name, $type);
@ -239,6 +245,9 @@ class NameResolver extends NodeVisitorAbstract
return $name;
}
/**
* @return null|Name
*/
protected function getResolvedClassName(Name $name) {
// don't resolve special class names
if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
@ -267,6 +276,9 @@ class NameResolver extends NodeVisitorAbstract
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
}
/**
* @return null|Name
*/
protected function getResolvedOtherName(Name $name, $type) {
// fully qualified names are already resolved
if ($name->isFullyQualified()) {

View File

@ -23,6 +23,10 @@ class Multiple implements Parser {
$this->parsers = $parsers;
}
/**
* @return \PhpParser\Node\Stmt[]
* @psalm-return array<mixed, \PhpParser\Node\Stmt>
*/
public function parse($code, ErrorHandler $errorHandler = null) {
if (null === $errorHandler) {
$errorHandler = new ErrorHandler\Throwing;

View File

@ -355,6 +355,9 @@ abstract class ParserAbstract implements Parser
$this->errorHandler->handleError($error);
}
/**
* @return string
*/
protected function getErrorMessage($symbol, $state) {
$expectedString = '';
if ($expected = $this->getExpectedTokens($state)) {
@ -364,6 +367,10 @@ abstract class ParserAbstract implements Parser
return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
}
/**
* @return array
* @psalm-return array<int, mixed>
*/
protected function getExpectedTokens($state) {
$expected = array();
@ -509,6 +516,9 @@ abstract class ParserAbstract implements Parser
}
}
/**
* @return null|string
*/
private function getNamespacingStyle(array $stmts) {
$style = null;
$hasNotAllowedStmts = false;
@ -552,6 +562,9 @@ abstract class ParserAbstract implements Parser
return $style;
}
/**
* @return \PhpParser\Node\Expr\StaticCall
*/
protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) {
if ($prop instanceof Node\Expr\StaticPropertyFetch) {
// Preserve attributes if possible
@ -602,6 +615,9 @@ abstract class ParserAbstract implements Parser
}
}
/**
* @return string
*/
protected function handleBuiltinTypes(Name $name) {
$scalarTypes = [
'bool' => true,
@ -632,10 +648,16 @@ abstract class ParserAbstract implements Parser
'static' => true,
);
/**
* @return array
*/
protected function getAttributesAt($pos) {
return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
}
/**
* @return LNumber
*/
protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
try {
return LNumber::fromString($str, $attributes, $allowInvalidOctal);
@ -646,6 +668,9 @@ abstract class ParserAbstract implements Parser
}
}
/**
* @return LNumber|String_
*/
protected function parseNumString($str, $attributes) {
if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
return new String_($str, $attributes);

View File

@ -238,6 +238,9 @@ abstract class PrettyPrinterAbstract
}
}
/**
* @return string
*/
protected function pInfixOp($type, Node $leftNode, $operatorString, Node $rightNode) {
list($precedence, $associativity) = $this->precedenceMap[$type];
@ -246,11 +249,17 @@ abstract class PrettyPrinterAbstract
. $this->pPrec($rightNode, $precedence, $associativity, 1);
}
/**
* @return string
*/
protected function pPrefixOp($type, $operatorString, Node $node) {
list($precedence, $associativity) = $this->precedenceMap[$type];
return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
}
/**
* @return string
*/
protected function pPostfixOp($type, Node $node, $operatorString) {
list($precedence, $associativity) = $this->precedenceMap[$type];
return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
@ -725,11 +734,17 @@ abstract class PrettyPrinterAbstract
return $indent;
}
/**
* @return bool
*/
protected function haveParens($startPos, $endPos) {
return $this->haveTokenImmediativelyBefore($startPos, '(')
&& $this->haveTokenImmediatelyAfter($endPos, ')');
}
/**
* @return bool
*/
protected function haveBraces($startPos, $endPos) {
return $this->haveTokenImmediativelyBefore($startPos, '{')
&& $this->haveTokenImmediatelyAfter($endPos, '}');

View File

@ -139,6 +139,9 @@ class XML implements Unserializer
);
}
/**
* @return string
*/
protected function getClassNameFromType($type) {
$className = 'PhpParser\\Node\\' . strtr($type, '_', '\\');
if (!class_exists($className)) {