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

Support "one-line" comments (starting with //) #202

Open
wants to merge 2 commits into
base: 1.22.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function count;
use function in_array;
use function strlen;
use function strpos;
use function substr;

class TokenIterator
Expand Down Expand Up @@ -380,4 +381,37 @@ public function hasParentheses(int $startPos, int $endPos): bool
&& $this->hasTokenImmediatelyAfter($endPos, Lexer::TOKEN_CLOSE_PARENTHESES);
}

/**
* Strip PHP style "one-line" comments (text starting with //)
*/
public function stripComments(): void
{
$line = 1;
$cleanTokens = [];
for ($i = 0; $i < count($this->tokens); $i++) {
$token = $this->tokens[$i];
if (
$token[Lexer::TYPE_OFFSET] === Lexer::TOKEN_OTHER
&& strpos($token[Lexer::VALUE_OFFSET], '//') === 0
) {
while (
strpos($this->tokens[$i][Lexer::VALUE_OFFSET], "\n") === false
&& $i < count($this->tokens)
) {
$i++;
}
if ($this->tokens[$i][Lexer::LINE_OFFSET] !== $line) {
for ($j = $i + 1; $j < count($this->tokens); $j++) {
$this->tokens[$j][Lexer::LINE_OFFSET]--;
}
}
} else {
$cleanTokens[] = $token;
$line = $token[Lexer::LINE_OFFSET];
}
}

$this->tokens = $cleanTokens;
}

}
1 change: 1 addition & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct(
/** @phpstan-impure */
public function parse(TokenIterator $tokens): Ast\Type\TypeNode
{
$tokens->stripComments();
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();
if ($tokens->isCurrentTokenType(Lexer::TOKEN_NULLABLE)) {
Expand Down
116 changes: 116 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,122 @@ public function provideParseData(): array
'int|array{}',
new UnionTypeNode([new IdentifierTypeNode('int'), new ArrayShapeNode([])]),
],
[
<<<EOT
array{ // Array with comments
// Array with single quoted keys
'single quote keys': array{ // Single quoted key
'single_quote_key//1': int, // Single quoted key with //
'single_quote_key\'//2': string, // Single quoted key with ' and //
'single_quote_key\'//\'3': bool, // Single quoted key with 2x ' and //
'single_quote_key"//"4': float, // Single quoted key with 2x " and //
'single_quote_key"//\'5': array{ // Single quoted key with ', " and //
'single_quote_key//5//1': int, // Single quoted key with 2x //
},
// 'commented_out_array_element//1': int
'single_quote_key//no_whitespace':int,//Single quoted key without whitespace
},
// Array with double quoted keys
"double quote keys": array{ // Double quoted key
"double_quote_key//1": int, // Double quoted key with //
"double_quote_key'//2": string, // Double quoted key with ' and //
"double_quote_key\"//\"3": bool, // Double quoted key with 2x ' and //
"double_quote_key'//'4": float, // Double quoted key with 2x " and //
"double_quote_key\"//'5": array{ // Double quoted key with ', " and //
"double_quote_key//5//1": int, // Double quoted key with 2x //
},
// "commented_out_array_element//1": int
"double_quote_key//no_whitespace":int,//Double quoted key without whitespace
},
}
EOT,
new ArrayShapeNode([
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single quote keys', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new ArrayShapeNode([
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key//1', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key\'//2', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('string')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key\'//\'3', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('bool')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key"//"4', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('float')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key"//\'5', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new ArrayShapeNode([
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key//5//1', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
])
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('single_quote_key//no_whitespace', QuoteAwareConstExprStringNode::SINGLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
])
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('double quote keys', QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new ArrayShapeNode([
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('double_quote_key//1', QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode("double_quote_key'//2", QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('string')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('double_quote_key"//"3', QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('bool')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode("double_quote_key'//'4", QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('float')
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode("double_quote_key\"//'5", QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new ArrayShapeNode([
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('double_quote_key//5//1', QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
])
),
new ArrayShapeItemNode(
new QuoteAwareConstExprStringNode('double_quote_key//no_whitespace', QuoteAwareConstExprStringNode::DOUBLE_QUOTED),
false,
new IdentifierTypeNode('int')
),
])
),
]),
],
[
'callable(' . PHP_EOL .
' Foo' . PHP_EOL .
Expand Down