Skip to content
Open
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,39 @@
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

final class SkipDefinedIntOrString
{
/**
* @var array<int, array{unique_key: int|string, label: string}>
*/
private array $todos = [
0 => [
'unique_key' => 1,
'label' => 'test 1',
],
1 => [
'unique_key' => 'fallback_todo',
'label' => 'fallback todo 1',
],
];

/**
* @var array<int|string, string>
*/
public array $selectedTodos = [];

public function run(int $index): string
{
if (!array_key_exists($index, $this->todos)) {
return 'Todo not found';
}

if (!array_key_exists($this->todos[$index]['unique_key'], $this->selectedTodos)) {
$this->selectedTodos[$this->todos[$index]['unique_key']] = $this->todos[$index]['label'];
return 'Todo selected !';
}

return 'Todo already selected !';
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this fixture simpler, but keep its point? It's quite long to read/understand

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\Php85\Rector\FuncCall;

use PHPStan\Type\UnionType;
use PHPStan\Type\TypeCombinator;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -88,12 +90,21 @@ public function refactor(Node $node): ?Node
return null;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($functionReflection, $node, $scope);
$argPosition = $this->argsAnalyzer->resolveArgPosition($args, 'key', 0);
$originalType = $this->getType($args[$argPosition]->value);

if ($originalType instanceof UnionType) {
$withoutNullParameterType = TypeCombinator::removeNull($originalType);
if ($withoutNullParameterType->equals($originalType)) {
return null;
}
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($functionReflection, $node, $scope);
$result = $this->nullToStrictStringIntConverter->convertIfNull(
$node,
$args,
$this->argsAnalyzer->resolveArgPosition($args, 'key', 0),
$argPosition,
$isTrait,
$scope,
$parametersAcceptor
Expand Down