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

Few PHPStan fixes #5900

Merged
merged 1 commit into from
May 19, 2024
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
10 changes: 6 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ parameters:
- src/StaticTypeMapper/ValueObject/Type/*Type.php

# generics nullable bugs
- '#Method (.*?) should return (.*?)\|null but returns PhpParser\\Node\|null#'
-
message: '#Method (.*?) should return (.*?)\|null but returns PhpParser\\Node\|null#'
path: src/PhpParser/Node/BetterNodeFinder.php

- '#Method (.*?) should return array<T of PhpParser\\Node\> but returns array<PhpParser\\Node\>#'

- '#Property Rector\\PhpParser\\Node\\AssignAndBinaryMap\:\:\$binaryOpToAssignClasses \(array<class\-string<PhpParser\\Node\\Expr\\BinaryOp\>, class\-string<PhpParser\\Node\\Expr\\BinaryOp\>\>\) does not accept array#'
Expand Down Expand Up @@ -121,6 +124,8 @@ parameters:
# array_index on generic types
- '#Method Rector\\NodeTypeResolver\\PHPStan\\Type\\TypeFactory\:\:uniquateTypes\(\) should return array<TType of PHPStan\\Type\\Type\> but returns array<int, PHPStan\\Type\\Type\>#'

- '#Method Rector\\Arguments\\ArgumentDefaultValueReplacer\:\:processReplaces\(\) should return \(TCall of PhpParser\\Node\\Expr\\FuncCall\|PhpParser\\Node\\Expr\\MethodCall\|PhpParser\\Node\\Expr\\New_\|PhpParser\\Node\\Expr\\StaticCall\|PhpParser\\Node\\Stmt\\ClassMethod\)\|null but returns PhpParser\\Node\\Stmt\\ClassMethod\|null#'

# native filesystem calls, required for performance reasons
-
message: '#"@\\unlink\(\$tmpPath\)" is forbidden to use#'
Expand Down Expand Up @@ -160,9 +165,6 @@ parameters:
- rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
- src/PhpParser/NodeTransformer.php

# impossible to validate json string is a class-string
- '#Parameter \#1 \$rectorClass of class Rector\\ChangesReporting\\ValueObject\\RectorWithLineChange constructor expects class\-string<Rector\\Contract\\Rector\\RectorInterface\>\|Rector\\Contract\\Rector\\RectorInterface, string given#'

# mapper re-use
- '#Parameter \#1 \$type of method Rector\\PHPStanStaticTypeMapper\\TypeMapper\\ObjectWithoutClassTypeMapper\:\:mapToPhpParserNode\(\) expects PHPStan\\Type\\ObjectWithoutClassType, PHPStan\\Type\\Accessory\\Has(Property|Method)Type given#'

Expand Down
15 changes: 13 additions & 2 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Arguments;

use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
Expand All @@ -27,10 +26,16 @@ public function __construct(
) {
}

/**
* @template TCall as (MethodCall|StaticCall|ClassMethod|FuncCall|New_)
*
* @param TCall $node
* @return TCall|null
*/
public function processReplaces(
MethodCall | StaticCall | ClassMethod | FuncCall | New_ $node,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
): ?Node {
): MethodCall | StaticCall | ClassMethod | FuncCall | New_ |null {
if ($node instanceof ClassMethod) {
if (! isset($node->params[$replaceArgumentDefaultValue->getPosition()])) {
return null;
Expand Down Expand Up @@ -82,6 +87,12 @@ private function processParams(
return $classMethod;
}

/**
* @template TCall as (MethodCall|StaticCall|FuncCall|New_)
*
* @param TCall $expr
* @return TCall|null
*/
private function processArgs(
MethodCall | StaticCall | FuncCall | New_ $expr,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
Expand Down
1 change: 1 addition & 0 deletions rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(

/**
* @param array<string, string> $oldToNewClasses
* @return ($node is FullyQualified ? FullyQualified : Node)
*/
public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope): ?Node
{
Expand Down
1 change: 1 addition & 0 deletions src/ChangesReporting/ValueObject/RectorWithLineChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getRectorClass(): string
*/
public static function decode(array $json): self
{
/** @var class-string<RectorInterface> $rectorClass */
$rectorClass = $json[self::KEY_RECTOR_CLASS];
Assert::string($rectorClass);

Expand Down
6 changes: 4 additions & 2 deletions src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ private function resolveNullableType(NullableType $nullableType): null|NullableT
/**
* @param TypeKind::* $typeKind
*/
private function mapNullabledType(Type $nullabledType, string $typeKind): ?Node
{
private function mapNullabledType(
Type $nullabledType,
string $typeKind
): NullableType|ComplexType|PhpParserUnionType|null {
// void cannot be nullable
if ($nullabledType->isVoid()->yes()) {
return null;
Expand Down
3 changes: 1 addition & 2 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeFinder;
Expand Down Expand Up @@ -134,7 +133,7 @@ public function find(Node | array $nodes, callable $filter): array
/**
* @api symfony
* @param Node[] $nodes
* @return ClassLike|null
* @return Class_|null
*/
public function findFirstNonAnonymousClass(array $nodes): ?Node
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Tests\NodeTypeResolver\StaticTypeMapper;

use PHPStan\Type\Type;
use Iterator;
use PhpParser\Node;
use PhpParser\Node\Identifier;
Expand All @@ -17,6 +16,7 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
Expand Down