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

Livewire computed property class method to Computed Attribute #236

Merged
merged 6 commits into from
Jul 31, 2024
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
2 changes: 2 additions & 0 deletions config/sets/packages/livewire/livewire-30.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Class_\RenameAttributeRector;
use Rector\Renaming\ValueObject\RenameAttribute;
use RectorLaravel\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector;
use RectorLaravel\Rector\Class_\LivewireComponentQueryStringToUrlAttributeRector;

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

$rectorConfig->rule(LivewireComponentQueryStringToUrlAttributeRector::class);
$rectorConfig->rule(LivewireComponentComputedMethodToComputedAttributeRector::class);

$rectorConfig->ruleWithConfiguration(RenameAttributeRector::class, [
new RenameAttribute('Livewire\Attributes\Rule', 'Livewire\Attributes\Validate'),
Expand Down
21 changes: 21 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,27 @@ Change method calls from `$this->json` to `$this->postJson,` `$this->putJson,` e

<br>

## LivewireComponentComputedMethodToComputedAttributeRector

Converts the computed methods of a Livewire component to use the Computed Attribute

- class: [`RectorLaravel\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector`](../src/Rector/Class_/LivewireComponentComputedMethodToComputedAttributeRector.php)

```diff
use Livewire\Component;

class MyComponent extends Component
{
- public function getFooBarProperty()
+ #[\Livewire\Attributes\Computed]
+ public function fooBar()
{
}
}
```

<br>

## LivewireComponentQueryStringToUrlAttributeRector

Converts the `$queryString` property of a Livewire component to use the Url Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace RectorLaravel\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\LivewireComponentComputedMethodToComputedAttributeRectorTest
*/
final class LivewireComponentComputedMethodToComputedAttributeRector extends AbstractRector
{
private const COMPUTED_ATTRIBUTE = 'Livewire\Attributes\Computed';

private const COMPONENT_CLASS = 'Livewire\Component';

private const METHOD_PATTERN = '/^get(?\'methodName\'[\w]*)Property$/';

public function __construct(private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer)
{

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Converts the computed methods of a Livewire component to use the Computed Attribute',
[
new CodeSample(<<<'CODE_SAMPLE'
use Livewire\Component;

class MyComponent extends Component
{
public function getFooBarProperty()
{
}
}
CODE_SAMPLE,
<<<'CODE_SAMPLE'
use Livewire\Component;

class MyComponent extends Component
{
#[\Livewire\Attributes\Computed]
public function fooBar()
{
}
}
CODE_SAMPLE
),
]
);
}

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

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->isObjectType($node, new ObjectType(self::COMPONENT_CLASS))) {
return null;
}

$changes = false;

foreach ($node->stmts as $stmt) {
if (
$stmt instanceof ClassMethod &&
$stmt->isPublic() &&
(bool) preg_match(self::METHOD_PATTERN, $this->getName($stmt), $matches)) {
$methodName = lcfirst($matches['methodName']);

if ($this->methodExistsInClass($node, $methodName)) {
continue;
}

$this->addComputedAttributeToClassMethodAndRename($stmt, $methodName);
$changes = true;
}
}

if ($changes === false) {
return null;
}

return $node;
}

private function addComputedAttributeToClassMethodAndRename(ClassMethod $classMethod, string $name): void
{
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::COMPUTED_ATTRIBUTE)) {
return;
}

$classMethod->attrGroups[] = new AttributeGroup([
new Attribute(
new FullyQualified(self::COMPUTED_ATTRIBUTE)
),
]);

$classMethod->name = new Identifier($name);
}

private function methodExistsInClass(Class_ $class, string $methodName): bool
{
return $this->getType($class)->hasMethod($methodName)->yes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\Fixture;

use Livewire\Component;

class MyComponent extends Component
{
public function getFooBarProperty()
{

}

public function getBarFooProperty()
{

}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\Fixture;

use Livewire\Component;

class MyComponent extends Component
{
#[\Livewire\Attributes\Computed]
public function fooBar()
{

}

#[\Livewire\Attributes\Computed]
public function barFoo()
{

}
}

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

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\Fixture;

use Livewire\Component;

class SkipIfMethodAlreadyExists extends Component
{
public function getUserProperty()
{

}

public function user()
{

}
}

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

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\Fixture;

class SkipNonComponent
{
public function getBarFooProperty()
{

}
}

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

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\Fixture;

use Livewire\Component;

class MyComponent extends Component
{
private function getFooBarProperty()
{

}

protected function getBarFooProperty()
{

}
}

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

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector;

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

final class LivewireComponentComputedMethodToComputedAttributeRectorTest 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,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector;

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

$rectorConfig->rule(LivewireComponentComputedMethodToComputedAttributeRector::class);
};
9 changes: 9 additions & 0 deletions tests/Sets/Livewire30/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class FixtureComponent extends Component
{
#[\Livewire\Attributes\Rule]
public $email;

public function getFooBarProperty()
{
}
}

?>
Expand All @@ -22,6 +26,11 @@ class FixtureComponent extends Component
{
#[\Livewire\Attributes\Validate]
public $email;

#[\Livewire\Attributes\Computed]
public function fooBar()
{
}
}

?>
Loading