Skip to content

Commit

Permalink
Use rector-src#dev-upgrade-to-php-parser5-and-phpstan-2 (#408)
Browse files Browse the repository at this point in the history
* Use rector-src#dev-upgrade-to-php-parser5-and-phpstan-2

* [ci-review] Rector Rectify

* update node class usage

* [ci-review] Rector Rectify

* fix throw as expr

* [ci-review] Rector Rectify

* fix phpstan

* require dev phpstan webmozart v2 and fix phsptan message return

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Nov 20, 2024
1 parent b75c030 commit 5fcea91
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 60 deletions.
13 changes: 5 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
"php": ">=8.2"
},
"require-dev": {
"rector/rector-src": "dev-main",
"rector/rector-src": "dev-upgrade-to-php-parser5-and-phpstan-2",
"phpunit/phpunit": "^10.5",
"phpstan/phpstan": "^1.12",
"symplify/phpstan-rules": "^13.0",
"symplify/phpstan-extensions": "^11.4",
"phpstan/phpstan": "^2.0",
"symplify/easy-coding-standard": "^12.3",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-webmozart-assert": "^1.2",
"symplify/vendor-patches": "^11.3",
"tracy/tracy": "^2.10",
"tomasvotruba/class-leak": "^1.2",
"rector/type-perfect": "^1.0",
"rector/swiss-knife": "^1.0"
"rector/swiss-knife": "^1.0",
"phpstan/phpstan-webmozart-assert": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -43,7 +40,7 @@
"@docs",
"phpunit"
],
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
"phpstan": "vendor/bin/phpstan analyse --ansi",
"check-cs": "vendor/bin/ecs check --ansi",
"class-leak": "vendor/bin/class-leak check config src rules --skip-suffix \"Rector\"",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
Expand Down
35 changes: 29 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ parameters:
- */Fixture/*
- */Expected/*

# see https://github.com/rectorphp/type-perfect/
type_perfect:
no_mixed: true
null_over_false: true
narrow_param: true
narrow_return: true
# to be enabled later once rector upgraded to use phpstan v2
# # see https://github.com/rectorphp/type-perfect/
# type_perfect:
# no_mixed: true
# null_over_false: true
# narrow_param: true
# narrow_return: true

ignoreErrors:
# phpstan false positive
Expand All @@ -34,3 +35,25 @@ parameters:
- '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'

- '#PhpParser\\Node\\Stmt\\Expression is not generic#'

# more advanced usage, but not always working
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'

-
identifier: instanceof.alwaysTrue

-
identifier: argument.type

# phpstan instanceof
-
identifier: phpstanApi.instanceofAssumption

-
identifier: phpstanApi.varTagAssumption

-
identifier: assign.propertyType

- '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#'
8 changes: 4 additions & 4 deletions rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\Enum\ConsecutiveVariable;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function create(MethodCall $assertMethodCall, int $assertKey): array

$stmts = [new Expression($callbackAssign)];

$parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey));
$parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey));
$callbackFuncCall = new FuncCall($callbackVariable, [new Arg($parametersArrayDimFetch)]);

// add assert true to the callback
Expand All @@ -77,7 +77,7 @@ private function createAssertSameParameters(Expr $comparedExpr, int $assertKey):
// use assert same directly instead
$args = [
new Arg($comparedExpr),
new Arg(new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey))),
new Arg(new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey))),
];

$assertSameMethodCall = new MethodCall(new Variable('this'), new Identifier('assertSame'), $args);
Expand All @@ -90,7 +90,7 @@ private function createAssertSameParameters(Expr $comparedExpr, int $assertKey):
*/
private function createAssertNotEmpty(int $assertKey, string $emptyMethodName): array
{
$arrayDimFetch = new ArrayDimFetch(new Variable(ConsecutiveVariable::PARAMETERS), new LNumber($assertKey));
$arrayDimFetch = new ArrayDimFetch(new Variable(ConsecutiveVariable::PARAMETERS), new Int_($assertKey));

$assertEmptyMethodCall = new MethodCall(new Variable('this'), new Identifier($emptyMethodName), [
new Arg($arrayDimFetch),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -246,7 +247,7 @@ private function createMockedClassMethod(MethodCall $rootMethodCall, MethodCall
->value;

return new ClassMethod($methodName, [
'flags' => Class_::MODIFIER_PUBLIC,
'flags' => Modifiers::PUBLIC,
'stmts' => [new Return_($returnedExpr)],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
Expand Down Expand Up @@ -134,13 +135,13 @@ public function refactor(Node $node): ?Node
}

// remove static flag
$setUpBeforeClassMethod->flags -= Class_::MODIFIER_STATIC;
$setUpBeforeClassMethod->flags -= Modifiers::STATIC;

// remove public flag
$setUpBeforeClassMethod->flags -= Class_::MODIFIER_PUBLIC;
$setUpBeforeClassMethod->flags -= Modifiers::PUBLIC;

// make protected
$setUpBeforeClassMethod->flags += Class_::MODIFIER_PROTECTED;
$setUpBeforeClassMethod->flags += Modifiers::PROTECTED;

$setUpBeforeClassMethod->name = new Identifier('setUp');

Expand All @@ -150,7 +151,7 @@ public function refactor(Node $node): ?Node
}

if ($this->isNames($property, $changedPropertyNames)) {
$property->flags -= Class_::MODIFIER_STATIC;
$property->flags -= Modifiers::STATIC;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Nette\Utils\Json;
use Nette\Utils\Strings;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -174,7 +175,7 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): v
}

$providerMethod = new ClassMethod($dataProviderName);
$providerMethod->flags = Class_::MODIFIER_PUBLIC;
$providerMethod->flags = Modifiers::PUBLIC;
$providerMethod->stmts[] = new Return_($returnValue);
$this->classInsertManipulator->addAsFirstMethod($class, $providerMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\Float_;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFactory\AssertCallFactory;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node
$args = $node->getArgs();

$firstValue = $args[0]->value;
if (! $firstValue instanceof DNumber) {
if (! $firstValue instanceof Float_) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\InterpolatedString;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -152,7 +152,7 @@ private function isScalarOrEnumValue(Expr $expr): bool
return true;
}

if ($expr instanceof Encapsed) {
if ($expr instanceof InterpolatedString) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Expression;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -159,7 +159,7 @@ public function refactor(Node $node): ?Node

private function resolveOldCondition(Expr $expr): int
{
if ($expr instanceof LNumber) {
if ($expr instanceof Int_) {
return $expr->value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\PHPUnit\CodeQuality\ValueObject\MatchAndReturnMatch;
Expand Down Expand Up @@ -204,7 +204,7 @@ private function matchSingleMatchArmBodyWithConditionOne(Match_ $match): ?Expr

private function isNumberOne(Expr $expr): bool
{
if (! $expr instanceof LNumber) {
if (! $expr instanceof Int_) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

use PhpParser\BuilderFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\Enum\ConsecutiveVariable;
Expand Down Expand Up @@ -80,7 +80,7 @@ private function createAssertSameDimFetch(Arg $firstArg, Variable $variable): Me

$currentValueArrayDimFetch = new ArrayDimFetch($firstArg->value, new Minus(
$matcherCountMethodCall,
new LNumber(1)
new Int_(1)
));

$compareArgs = [new Arg($currentValueArrayDimFetch), new Arg($variable)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use Rector\Exception\ShouldNotHappenException;
use Rector\PHPUnit\Enum\ConsecutiveMethodName;
use Rector\PHPUnit\Enum\ConsecutiveVariable;
Expand Down Expand Up @@ -106,8 +106,9 @@ public function getNodeTypes(): array

/**
* @param Expression $node
* @return null|Stmt[]|Expression
*/
public function refactor(Node $node)
public function refactor(Node $node): null|array|Expression
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
Expand Down Expand Up @@ -166,7 +167,7 @@ public function refactor(Node $node)
if ($willThrowException instanceof MethodCall) {
$this->methodCallRemover->removeMethodCall($node, ConsecutiveMethodName::WILL_THROW_EXCEPTION);
$expr = $this->getFirstArgValue($willThrowException);
$returnStmt = new Throw_($expr);
$returnStmt = new Expression(new Throw_($expr));
}

$willReturnReferenceArgument = $this->methodCallNodeFinder->findByName(
Expand All @@ -188,8 +189,8 @@ public function refactor(Node $node)

if (! $expectsCall instanceof MethodCall && ! $expectsCall instanceof StaticCall) {
// fallback to default by case count
$lNumber = new LNumber(\count($withConsecutiveMethodCall->args));
$expectsCall = new MethodCall(new Variable('this'), new Identifier('exactly'), [new Arg($lNumber)]);
$int = new Int_(\count($withConsecutiveMethodCall->args));
$expectsCall = new MethodCall(new Variable('this'), new Identifier('exactly'), [new Arg($int)]);
}

// 2. does willReturnCallback() exist? just merge them together
Expand Down Expand Up @@ -221,7 +222,7 @@ public function refactor(Node $node)
*/
private function refactorToWillReturnCallback(
MethodCall $withConsecutiveMethodCall,
?Stmt $returnStmt,
Return_|Expression|null $returnStmt,
Expr|Variable|null $referenceVariable,
StaticCall|MethodCall $expectsCall,
Expression $expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private function refactorArrayKey(Array_ $array, array $dataProviderNameMapping)
$allArrayKeyNames = [];

foreach ($array->items as $arrayItem) {
if ($arrayItem?->key instanceof String_) {
if ($arrayItem->key instanceof String_) {
$needToSetAllKeyNames = true;
$allArrayKeyNames[] = $arrayItem->key->value;
}
Expand All @@ -230,10 +230,6 @@ private function refactorArrayKey(Array_ $array, array $dataProviderNameMapping)
}

foreach ($array->items as $arrayIndex => $arrayItem) {
if ($arrayItem === null) {
continue;
}

if (! isset($dataProviderNameMapping[$arrayIndex])) {
continue;
}
Expand Down Expand Up @@ -271,21 +267,21 @@ private function extractDataProviderArrayItem(ClassMethod $classMethod): iterabl
$dataProviderTestCases = $stmt->expr;

foreach ($dataProviderTestCases->items as $dataProviderTestCase) {
$arrayItem = $dataProviderTestCase?->value;
$arrayItem = $dataProviderTestCase->value;

if ($arrayItem instanceof Array_) {
yield $arrayItem;
}

$variableName = $arrayItem === null ? null : $this->getName($arrayItem);
$variableName = $this->getName($arrayItem);
if (
$arrayItem instanceof Variable
&& $variableName !== null
&& isset($resolvedVariables[$variableName])
) {
$dataProviderList = $resolvedVariables[$variableName];
foreach ($dataProviderList->items as $dataProviderItem) {
if ($dataProviderItem?->value instanceof Array_) {
if ($dataProviderItem->value instanceof Array_) {
yield $dataProviderItem->value;
}
}
Expand Down
Loading

0 comments on commit 5fcea91

Please sign in to comment.