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 @@ -2,12 +2,15 @@

namespace Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Fixture;

use Illuminate\Support\Facades\Response;
use Nette\Utils\FileSystem;

class InstantMakeInStaticMethod
{
public static function run()
{
Response::view('example', ['new_example' => 123])->render();

return FileSystem::write('file', 'content');
}
}
Expand All @@ -18,12 +21,15 @@ class InstantMakeInStaticMethod

namespace Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Fixture;

use Illuminate\Support\Facades\Response;
use Nette\Utils\FileSystem;

class InstantMakeInStaticMethod
{
public static function run()
{
(new \Illuminate\Contracts\Routing\ResponseFactory())->view('example', ['new_example' => 123])->render();

return (new \Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Source\TargetFileSystem())->dumpFile('file', 'content');
}
}
Expand Down
31 changes: 21 additions & 10 deletions rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,7 @@ public function refactor(Node $node): ?Node
$staticCallToMethodCall->getClassObjectType(),
);

if ($staticCallToMethodCall->getMethodName() === '*') {
$methodName = $this->getName($node->name);
} else {
$methodName = $staticCallToMethodCall->getMethodName();
}

if (! is_string($methodName)) {
throw new ShouldNotHappenException();
}
$methodName = $this->getMethodName($node, $staticCallToMethodCall);

$hasChanged = true;

Expand Down Expand Up @@ -161,12 +153,31 @@ public function configure(array $configuration): void
$this->staticCallsToMethodCalls = $configuration;
}

private function getMethodName(
StaticCall $staticCall,
StaticCallToMethodCall $staticCallToMethodCall
): string {
if ($staticCallToMethodCall->getMethodName() === '*') {
$methodName = $this->getName($staticCall->name);
} else {
$methodName = $staticCallToMethodCall->getMethodName();
}

if (! is_string($methodName)) {
throw new ShouldNotHappenException();
}

return $methodName;
}

private function refactorToInstanceCall(
StaticCall $staticCall,
StaticCallToMethodCall $staticCallToMethodCall
): MethodCall {
$new = new New_(new FullyQualified($staticCallToMethodCall->getClassType()));

return new MethodCall($new, $staticCallToMethodCall->getMethodName(), $staticCall->args);
$methodName = $this->getMethodName($staticCall, $staticCallToMethodCall);

return new MethodCall($new, $methodName, $staticCall->args);
}
}