Skip to content

Commit dd2857b

Browse files
committed
minor #4377 CommentsAnalyzer - fix for declare before header comment (kubawerlos)
This PR was squashed before being merged into the 2.15 branch (closes #4377). Discussion ---------- CommentsAnalyzer - fix for declare before header comment Commits ------- b27f23f CommentsAnalyzer - fix for declare before header comment
2 parents a8604fe + b27f23f commit dd2857b

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

php-cs-fixer

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
* with this source code in the file LICENSE.
1212
*/
1313

14-
/**
15-
* @author Fabien Potencier <fabien@symfony.com>
16-
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
17-
*/
1814
if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
1915
error_reporting(-1);
2016
}

src/Tokenizer/Analyzer/CommentsAnalyzer.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,28 @@ public function isHeaderComment(Tokens $tokens, $index)
4040
throw new \InvalidArgumentException('Given index must point to a comment.');
4141
}
4242

43-
$prevIndex = $tokens->getPrevMeaningfulToken($index);
43+
if (null === $tokens->getNextMeaningfulToken($index)) {
44+
return false;
45+
}
46+
47+
$prevIndex = $tokens->getPrevNonWhitespace($index);
48+
49+
if ($tokens[$prevIndex]->equals(';')) {
50+
$braceCloseIndex = $tokens->getPrevMeaningfulToken($prevIndex);
51+
if (!$tokens[$braceCloseIndex]->equals(')')) {
52+
return false;
53+
}
54+
55+
$braceOpenIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $braceCloseIndex);
56+
$declareIndex = $tokens->getPrevMeaningfulToken($braceOpenIndex);
57+
if (!$tokens[$declareIndex]->isGivenKind(T_DECLARE)) {
58+
return false;
59+
}
60+
61+
$prevIndex = $tokens->getPrevNonWhitespace($declareIndex);
62+
}
4463

45-
return $tokens[$prevIndex]->isGivenKind(T_OPEN_TAG) && null !== $tokens->getNextMeaningfulToken($index);
64+
return $tokens[$prevIndex]->isGivenKind(T_OPEN_TAG);
4665
}
4766

4867
/**
@@ -224,8 +243,7 @@ private function isValidLanguageConstruct(Tokens $tokens, Token $docsToken, $lan
224243

225244
$endKind = $tokens[$languageConstructIndex]->isGivenKind(CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN)
226245
? [CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE]
227-
: ')'
228-
;
246+
: ')';
229247

230248
$endIndex = $tokens->getNextTokenOfKind($languageConstructIndex, [$endKind]);
231249

tests/Tokenizer/Analyzer/CommentsAnalyzerTest.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,54 @@ public function testHeaderCommentAcceptsOnlyComments()
157157
$analyzer->isHeaderComment($tokens, 2);
158158
}
159159

160-
public function testHeaderComment()
160+
/**
161+
* @param string $code
162+
* @param int $index
163+
*
164+
* @dataProvider provideHeaderCommentCases
165+
*/
166+
public function testHeaderComment($code, $index)
161167
{
162-
$tokens = Tokens::fromCode('<?php /* This is header */ namespace Foo;');
168+
$tokens = Tokens::fromCode($code);
163169
$analyzer = new CommentsAnalyzer();
164170

165-
static::assertTrue($analyzer->isHeaderComment($tokens, 1));
171+
static::assertTrue($analyzer->isHeaderComment($tokens, $index));
166172
}
167173

168-
public function testNotHeaderComment()
174+
public function provideHeaderCommentCases()
169175
{
170-
$tokens = Tokens::fromCode('<?php /* This is not header */');
176+
return [
177+
['<?php /* Comment */ namespace Foo;', 1],
178+
['<?php /** Comment */ namespace Foo;', 1],
179+
['<?php declare(strict_types=1); /* Comment */ namespace Foo;', 9],
180+
['<?php /* We test this one */ /* Foo */ namespace Bar;', 1],
181+
];
182+
}
183+
184+
/**
185+
* @param string $code
186+
* @param int $index
187+
*
188+
* @dataProvider provideNotHeaderCommentCases
189+
*/
190+
public function testNotHeaderComment($code, $index)
191+
{
192+
$tokens = Tokens::fromCode($code);
171193
$analyzer = new CommentsAnalyzer();
172194

173-
static::assertFalse($analyzer->isHeaderComment($tokens, 1));
195+
static::assertFalse($analyzer->isHeaderComment($tokens, $index));
196+
}
197+
198+
public function provideNotHeaderCommentCases()
199+
{
200+
return [
201+
['<?php $foo; /* Comment */ $bar;', 4],
202+
['<?php foo(); /* Comment */ $bar;', 6],
203+
['<?php namespace Foo; /* Comment */ class Bar {};', 6],
204+
['<?php /* It is not header when no content after */', 1],
205+
['<?php /* Foo */ /* We test this one */ namespace Bar;', 3],
206+
['<?php /* Foo */ declare(strict_types=1); /* We test this one */ namespace Bar;', 11],
207+
];
174208
}
175209

176210
public function testPhpdocCandidateAcceptsOnlyComments()

0 commit comments

Comments
 (0)