Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise phpdocs #1058

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/PhpParser/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

class Comment implements \JsonSerializable {
protected string $text;
/** @var -1|positive-int */
protected int $startLine;
protected int $startFilePos;
protected int $startTokenPos;
/** @var -1|positive-int */
protected int $endLine;
protected int $endFilePos;
protected int $endTokenPos;
Expand All @@ -15,9 +17,10 @@ class Comment implements \JsonSerializable {
* Constructs a comment node.
*
* @param string $text Comment text (including comment delimiters like /*)
* @param int $startLine Line number the comment started on
* @param -1|positive-int $startLine Line number the comment started on
* @param int $startFilePos File offset the comment started on
* @param int $startTokenPos Token offset the comment started on
* @param -1|positive-int $endLine
*/
public function __construct(
string $text,
Expand Down Expand Up @@ -179,7 +182,9 @@ private function getShortestWhitespacePrefixLen(string $str): int {
$lines = explode("\n", $str);
$shortestPrefixLen = \PHP_INT_MAX;
foreach ($lines as $line) {
preg_match('(^\s*)', $line, $matches);
if (!preg_match('(^\s*)', $line, $matches)) {
continue;
}
$prefixLen = strlen($matches[0]);
if ($prefixLen < $shortestPrefixLen) {
$shortestPrefixLen = $prefixLen;
Expand Down
8 changes: 5 additions & 3 deletions lib/PhpParser/Internal/TokenPolyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class TokenPolyfill {
public int $id;
/** @var string The textual content of the token. */
public string $text;
/** @var int The 1-based starting line of the token (or -1 if unknown). */
/** @var -1|positive-int The 1-based starting line of the token (or -1 if unknown). */
public int $line;
/** @var int The 0-based starting position of the token (or -1 if unknown). */
/** @var int<-1, max> The 0-based starting position of the token (or -1 if unknown). */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these types match whatever phpstan specifies for the PhpToken class? As we may either use this class or PhpToken. Not sure which one phpstan would actually pick for analysis...

public int $pos;

/** @var array<int, bool> Tokens ignored by the PHP parser. */
Expand All @@ -38,6 +38,8 @@ class TokenPolyfill {

/**
* Create a Token with the given ID and text, as well optional line and position information.
*
* @param -1|positive-int $line
*/
final public function __construct(int $id, string $text, int $line = -1, int $pos = -1) {
$this->id = $id;
Expand Down Expand Up @@ -119,7 +121,7 @@ public function __toString(): string {
* T_WHITESPACE token.
* * Namespaced names are represented using T_NAME_* tokens.
*
* @return static[]
* @return list<static>
*/
public static function tokenize(string $code, int $flags = 0): array {
self::init();
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Lexer {
* @param string $code The source code to tokenize.
* @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
* ErrorHandler\Throwing.
* @return Token[] Tokens
* @return list<Token> Tokens
*/
public function tokenize(string $code, ?ErrorHandler $errorHandler = null): array {
if (null === $errorHandler) {
Expand Down
6 changes: 5 additions & 1 deletion lib/PhpParser/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public function getEndPos(): int {
return $this->pos + \strlen($this->text);
}

/** Get 1-based end line number of the token. */
/**
* Get 1-based end line number of the token.
*
* @return -1|positive-int
*/
public function getEndLine(): int {
return $this->line + \substr_count($this->text, "\n");
}
Expand Down
Loading