generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from spatie/rector
Add rector rule to remove ray calls from a codebase
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
# Check if the argument is provided | ||
if [ -z "$1" ]; then | ||
echo "Usage: $0 <target>" | ||
exit 1 | ||
fi | ||
|
||
# Execute the Rector command with the provided target | ||
vendor/bin/rector process "$1" --config vendor/spatie/ray/remove-ray-rector.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Automatically remove ray calls | ||
weight: 1 | ||
--- | ||
|
||
To avoid shipping your code with some `ray()` calls in it you can automate the removal of all `ray()` calls in your codebase. | ||
|
||
## Rector | ||
If you are already using [Rector](https://getrector.com/) this can be simply done by adding a rule to your `rector.php` config file: | ||
|
||
```php | ||
use Spatie\Ray\Rector\RemoveRayCallRector; | ||
|
||
$rectorConfig->rule(RemoveRayCallRector::class); | ||
``` | ||
|
||
## If you are not using Rector | ||
If you are not using Rector you can use the script provided by the package: | ||
|
||
```bash | ||
./vendor/bin/remove-ray.sh <path> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Rector\Config\RectorConfig; | ||
use Spatie\Ray\Rector\RemoveRayCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveRayCallRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Spatie\Ray\Rector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\NodeTraverser; | ||
use Rector\Core\Contract\Rector\RectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class RemoveRayCallRector extends AbstractRector implements RectorInterface | ||
{ | ||
public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove Ray calls', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' | ||
$x = 'something'; | ||
ray($x); | ||
CODE_SAMPLE | ||
, <<<'CODE_SAMPLE' | ||
$x = 'something'; | ||
CODE_SAMPLE | ||
, ['ray'])]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Expression::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?int | ||
{ | ||
$expr = $node->expr; | ||
|
||
if (! $expr instanceof FuncCall && ! $expr instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
if ($this->isName($expr->name, 'ray')) { | ||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
|
||
if ($expr->var->name->parts && in_array('ray', $expr->var->name->parts)) { | ||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
|
||
return null; | ||
} | ||
} |