Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/run_on_project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php_version: ['8.2']
php_version: ['8.4']

steps:
-
Expand All @@ -24,7 +24,7 @@ jobs:

- run: composer require rector/argtyper --ansi

# clone laravel project with dependencies
# clone symfony/console project with dependencies
- run: git clone https://github.com/symfony/console.git --depth=1
- run: composer install --working-dir console --ansi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function refactor(Node $node): ?Class_
continue;
}

// keep nullability if the param is already nullable or implicitly nullable via a null default,
// otherwise the "?" would be dropped and the param would become implicitly nullable (deprecated)
// a null default value implies the type must stay nullable
$isNullable = $classMethodType->isNullable()
|| $param->type instanceof NullableType
|| $this->hasNullDefault($param);

$typeNode = TypeResolver::resolveTypeNode($classMethodType->getType());

if ($this->shouldSkipOverride($param, $classMethodType)) {
Expand Down
14 changes: 13 additions & 1 deletion src/Rector/Rector/Function_/AddFunctionParamTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\ArgTyper\Rector\Rector\Function_;

use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -64,7 +66,8 @@ public function refactor(Node $node): ?Function_
continue;
}

$isNullable = $paramFunctionType->isNullable();
// a null default value implies the type must stay nullable
$isNullable = $paramFunctionType->isNullable() || $this->hasDefaultNull($param);
$typeNode = TypeResolver::resolveTypeNode($paramFunctionType->getType());

if ($paramFunctionType->isObjectType() && ($param->type instanceof Name || ($param->type instanceof NullableType && $param->type->type instanceof Name))) {
Expand All @@ -87,4 +90,13 @@ public function refactor(Node $node): ?Function_

return $node;
}

private function hasDefaultNull(Param $param): bool
{
if (! $param->default instanceof ConstFetch) {
return false;
}

return $param->default->name->toLowerString() === 'null';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\ArgTyper\Configuration\CallLikeTypesConfigurationProvider;
use Rector\ArgTyper\Rector\ValueObject\ClassMethodType;
use Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture\AddNullableForDefaultNull;
use Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture\AddNullableScalarFromNullDefault;
use Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture\KeepDateTimeInterface;
use Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture\KeepNullableDateTimeInterface;
Expand All @@ -35,6 +36,7 @@ public function test(string $filePath): void
new ClassMethodType(KeepNullableDateTimeInterface::class, 'record', 0, 'object:' . \DateTime::class),
new ClassMethodType(KeepDateTimeInterface::class, 'record', 0, 'object:' . \DateTime::class),
new ClassMethodType(SkipIntToFloatOverride::class, 'passInteger', 0, IntegerType::class),
new ClassMethodType(AddNullableForDefaultNull::class, 'run', 0, StringType::class),
new ClassMethodType(KeepNullableScalarParam::class, 'translate', 0, StringType::class),
new ClassMethodType(AddNullableScalarFromNullDefault::class, 'translate', 0, StringType::class),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture;

final class AddNullableForDefaultNull
{
public function run($value = null)
{
}
}

?>
-----
<?php

namespace Rector\ArgTyper\Tests\Rector\Rector\ClassMethod\AddClassMethodParamTypeRector\Fixture;

final class AddNullableForDefaultNull
{
public function run(?string $value = null)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function test(string $filePath): void
0,
StringType::class
),
new FuncCallType(
'Rector\ArgTyper\Tests\Rector\Rector\Function_\AddFunctionParamTypeRector\Fixture\defaultNullFunction',
0,
StringType::class
),
]);

$this->doTestFile($filePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\ArgTyper\Tests\Rector\Rector\Function_\AddFunctionParamTypeRector\Fixture;

function defaultNullFunction($item = null)
{
}

?>
-----
<?php

namespace Rector\ArgTyper\Tests\Rector\Rector\Function_\AddFunctionParamTypeRector\Fixture;

function defaultNullFunction(?string $item = null)
{
}

?>
Loading