Skip to content

Commit

Permalink
Merge pull request #884 from spatie/rector
Browse files Browse the repository at this point in the history
Add rector rule to remove ray calls from a codebase
  • Loading branch information
timvandijck committed Jan 24, 2024
2 parents 1c283d8 + 18f1303 commit 2ed3567
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bin/remove-ray.sh
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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"role": "Developer"
}
],
"bin": [
"bin/remove-ray.sh"
],
"require": {
"php": "^7.3|^8.0",
"ext-curl": "*",
Expand All @@ -31,6 +34,7 @@
"pestphp/pest": "^1.22",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.18.4",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/test-time": "^1.2"
},
Expand Down
22 changes: 22 additions & 0 deletions docs/advanced-usage/automatically-remove-ray-calls.md
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>
```
8 changes: 8 additions & 0 deletions remove-ray-rector.php
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);
};
52 changes: 52 additions & 0 deletions src/Rector/RemoveRayCallRector.php
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;
}
}

0 comments on commit 2ed3567

Please sign in to comment.