Skip to content

Commit

Permalink
fix non-existing method trying to get method reflection (rectorphp#4505)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Oct 29, 2020
1 parent 3580436 commit 252f178
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions rules/generic/src/NodeTypeAnalyzer/CallTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ private function getMethodParameterTypes(string $className, Node $node): array
return [];
}

// method not found
if (! $classReflection->hasMethod($methodName)) {
return [];
}

$methodReflection = $classReflection->getMethod($methodName, $scope);
$functionVariant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Generic\Tests\Rector\MethodCall\FormerNullableArgumentToScalarTypedRector\Fixture;

use Nette\Application\UI\Form;

class SkipNetteFormClassCall
{
public function create()
{
$form = new Form;

$form->addText('reading', 'Reading')
->setRequired('...')
->getControlPrototype()
->nonExistingMagic('rating');

return $form;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function process()
}
CODE_SAMPLE
, [
self::METHOD_ORDER_BY_INTERFACES => [
'FoodRecipeInterface' => ['getDescription', 'process'],
self::METHOD_ORDER_BY_INTERFACES => [
'FoodRecipeInterface' => ['getDescription', 'process'],
],
]),
]);
Expand Down Expand Up @@ -104,14 +104,14 @@ public function refactor(Node $node): ?Node
foreach ($implementedInterfaces as $implementedInterface) {
$methodOrder = $this->methodOrderByInterfaces[$implementedInterface] ?? null;
if ($methodOrder === null) {
continue;
continue;
}

$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($publicMethodOrderByKey, $methodOrder);

// nothing to re-order
if (array_keys($oldToNewKeys) === array_values($oldToNewKeys)) {
return null;
return null;
}

$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
Expand All @@ -136,11 +136,11 @@ private function collectPublicMethods(Class_ $class): array

foreach ($class->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
continue;
}

if (! $classStmt->isPublic()) {
continue;
continue;
}

/** @var string $classMethodName */
Expand Down

0 comments on commit 252f178

Please sign in to comment.