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

fix: PhpUnitTestClassRequiresCoversFixer - attribute detection when class is readonly #8042

Merged
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
15 changes: 11 additions & 4 deletions src/Fixer/AbstractPhpUnitFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,20 @@ private static function isPreventedByAttribute(Tokens $tokens, int $index, array
return false;
}

$attributeIndex = $tokens->getPrevMeaningfulToken($index);
if (!$tokens[$attributeIndex]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
$modifiers = [T_FINAL];
Copy link
Member

Choose a reason for hiding this comment

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

potentially we can reuse bit of getDocBlockIndex , where we jump directly before any final/readonly/...` to expected docblock place.

if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.2+ is required
$modifiers[] = T_READONLY;
}

do {
$index = $tokens->getPrevMeaningfulToken($index);
} while ($tokens[$index]->isGivenKind($modifiers));
if (!$tokens[$index]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
return false;
}
$attributeIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ATTRIBUTE, $attributeIndex);
$index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);

foreach (AttributeAnalyzer::collect($tokens, $attributeIndex) as $attributeAnalysis) {
foreach (AttributeAnalyzer::collect($tokens, $index) as $attributeAnalysis) {
foreach ($attributeAnalysis->getAttributes() as $attribute) {
if (\in_array(self::getFullyQualifiedName($tokens, $attribute['name']), $preventingAttributes, true)) {
return true;
Expand Down
4 changes: 0 additions & 4 deletions src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
return; // don't add `@covers` annotation for abstract base classes
}

if (isset($modifiers['final'])) {
$classIndex = $tokens->getPrevMeaningfulToken($classIndex);
}

$this->ensureIsDocBlockWithAnnotation(
$tokens,
$classIndex,
Expand Down
18 changes: 17 additions & 1 deletion tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ final class FooTest extends \PHPUnit_Framework_TestCase {}
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input): void
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
Expand All @@ -412,5 +412,21 @@ public static function provideFix82Cases(): iterable
',
null,
];

yield 'with attribute on readonly class' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
readonly class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];

yield 'with attribute on final readonly class' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
final readonly class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
}
}