Skip to content

Commit

Permalink
[CodeQuality] Adds rules for the with and value helpers (#175)
Browse files Browse the repository at this point in the history
* Adds rules for the with and value helpers

* cs fixes
  • Loading branch information
peterfox authored Feb 5, 2024
1 parent f647f0e commit 4d9188b
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 1 deletion.
28 changes: 27 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 51 Rules Overview
# 53 Rules Overview

## AddArgumentDefaultValueRector

Expand Down Expand Up @@ -907,6 +907,32 @@ Removes the `$model` property from Factories.

<br>

## RemoveRedundantValueCallsRector

Removes redundant value helper calls

- class: [`RectorLaravel\Rector\FuncCall\RemoveRedundantValueCallsRector`](../src/Rector/FuncCall/RemoveRedundantValueCallsRector.php)

```diff
-value(new Object())->something();
+(new Object())->something();
```

<br>

## RemoveRedundantWithCallsRector

Removes redundant with helper calls

- class: [`RectorLaravel\Rector\FuncCall\RemoveRedundantWithCallsRector`](../src/Rector/FuncCall/RemoveRedundantWithCallsRector.php)

```diff
-with(new Object())->something();
+(new Object())->something();
```

<br>

## ReplaceAssertTimesSendWithAssertSentTimesRector

Replace assertTimesSent with assertSentTimes
Expand Down
61 changes: 61 additions & 0 deletions src/Rector/FuncCall/RemoveRedundantValueCallsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace RectorLaravel\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ClosureType;
use PHPStan\Type\MixedType;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\RemoveRedundantValueCallsRectorTest
*/
class RemoveRedundantValueCallsRector extends AbstractScopeAwareRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Removes redundant value helper calls', [
new CodeSample(
'value(new Object())->something();',
'(new Object())->something();'
),
]);
}

public function getNodeTypes(): array
{
return [FuncCall::class];
}

public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if (! $node instanceof FuncCall) {
return null;
}

if (! $node->name instanceof Name) {
return null;
}

if (! $this->isName($node->name, 'value')) {
return null;
}

$args = $node->getArgs();

if (count($args) !== 1) {
return null;
}

if ($scope->getType($args[0]->value)->isSuperTypeOf(new ClosureType([], new MixedType, true))->no() === false) {
return null;
}

return $args[0]->value;
}
}
63 changes: 63 additions & 0 deletions src/Rector/FuncCall/RemoveRedundantWithCallsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace RectorLaravel\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\RemoveRedundantWithCallsRectorTest
*/
class RemoveRedundantWithCallsRector extends AbstractScopeAwareRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Removes redundant with helper calls', [
new CodeSample(
'with(new Object())->something();',
'(new Object())->something();'
),
]);
}

public function getNodeTypes(): array
{
return [FuncCall::class];
}

public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if (! $node instanceof FuncCall) {
return null;
}

if (! $node->name instanceof Name) {
return null;
}

if (! $this->isName($node->name, 'with')) {
return null;
}

$args = $node->getArgs();

if (count($args) < 1 || count($args) > 2) {
return null;
}

if (count($args) === 2) {
$secondArgumentType = $scope->getType($args[1]->value);

if ($secondArgumentType->isCallable()->no() === false) {
return null;
}
}

return $args[0]->value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Source\HelperObject;

value(new HelperObject())->store();

$foo = value(new HelperObject());

$user = 'a';

value($user)->store();

$foo = value($user);

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Source\HelperObject;

(new HelperObject())->store();

$foo = new HelperObject();

$user = 'a';

$user->store();

$foo = $user;

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

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture;

value(new UndefinedClass())->store();

$foo = value(new UndefinedClass());

$user = new UndefinedClass();

value($user)->store();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector;

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

final class RemoveRedundantValueCallsRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Source;

class HelperObject
{
public function store(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\FuncCall\RemoveRedundantValueCallsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RemoveRedundantValueCallsRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;

with(new HelperObject())->store();

$foo = with(new HelperObject());

$user = 'a';

with($user)->store();

$foo = with($user);

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;

(new HelperObject())->store();

$foo = new HelperObject();

$user = 'a';

$user->store();

$foo = $user;

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

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;

with(new HelperObject(), function () {

})->store();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

with(new UndefinedClass(), new AnotherUndefinedType())->store();

$foo = with(new UndefinedClass(), new AnotherUndefinedType());

$user = new UndefinedClass();
$bar = new AnotherUndefinedType();

with($user, $bar)->store();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector;

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

final class RemoveRedundantWithCallsRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source;

class HelperObject
{
public function store(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\FuncCall\RemoveRedundantWithCallsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RemoveRedundantWithCallsRector::class);
};

0 comments on commit 4d9188b

Please sign in to comment.