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
Expand Up @@ -14,7 +14,6 @@
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeAnalyzer\CallAnalyzer;
Expand All @@ -33,8 +32,7 @@ final class SimplifyUselessVariableRector extends AbstractScopeAwareRector
public function __construct(
private readonly AssignAndBinaryMap $assignAndBinaryMap,
private readonly VariableAnalyzer $variableAnalyzer,
private readonly CallAnalyzer $callAnalyzer,
private readonly ReflectionProvider $reflectionProvider
private readonly CallAnalyzer $callAnalyzer
) {
}

Expand Down Expand Up @@ -167,7 +165,7 @@ private function shouldSkipStmt(Return_ $return, Stmt $previousStmt, Scope $scop

/** @var Variable $previousVar */
$previousVar = $previousNode->var;
if ($this->callAnalyzer->isNewInstance($previousVar, $this->reflectionProvider)) {
if ($this->callAnalyzer->isNewInstance($previousVar)) {
return true;
}

Expand Down
10 changes: 7 additions & 3 deletions src/NodeAnalyzer/CallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ final class CallAnalyzer
*/
private const OBJECT_CALL_TYPES = [MethodCall::class, NullsafeMethodCall::class, StaticCall::class];

public function __construct(private readonly ReflectionProvider $reflectionProvider)
{
}

public function isObjectCall(Expr $expr): bool
{
if ($expr instanceof BooleanNot) {
Expand Down Expand Up @@ -60,7 +64,7 @@ public function doesIfHasObjectCall(array $ifs): bool
return false;
}

public function isNewInstance(Variable $variable, ReflectionProvider $reflectionProvider): bool
public function isNewInstance(Variable $variable): bool
{
$scope = $variable->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
Expand All @@ -73,11 +77,11 @@ public function isNewInstance(Variable $variable, ReflectionProvider $reflection
}

$className = $type->getClassName();
if (! $reflectionProvider->hasClass($className)) {
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $reflectionProvider->getClass($className);
$classReflection = $this->reflectionProvider->getClass($className);
return $classReflection->getNativeReflection()
->isInstantiable();
}
Expand Down