Skip to content

PHPUnit attributes #4056

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

Merged
merged 9 commits into from
Jun 10, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions build/PHPStan/Build/AttributeNamedArgumentsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}
$constructor = $attributeReflection->getConstructor();
if (!$constructor->acceptsNamedArguments()->yes()) {
return [];
}

$variants = $constructor->getVariants();
if (count($variants) !== 1) {
return [];
Expand Down
130 changes: 130 additions & 0 deletions build/PHPStan/Build/SkipTestsWithRequiresPhpAttributeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php declare(strict_types = 1);

namespace PHPStan\Build;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPUnit\Framework\TestCase;
use function array_values;
use function count;
use function get_class;
use function sprintf;

/**
* @implements Rule<InClassMethodNode>
*/
final class SkipTestsWithRequiresPhpAttributeRule implements Rule
{

public function getNodeType(): string
{
return InClassMethodNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$methodReflection = $node->getMethodReflection();
if (!$methodReflection->getDeclaringClass()->is(TestCase::class)) {
return [];
}

$originalNode = $node->getOriginalNode();
if ($originalNode->stmts === null) {
return [];
}

if (count($originalNode->stmts) === 0) {
return [];
}

$firstStmt = $originalNode->stmts[0];
if (!$firstStmt instanceof Node\Stmt\If_) {
return [];
}

if (!$firstStmt->cond instanceof Node\Expr\BinaryOp) {
return [];
}

switch (get_class($firstStmt->cond)) {
case Node\Expr\BinaryOp\SmallerOrEqual::class:
$inverseBinaryOpSigil = '>';
break;
case Node\Expr\BinaryOp\Smaller::class:
$inverseBinaryOpSigil = '>=';
break;
case Node\Expr\BinaryOp\GreaterOrEqual::class:
$inverseBinaryOpSigil = '<';
break;
case Node\Expr\BinaryOp\Greater::class:
$inverseBinaryOpSigil = '<=';
break;
case Node\Expr\BinaryOp\Identical::class:
$inverseBinaryOpSigil = '!==';
break;
case Node\Expr\BinaryOp\NotIdentical::class:
$inverseBinaryOpSigil = '===';
break;
default:
throw new ShouldNotHappenException('No inverse comparison specified for ' . get_class($firstStmt->cond));
}

if (!$firstStmt->cond->left instanceof Node\Expr\ConstFetch || $firstStmt->cond->left->name->toString() !== 'PHP_VERSION_ID') {
return [];
}

if (!$firstStmt->cond->right instanceof Node\Scalar\Int_) {
return [];
}

if (count($firstStmt->stmts) !== 1) {
return [];
}

$ifStmt = $firstStmt->stmts[0];
if (!$ifStmt instanceof Node\Stmt\Expression) {
return [];
}

if (!$ifStmt->expr instanceof Node\Expr\StaticCall && !$ifStmt->expr instanceof Node\Expr\MethodCall) {
return [];
}

if (!$ifStmt->expr->name instanceof Node\Identifier || $ifStmt->expr->name->toLowerString() !== 'marktestskipped') {
return [];
}

$phpVersion = new PhpVersion($firstStmt->cond->right->value);

return [
RuleErrorBuilder::message('Skip tests with #[RequiresPhp] attribute intead.')
->identifier('phpstan.skipTestsRequiresPhp')
->line($firstStmt->getStartLine())
->fixNode($originalNode, static function (Node\Stmt\ClassMethod $node) use ($phpVersion, $inverseBinaryOpSigil) {
$stmts = $node->stmts;
if ($stmts === null) {
return $node;
}

unset($stmts[0]);
$node->stmts = array_values($stmts);
$node->attrGroups[] = new Node\AttributeGroup([
new Attribute(new Node\Name\FullyQualified('PHPUnit\\Framework\\Attributes\\RequiresPhp'), [
new Node\Arg(new Node\Scalar\String_(sprintf('%s %s', $inverseBinaryOpSigil, $phpVersion->getVersionString()))),
]),
]);

return $node;
})
->build(),
];
}


}
1 change: 1 addition & 0 deletions build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ rules:
- PHPStan\Build\AttributeNamedArgumentsRule
- PHPStan\Build\NamedArgumentsRule
- PHPStan\Build\OverrideAttributeThirdPartyMethodRule
- PHPStan\Build\SkipTestsWithRequiresPhpAttributeRule

services:
-
Expand Down
2 changes: 1 addition & 1 deletion compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.0",
"nette/neon": "^3.0.0",
"ondrejmirtes/simple-downgrader": "^2.1.7",
"ondrejmirtes/simple-downgrader": "^2.1.8",
"seld/phar-utils": "^1.2",
"symfony/console": "^5.4.43",
"symfony/filesystem": "^5.4.43",
Expand Down
22 changes: 11 additions & 11 deletions compiler/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading