Skip to content

Commit 4a13c85

Browse files
authored
[Laravel110] Blueprint geometry columns (#201)
* Adds RefactorBlueprintGeometryColumnsRector rule
1 parent cc9c626 commit 4a13c85

File tree

8 files changed

+184
-1
lines changed

8 files changed

+184
-1
lines changed

config/sets/laravel110.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use Rector\Config\RectorConfig;
66
use RectorLaravel\Rector\Class_\ModelCastsPropertyToCastsMethodRector;
7+
use RectorLaravel\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector;
78

89
// see https://laravel.com/docs/11.x/upgrade
910
return static function (RectorConfig $rectorConfig): void {
1011
$rectorConfig->import(__DIR__ . '/../config.php');
1112

1213
// https://github.com/laravel/framework/pull/47237
1314
$rectorConfig->rule(ModelCastsPropertyToCastsMethodRector::class);
15+
16+
// https://github.com/laravel/framework/pull/49634
17+
$rectorConfig->rule(RefactorBlueprintGeometryColumnsRector::class);
1418
};

docs/rector_rules_overview.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 60 Rules Overview
1+
# 61 Rules Overview
22

33
## AbortIfRector
44

@@ -921,6 +921,19 @@ Replace `redirect()->route("home")` and `Redirect::route("home")` with `to_route
921921

922922
<br>
923923

924+
## RefactorBlueprintGeometryColumnsRector
925+
926+
refactors calls with the pre Laravel 11 methods for blueprint geometry columns
927+
928+
- class: [`RectorLaravel\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector`](../src/Rector/MethodCall/RefactorBlueprintGeometryColumnsRector.php)
929+
930+
```diff
931+
-$blueprint->point('coordinates')->spatialIndex();
932+
+$blueprint->geometry('coordinates', 'point')->spatialIndex();
933+
```
934+
935+
<br>
936+
924937
## RemoveDumpDataDeadCodeRector
925938

926939
It will removes the dump data just like dd or dump functions from the code.`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace RectorLaravel\Rector\MethodCall;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Arg;
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Scalar\String_;
10+
use PHPStan\Type\ObjectType;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector\RefactorBlueprintGeometryColumnsRectorTest
17+
*/
18+
class RefactorBlueprintGeometryColumnsRector extends AbstractRector
19+
{
20+
public function getRuleDefinition(): RuleDefinition
21+
{
22+
return new RuleDefinition(
23+
'refactors calls with the pre Laravel 11 methods for blueprint geometry columns',
24+
[new CodeSample(<<<'CODE_SAMPLE'
25+
$blueprint->point('coordinates')->spatialIndex();
26+
CODE_SAMPLE,
27+
<<<'CODE_SAMPLE'
28+
$blueprint->geometry('coordinates', 'point')->spatialIndex();
29+
CODE_SAMPLE,
30+
)]
31+
);
32+
}
33+
34+
public function getNodeTypes(): array
35+
{
36+
return [MethodCall::class];
37+
}
38+
39+
/**
40+
* @param Node\Expr\MethodCall $node
41+
*/
42+
public function refactor(Node $node): ?MethodCall
43+
{
44+
if (! $this->isNames($node->name, [
45+
'point',
46+
'linestring',
47+
'polygon',
48+
'geometrycollection',
49+
'multipoint',
50+
'multilinestring',
51+
'multipolygon',
52+
53+
])) {
54+
return null;
55+
}
56+
57+
if (! $node->name instanceof Identifier) {
58+
return null;
59+
}
60+
61+
if (! $this->isObjectType($node->var, new ObjectType('Illuminate\Database\Schema\Blueprint'))) {
62+
return null;
63+
}
64+
65+
$previousName = $node->name;
66+
67+
$node->name = new Identifier('geometry');
68+
$node->args[] = new Arg(new String_($previousName->name));
69+
70+
return $node;
71+
}
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector\Fixture;
4+
5+
/** @var \Illuminate\Database\Schema\Blueprint $table */
6+
$table->point('coordinates')->spatialIndex();
7+
$table->linestring('coordinates')->spatialIndex();
8+
$table->polygon('coordinates')->spatialIndex();
9+
$table->geometrycollection('coordinates')->spatialIndex();
10+
$table->multipoint('coordinates')->spatialIndex();
11+
$table->multilinestring('coordinates')->spatialIndex();
12+
$table->multipolygon('coordinates')->spatialIndex();
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector\Fixture;
19+
20+
/** @var \Illuminate\Database\Schema\Blueprint $table */
21+
$table->geometry('coordinates', 'point')->spatialIndex();
22+
$table->geometry('coordinates', 'linestring')->spatialIndex();
23+
$table->geometry('coordinates', 'polygon')->spatialIndex();
24+
$table->geometry('coordinates', 'geometrycollection')->spatialIndex();
25+
$table->geometry('coordinates', 'multipoint')->spatialIndex();
26+
$table->geometry('coordinates', 'multilinestring')->spatialIndex();
27+
$table->geometry('coordinates', 'multipolygon')->spatialIndex();
28+
29+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector\Fixture;
4+
5+
/** @var \stdClass $table */
6+
$table->point('coordinates')->spatialIndex();
7+
$table->linestring('coordinates')->spatialIndex();
8+
$table->polygon('coordinates')->spatialIndex();
9+
$table->geometrycollection('coordinates')->spatialIndex();
10+
$table->multipoint('coordinates')->spatialIndex();
11+
$table->multilinestring('coordinates')->spatialIndex();
12+
$table->multipolygon('coordinates')->spatialIndex();
13+
14+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector\Fixture;
4+
5+
/** @var \Illuminate\Database\Schema\Blueprint $table */
6+
$table->string('coordinates')->nullable();
7+
8+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RefactorBlueprintGeometryColumnsRectorTest extends AbstractRectorTestCase
12+
{
13+
public static function provideData(): Iterator
14+
{
15+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
#[DataProvider('provideData')]
22+
public function test(string $filePath): void
23+
{
24+
$this->doTestFile($filePath);
25+
}
26+
27+
public function provideConfigFilePath(): string
28+
{
29+
return __DIR__ . '/config/configured_rule.php';
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
10+
11+
$rectorConfig->rule(RefactorBlueprintGeometryColumnsRector::class);
12+
};

0 commit comments

Comments
 (0)