Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodingStyle] FunctionLikeToFirstClassCallableRector #6667

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo)
{
return FooBar::foo($foo);
};

fn ($foo) => FooBar::foo($foo);

$bar = null;

fn ($foo) => $bar->foo($foo);

function ($foo) use ($bar)
{
return $bar->foo($foo);
};

function ($foo, $bar, $ray)
{
return FooBar::foo($foo, $bar);
};

fn ($foo, $bar, $ray) => FooBar::foo($foo);

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

FooBar::foo(...);

FooBar::foo(...);

$bar = null;

$bar->foo(...);

$bar->foo(...);

FooBar::foo(...);

FooBar::foo(...);

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function (&$foo)
{
return Foo::foo($foo);
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo)
{
return $foo->foo();
};

function ($foo)
{
return (new $foo)->foo();
};

fn ($foo) => $foo->foo();

function ($foo)
{
return $foo::foo();
};

function ($foo)
{
return ($foo . '\\Foo')::foo();
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo)
{
FooBar::foo($foo);
};

$bar = null;

function ($foo) use ($bar)
{
$bar->foo($foo);
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo)
{
$foo = 1;
return FooBar::foo($foo);
};

fn ($foo) => FooBar::foo($foo) && true;

$bar = null;

function ($foo) use ($bar)
{
$foo = 1;
return $bar->foo($foo);
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo)
{
return $foo;
};

fn ($foo) => $foo;

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function ($foo, $bar)
{
return FooBar::foo($bar, $foo);
};

fn ($foo, $bar) => FooBar::foo($bar, $foo);

$bar = null;

fn ($foo, $bar) => $bar->foo($bar, $foo);

function ($foo, $barFoo) use ($bar)
{
return $bar->foo($barFoo, $foo);
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

$bar = null;

function ($foo) use ($bar)
{
return $bar->foo(foo: $foo);
};

function ($foo)
{
return Foo::foo(foo: $foo);
};

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

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

function (...$foo)
{
return FooBar::foo(...$foo);
};

fn (...$foo) => FooBar::foo(...$foo);

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;

FooBar::foo(...);

FooBar::foo(...);

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

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FunctionLikeToFirstClassCallableRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([FunctionLikeToFirstClassCallableRector::class]);
Loading
Loading