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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\SomeTypedService;

final class AlsoArrowFunction
{
public function __construct(
private SomeTypedService $someTypedService
) {
}

public function go()
{
$closure = fn ($value) => $this->someTypedService->run($value);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\SomeTypedService;

final class AlsoArrowFunction
{
public function __construct(
private SomeTypedService $someTypedService
) {
}

public function go()
{
$closure = fn (string $value) => $this->someTypedService->run($value);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\SomeTypedService;

final class AlsoClosure
{
public function __construct(
private SomeTypedService $someTypedService
) {
}

public function go()
{
$closure = function ($value) {
$this->someTypedService->run($value);
};
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\SomeTypedService;

final class AlsoClosure
{
public function __construct(
private SomeTypedService $someTypedService
) {
}

public function go()
{
$closure = function (string $value) {
$this->someTypedService->run($value);
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public function __construct(
public function matchMask(ClassMethod|Function_ $functionLike, string $variableName, string $mask): bool
{
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $functionLike->stmts, FuncCall::class);
$funcCalls = array_values(array_filter($funcCalls, fn(FuncCall $funcCall): bool => $this->nodeNameResolver->isName($funcCall->name, 'sprintf')));
$funcCalls = array_values(
array_filter($funcCalls, fn (FuncCall $funcCall): bool => $this->nodeNameResolver->isName(
$funcCall->name,
'sprintf'
))
);

if (count($funcCalls) !== 1) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpParser\Node\BetterNodeFinder;
Expand Down Expand Up @@ -96,33 +98,25 @@ public function go(string $value)
*/
public function getNodeTypes(): array
{
return [Class_::class];
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
}

/**
* @param Class_ $node
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;

foreach ($node->getMethods() as $classMethod) {
if ($this->shouldSkipClassMethod($classMethod)) {
continue;
}

/** @var array<StaticCall|MethodCall|FuncCall> $callers */
$callers = $this->betterNodeFinder->findInstancesOf(
$classMethod,
[StaticCall::class, MethodCall::class, FuncCall::class]
);

$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $callers);
if ($hasClassMethodChanged) {
$hasChanged = true;
}
if ($node instanceof ClassMethod && $this->shouldSkipClassMethod($node)) {
return null;
}

/** @var array<StaticCall|MethodCall|FuncCall> $callers */
$callers = $this->betterNodeFinder->findInstancesOf(
$node,
[StaticCall::class, MethodCall::class, FuncCall::class]
);

$hasChanged = $this->refactorFunctionLike($node, $callers);
if ($hasChanged) {
return $node;
}
Expand All @@ -139,8 +133,10 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod);
}

private function shouldSkipParam(Param $param, ClassMethod $classMethod): bool
{
private function shouldSkipParam(
Param $param,
ClassMethod|Function_|Closure|ArrowFunction $functionLike
): bool {
// already has type, skip
if ($param->type instanceof Node) {
return true;
Expand All @@ -150,18 +146,24 @@ private function shouldSkipParam(Param $param, ClassMethod $classMethod): bool
return true;
}

return ! $this->paramTypeAddGuard->isLegal($param, $classMethod);
if (! $functionLike instanceof ClassMethod) {
return false;
}

return ! $this->paramTypeAddGuard->isLegal($param, $functionLike);
}

/**
* @param array<StaticCall|MethodCall|FuncCall> $callers
*/
private function refactorClassMethod(ClassMethod $classMethod, array $callers): bool
{
private function refactorFunctionLike(
ClassMethod|Closure|Function_|ArrowFunction $functionLike,
array $callers
): bool {
$hasChanged = false;

foreach ($classMethod->params as $param) {
if ($this->shouldSkipParam($param, $classMethod)) {
foreach ($functionLike->params as $param) {
if ($this->shouldSkipParam($param, $functionLike)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ChangesReporting/Output/GitHubOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function sanitizeAnnotationProperties(array $annotationProperties): stri
);

$sanitizedProperties = array_map(
fn (string $key, $value): string => sprintf('%s=%s', $key, $this->sanitizeAnnotationProperty($value)),
fn (string $key, string|int|null $value): string => sprintf('%s=%s', $key, $this->sanitizeAnnotationProperty($value)),
array_keys($nonNullProperties),
$nonNullProperties
);
Expand Down
Loading