Skip to content

Commit

Permalink
Specify more types
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 17, 2022
1 parent 032b102 commit fc6b489
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/PhpParser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getEndLine(): int {
/**
* Gets the attributes of the node/token the error occurred at.
*
* @return array
* @return array<string, mixed>
*/
public function getAttributes(): array {
return $this->attributes;
Expand Down
41 changes: 28 additions & 13 deletions lib/PhpParser/Internal/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
* Myers, Eugene W. "An O (ND) difference algorithm and its variations."
* Algorithmica 1.1 (1986): 251-266.
*
* @template T
* @internal
*/
class Differ {
/** @var callable(T, T): bool */
private $isEqual;

/**
* Create differ over the given equality relation.
*
* @param callable $isEqual Equality relation with signature function($a, $b) : bool
* @param callable(T, T): bool $isEqual Equality relation
*/
public function __construct(callable $isEqual) {
$this->isEqual = $isEqual;
Expand All @@ -25,8 +27,8 @@ public function __construct(callable $isEqual) {
/**
* Calculate diff (edit script) from $old to $new.
*
* @param array $old Original array
* @param array $new New array
* @param T[] $old Original array
* @param T[] $new New array
*
* @return DiffElem[] Diff (edit script)
*/
Expand All @@ -41,18 +43,23 @@ public function diff(array $old, array $new): array {
* If a sequence of remove operations is followed by the same number of add operations, these
* will be coalesced into replace operations.
*
* @param array $old Original array
* @param array $new New array
* @param T[] $old Original array
* @param T[] $new New array
*
* @return DiffElem[] Diff (edit script), including replace operations
*/
public function diffWithReplacements(array $old, array $new): array {
return $this->coalesceReplacements($this->diff($old, $new));
}

private function calculateTrace(array $a, array $b): array {
$n = \count($a);
$m = \count($b);
/**
* @param T[] $old
* @param T[] $new
* @return array{array<int, array<int, int>>, int, int}
*/
private function calculateTrace(array $old, array $new): array {
$n = \count($old);
$m = \count($new);
$max = $n + $m;
$v = [1 => 0];
$trace = [];
Expand All @@ -66,7 +73,7 @@ private function calculateTrace(array $a, array $b): array {
}

$y = $x - $k;
while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
while ($x < $n && $y < $m && ($this->isEqual)($old[$x], $new[$y])) {
$x++;
$y++;
}
Expand All @@ -80,7 +87,15 @@ private function calculateTrace(array $a, array $b): array {
throw new \Exception('Should not happen');
}

private function extractDiff(array $trace, int $x, int $y, array $a, array $b): array {
/**
* @param array<int, array<int, int>> $trace
* @param int $x
* @param int $y
* @param T[] $old
* @param T[] $new
* @return DiffElem[]
*/
private function extractDiff(array $trace, int $x, int $y, array $old, array $new): array {
$result = [];
for ($d = \count($trace) - 1; $d >= 0; $d--) {
$v = $trace[$d];
Expand All @@ -96,7 +111,7 @@ private function extractDiff(array $trace, int $x, int $y, array $a, array $b):
$prevY = $prevX - $prevK;

while ($x > $prevX && $y > $prevY) {
$result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
$result[] = new DiffElem(DiffElem::TYPE_KEEP, $old[$x-1], $new[$y-1]);
$x--;
$y--;
}
Expand All @@ -106,12 +121,12 @@ private function extractDiff(array $trace, int $x, int $y, array $a, array $b):
}

while ($x > $prevX) {
$result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
$result[] = new DiffElem(DiffElem::TYPE_REMOVE, $old[$x-1], null);
$x--;
}

while ($y > $prevY) {
$result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
$result[] = new DiffElem(DiffElem::TYPE_ADD, null, $new[$y-1]);
$y--;
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/PhpParser/Internal/PrintableNewAnonClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class PrintableNewAnonClassNode extends Expr {
/** @var Node\Stmt[] Statements */
public $stmts;

/**
* @param Node\AttributeGroup[] $attrGroups PHP attribute groups
* @param Node\Arg[] $args Arguments
* @param Node\Name|null $extends Name of extended class
* @param Node\Name[] $implements Names of implemented interfaces
* @param Node\Stmt[] $stmts Statements
* @param array<string, mixed> $attributes Attributes
*/
public function __construct(
array $attrGroups, array $args, ?Node\Name $extends, array $implements,
array $stmts, array $attributes
Expand Down
8 changes: 4 additions & 4 deletions lib/PhpParser/Internal/TokenPolyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class TokenPolyfill {
/** @var int The 0-based starting position of the token (or -1 if unknown). */
public $pos;

/** @var bool[] Tokens ignored by the PHP parser. */
/** @var array<int, bool> Tokens ignored by the PHP parser. */
private const IGNORABLE_TOKENS = [
\T_WHITESPACE => true,
\T_COMMENT => true,
\T_DOC_COMMENT => true,
\T_OPEN_TAG => true,
];

/** @var bool[]|null Tokens that may be part of a T_NAME_* identifier. */
/** @var array<int, bool>|null Tokens that may be part of a T_NAME_* identifier. */
private static $identifierTokens;

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ public function getTokenName(): ?string {
* the token ID, a string that matches the token text, or an array of integers/strings. In the
* latter case, the function returns true if any of the kinds in the array match.
*
* @param int|string|array $kind
* @param int|string|(int|string)[] $kind
*/
public function is($kind): bool {
if (\is_int($kind)) {
Expand Down Expand Up @@ -119,7 +119,7 @@ public function __toString(): string {
* T_WHITESPACE token.
* * Namespaced names are represented using T_NAME_* tokens.
*
* @returns static[]
* @return static[]
*/
public static function tokenize(string $code, int $flags = 0): array {
self::init();
Expand Down
6 changes: 3 additions & 3 deletions lib/PhpParser/Internal/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType): bool {
return false;
}

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

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

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

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

/** @param int|string|array $findTokenType */
/** @param int|string|(int|string)[] $findTokenType */
public function findRight(int $pos, $findTokenType): int {
$tokens = $this->tokens;
for ($count = \count($tokens); $pos < $count; $pos++) {
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PhpParser;

class JsonDecoder {
/** @var \ReflectionClass[] Node type to reflection class map */
/** @var \ReflectionClass<Node>[] Node type to reflection class map */
private $reflectionClassCache;

/** @return mixed */
Expand Down Expand Up @@ -48,7 +48,6 @@ private function decodeNode(array $value): Node {
}

$reflectionClass = $this->reflectionClassFromNodeType($nodeType);
/** @var Node $node */
$node = $reflectionClass->newInstanceWithoutConstructor();

if (isset($value['attributes'])) {
Expand Down Expand Up @@ -83,6 +82,7 @@ private function decodeComment(array $value): Comment {
);
}

/** @return \ReflectionClass<Node> */
private function reflectionClassFromNodeType(string $nodeType): \ReflectionClass {
if (!isset($this->reflectionClassCache[$nodeType])) {
$className = $this->classNameFromNodeType($nodeType);
Expand Down
10 changes: 9 additions & 1 deletion lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ class Lexer {
protected $tokens;
/** @var int Current position in the token array */
protected $pos;
/** @var bool Whether the preceding closing PHP tag has a trailing newline */
protected $prevCloseTagHasNewline;

/** @var array<int, int> Map of tokens that should be dropped (like T_WHITESPACE) */
protected $dropTokens;

/** @var bool Whether to use the startLine attribute */
private $attributeStartLineUsed;
/** @var bool Whether to use the endLine attribute */
private $attributeEndLineUsed;
/** @var bool Whether to use the startTokenPos attribute */
private $attributeStartTokenPosUsed;
/** @var bool Whether to use the endTokenPos attribute */
private $attributeEndTokenPosUsed;
/** @var bool Whether to use the startFilePos attribute */
private $attributeStartFilePosUsed;
/** @var bool Whether to use the endFilePos attribute */
private $attributeEndFilePosUsed;
/** @var bool Whether to use the comments attribute */
private $attributeCommentsUsed;

/**
Expand Down

0 comments on commit fc6b489

Please sign in to comment.