Skip to content

Commit

Permalink
Add missing parameter types
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 11, 2022
1 parent e9800cf commit b9fe344
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 28 deletions.
7 changes: 7 additions & 0 deletions lib/PhpParser/Builder/Declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
abstract class Declaration implements PhpParser\Builder {
protected $attributes = [];

/**
* Adds a statement.
*
* @param PhpParser\Node\Stmt|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
abstract public function addStmt($stmt);

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/PhpParser/Internal/DiffElem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class DiffElem {
/** @var mixed Is null for remove operations */
public $new;

/**
* @param int $type One of the TYPE_* constants
* @param mixed $old Is null for add operations
* @param mixed $new Is null for remove operations
*/
public function __construct(int $type, $old, $new) {
$this->type = $type;
$this->old = $old;
Expand Down
3 changes: 3 additions & 0 deletions lib/PhpParser/Internal/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType): bool {
return false;
}

/** @param int|string|array $skipTokenType */
public function skipLeft(int $pos, $skipTokenType) {
$tokens = $this->tokens;

Expand All @@ -119,6 +120,7 @@ public function skipLeft(int $pos, $skipTokenType) {
return $this->skipLeftWhitespace($pos);
}

/** @param int|string|array $skipTokenType */
public function skipRight(int $pos, $skipTokenType) {
$tokens = $this->tokens;

Expand Down Expand Up @@ -168,6 +170,7 @@ public function skipRightWhitespace(int $pos): int {
return $pos;
}

/** @param int|string|array $findTokenType */
public function findRight(int $pos, $findTokenType) {
$tokens = $this->tokens;
for ($count = \count($tokens); $pos < $count; $pos++) {
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function decode(string $json) {
return $this->decodeRecursive($value);
}

/** @param mixed $value */
private function decodeRecursive($value) {
if (\is_array($value)) {
if (isset($value['nodeType'])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function getShortName(string $name, int $type): Name {
return $shortestName;
}

private function resolveAlias(Name $name, $type) {
private function resolveAlias(Name $name, int $type) {
$firstPart = $name->getFirst();

if ($name->isQualified()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/Node/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function isAnonymous(): bool {
/**
* @internal
*/
public static function verifyClassModifier($a, $b) {
public static function verifyClassModifier(int $a, int $b) {
if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
throw new Error('Multiple abstract modifiers are not allowed');
}
Expand All @@ -113,7 +113,7 @@ public static function verifyClassModifier($a, $b) {
/**
* @internal
*/
public static function verifyModifier($a, $b) {
public static function verifyModifier(int $a, int $b) {
if ($a & Modifiers::VISIBILITY_MASK && $b & Modifiers::VISIBILITY_MASK) {
throw new Error('Multiple access type modifiers are not allowed');
}
Expand Down
9 changes: 5 additions & 4 deletions lib/PhpParser/NodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function dump($node, ?string $code = null): string {
return $this->dumpRecursive($node);
}

/** @param Node|Comment|array $node */
protected function dumpRecursive($node) {
if ($node instanceof Node) {
$r = $node->getType();
Expand Down Expand Up @@ -107,7 +108,7 @@ protected function dumpRecursive($node) {
return $r . "\n)";
}

protected function dumpFlags($flags) {
protected function dumpFlags(int $flags) {
$strs = [];
if ($flags & Modifiers::PUBLIC) {
$strs[] = 'PUBLIC';
Expand Down Expand Up @@ -138,7 +139,7 @@ protected function dumpFlags($flags) {
}
}

protected function dumpIncludeType($type) {
protected function dumpIncludeType(int $type) {
$map = [
Include_::TYPE_INCLUDE => 'TYPE_INCLUDE',
Include_::TYPE_INCLUDE_ONCE => 'TYPE_INCLUDE_ONCE',
Expand All @@ -152,7 +153,7 @@ protected function dumpIncludeType($type) {
return $map[$type] . ' (' . $type . ')';
}

protected function dumpUseType($type) {
protected function dumpUseType(int $type) {
$map = [
Use_::TYPE_UNKNOWN => 'TYPE_UNKNOWN',
Use_::TYPE_NORMAL => 'TYPE_NORMAL',
Expand Down Expand Up @@ -190,7 +191,7 @@ protected function dumpPosition(Node $node): ?string {
}

// Copied from Error class
private function toColumn($code, $pos) {
private function toColumn(string $code, int $pos) {
if ($pos > strlen($code)) {
throw new \RuntimeException('Invalid position information');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected function traverseArray(array $nodes): array {
return $nodes;
}

private function ensureReplacementReasonable($old, $new) {
private function ensureReplacementReasonable(Node $old, Node $new) {
if ($old instanceof Node\Stmt && $new instanceof Node\Expr) {
throw new \LogicException(
"Trying to replace statement ({$old->getType()}) " .
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function enterNode(Node $node) {
return null;
}

private function addAlias(Node\UseItem $use, $type, ?Name $prefix = null) {
private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null) {
// Add prefix for group uses
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
// Type is determined either by individual element or whole use declaration
Expand All @@ -180,7 +180,7 @@ private function resolveSignature($node) {
$node->returnType = $this->resolveType($node->returnType);
}

private function resolveType($node) {
private function resolveType(?Node $node) {
if ($node instanceof Name) {
return $this->resolveClassName($node);
}
Expand Down
24 changes: 13 additions & 11 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\InterpolatedString;
Expand Down Expand Up @@ -662,7 +663,7 @@ protected function getFloatCastKind(string $cast): int {
return Double::KIND_DOUBLE;
}

protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
protected function parseLNumber(string $str, array $attributes, bool $allowInvalidOctal = false) {
try {
return Int_::fromString($str, $attributes, $allowInvalidOctal);
} catch (Error $error) {
Expand Down Expand Up @@ -725,6 +726,7 @@ function ($matches) use ($indentLen, $indentChar, $attributes) {
);
}

/** @param string|array $contents */
protected function parseDocString(
string $startToken, $contents, string $endToken,
array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape
Expand Down Expand Up @@ -872,7 +874,7 @@ protected function postprocessList(Expr\List_ $node): void {
}
}

protected function checkClassModifier($a, $b, $modifierPos) {
protected function checkClassModifier(int $a, int $b, int $modifierPos) {
try {
Class_::verifyClassModifier($a, $b);
} catch (Error $error) {
Expand All @@ -881,7 +883,7 @@ protected function checkClassModifier($a, $b, $modifierPos) {
}
}

protected function checkModifier($a, $b, $modifierPos) {
protected function checkModifier(int $a, int $b, int $modifierPos) {
// Jumping through some hoops here because verifyModifier() is also used elsewhere
try {
Class_::verifyModifier($a, $b);
Expand Down Expand Up @@ -920,7 +922,7 @@ protected function checkNamespace(Namespace_ $node) {
}
}

private function checkClassName($name, $namePos) {
private function checkClassName(?Identifier $name, int $namePos) {
if (null !== $name && $name->isSpecialClassName()) {
$this->emitError(new Error(
sprintf('Cannot use \'%s\' as class name as it is reserved', $name),
Expand All @@ -940,7 +942,7 @@ private function checkImplementedInterfaces(array $interfaces) {
}
}

protected function checkClass(Class_ $node, $namePos) {
protected function checkClass(Class_ $node, int $namePos) {
$this->checkClassName($node->name, $namePos);

if ($node->extends && $node->extends->isSpecialClassName()) {
Expand All @@ -953,17 +955,17 @@ protected function checkClass(Class_ $node, $namePos) {
$this->checkImplementedInterfaces($node->implements);
}

protected function checkInterface(Interface_ $node, $namePos) {
protected function checkInterface(Interface_ $node, int $namePos) {
$this->checkClassName($node->name, $namePos);
$this->checkImplementedInterfaces($node->extends);
}

protected function checkEnum(Enum_ $node, $namePos) {
protected function checkEnum(Enum_ $node, int $namePos) {
$this->checkClassName($node->name, $namePos);
$this->checkImplementedInterfaces($node->implements);
}

protected function checkClassMethod(ClassMethod $node, $modifierPos) {
protected function checkClassMethod(ClassMethod $node, int $modifierPos) {
if ($node->flags & Modifiers::STATIC) {
switch ($node->name->toLowerString()) {
case '__construct':
Expand Down Expand Up @@ -991,7 +993,7 @@ protected function checkClassMethod(ClassMethod $node, $modifierPos) {
}
}

protected function checkClassConst(ClassConst $node, $modifierPos) {
protected function checkClassConst(ClassConst $node, int $modifierPos) {
if ($node->flags & Modifiers::STATIC) {
$this->emitError(new Error(
"Cannot use 'static' as constant modifier",
Expand All @@ -1009,7 +1011,7 @@ protected function checkClassConst(ClassConst $node, $modifierPos) {
}
}

protected function checkProperty(Property $node, $modifierPos) {
protected function checkProperty(Property $node, int $modifierPos) {
if ($node->flags & Modifiers::ABSTRACT) {
$this->emitError(new Error('Properties cannot be declared abstract',
$this->getAttributesAt($modifierPos)));
Expand All @@ -1021,7 +1023,7 @@ protected function checkProperty(Property $node, $modifierPos) {
}
}

protected function checkUseUse(UseItem $node, $namePos) {
protected function checkUseUse(UseItem $node, int $namePos) {
if ($node->alias && $node->alias->isSpecialClassName()) {
$this->emitError(new Error(
sprintf(
Expand Down
14 changes: 7 additions & 7 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ protected function pUseItem(Node\UseItem $node) {
. (null !== $node->alias ? ' as ' . $node->alias : '');
}

protected function pUseType($type) {
protected function pUseType(int $type) {
return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
: ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
}
Expand Down Expand Up @@ -978,7 +978,7 @@ protected function pStmt_Nop(Stmt\Nop $node) {

// Helpers

protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) {
protected function pClassCommon(Stmt\Class_ $node, string $afterClassToken) {
return $this->pAttrGroups($node->attrGroups, $node->name === null)
. $this->pModifiers($node->flags)
. 'class' . $afterClassToken
Expand All @@ -987,15 +987,15 @@ protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) {
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
}

protected function pObjectProperty($node) {
protected function pObjectProperty(Node $node) {
if ($node instanceof Expr) {
return '{' . $this->p($node) . '}';
} else {
return $node;
}
}

protected function pEncapsList(array $encapsList, $quote) {
protected function pEncapsList(array $encapsList, ?string $quote) {
$return = '';
foreach ($encapsList as $element) {
if ($element instanceof Node\InterpolatedStringPart) {
Expand All @@ -1017,7 +1017,7 @@ protected function pSingleQuotedString(string $string) {
return '\'' . preg_replace($regex, '\\\\$0', $string) . '\'';
}

protected function escapeString($string, $quote) {
protected function escapeString(string $string, ?string $quote) {
if (null === $quote) {
// For doc strings, don't escape newlines
$escaped = addcslashes($string, "\t\f\v$\\");
Expand Down Expand Up @@ -1050,14 +1050,14 @@ protected function escapeString($string, $quote) {
}, $escaped);
}

protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) {
protected function containsEndLabel(string $string, string $label, bool $atStart = true, bool $atEnd = true) {
$start = $atStart ? '(?:^|[\r\n])' : '[\r\n]';
$end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]';
return false !== strpos($string, $label)
&& preg_match('/' . $start . $label . $end . '/', $string);
}

protected function encapsedContainsEndLabel(array $parts, $label) {
protected function encapsedContainsEndLabel(array $parts, string $label) {
foreach ($parts as $i => $part) {
$atStart = $i === 0;
$atEnd = $i === count($parts) - 1;
Expand Down

0 comments on commit b9fe344

Please sign in to comment.