Skip to content

Commit ab3d4c0

Browse files
committed
Refactor of the type system
1 parent b87ff93 commit ab3d4c0

File tree

2 files changed

+143
-46
lines changed

2 files changed

+143
-46
lines changed

config/sets/type-declaration/eloquent.php

Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
return static function (RectorConfig $rectorConfig): void {
1010
$rectorConfig->import(__DIR__ . '/../../config.php');
1111

12+
$generator = new \RectorLaravel\Util\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRectorConfigGenerator();
13+
1214
$builderClass = new ObjectType(
1315
'Illuminate\Contracts\Database\Query\Builder'
1416
);
1517

18+
/** @var class-string[] $classesToApplyTo */
1619
$classesToApplyTo = [
1720
'Illuminate\Database\Eloquent\Model',
1821
'Illuminate\Contracts\Database\Query\Builder',
@@ -21,59 +24,112 @@
2124
'Illuminate\Database\Query\Builder',
2225
];
2326

24-
$basicPositionOne = [
25-
'where', 'orWhere', 'whereNot', 'whereExists',
26-
];
27-
$basicPositionTwo = [
28-
'where', 'whereHas', 'orWhereHas', 'whereDoesntHave', 'orWhereDoesntHave', 'withWhereHas', 'when',
27+
$basicRuleConfiguration = [
28+
...$generator->generate(
29+
[0, 1, 2, 'builder'],
30+
$classesToApplyTo,
31+
0,
32+
'where',
33+
$builderClass,
34+
),
35+
...$generator->generate(
36+
[0, 'builder'],
37+
$classesToApplyTo,
38+
0,
39+
'orWhere',
40+
$builderClass,
41+
),
42+
...$generator->generate(
43+
[0, 'builder'],
44+
$classesToApplyTo,
45+
0,
46+
'whereNot',
47+
$builderClass,
48+
),
49+
...$generator->generate(
50+
[0, 'builder'],
51+
$classesToApplyTo,
52+
0,
53+
'whereExists',
54+
$builderClass,
55+
),
56+
...$generator->generate(
57+
[1, 'builder'],
58+
$classesToApplyTo,
59+
0,
60+
'whereHas',
61+
$builderClass,
62+
),
63+
...$generator->generate(
64+
[1, 'builder'],
65+
$classesToApplyTo,
66+
0,
67+
'orWhereHas',
68+
$builderClass,
69+
),
70+
...$generator->generate(
71+
[1, 'builder'],
72+
$classesToApplyTo,
73+
0,
74+
'whereDoesntHave',
75+
$builderClass,
76+
),
77+
...$generator->generate(
78+
[1, 'builder'],
79+
$classesToApplyTo,
80+
0,
81+
'orWhereDoesntHave',
82+
$builderClass,
83+
),
84+
...$generator->generate(
85+
[1, 'builder'],
86+
$classesToApplyTo,
87+
0,
88+
'withWhereHas',
89+
$builderClass,
90+
),
91+
...$generator->generate(
92+
[1, 2, 'builder'],
93+
$classesToApplyTo,
94+
0,
95+
'when',
96+
$builderClass,
97+
),
98+
...$generator->generate(
99+
[2, 'builder'],
100+
$classesToApplyTo,
101+
0,
102+
'whereHasMorph',
103+
$builderClass,
104+
),
105+
...$generator->generate(
106+
[2, 'builder'],
107+
$classesToApplyTo,
108+
0,
109+
'orWhereHasMorph',
110+
$builderClass,
111+
),
112+
...$generator->generate(
113+
[2, 'builder'],
114+
$classesToApplyTo,
115+
0,
116+
'whereDoesntHaveMorph',
117+
$builderClass,
118+
),
119+
...$generator->generate(
120+
[2, 'builder'],
121+
$classesToApplyTo,
122+
0,
123+
'orWhereDoesntHaveMorph',
124+
$builderClass,
125+
),
29126
];
30-
$basicPositionThree = [
31-
'where', 'whereHasMorph', 'orWhereHasMorph', 'whereDoesntHaveMorph', 'orWhereDoesntHaveMorph', 'when',
32-
];
33-
34-
$basicRuleConfiguration = [];
35-
$arrayRuleConfiguration = [];
36-
37-
foreach ($classesToApplyTo as $targetClass) {
38-
foreach ($basicPositionOne as $method) {
39-
$basicRuleConfiguration[] = new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
40-
$targetClass,
41-
$method,
42-
0,
43-
0,
44-
$builderClass,
45-
);
46-
}
47-
foreach ($basicPositionTwo as $method) {
48-
$basicRuleConfiguration[] = new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
49-
$targetClass,
50-
$method,
51-
1,
52-
0,
53-
$builderClass,
54-
);
55-
}
56-
foreach ($basicPositionThree as $method) {
57-
$basicRuleConfiguration[] = new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
58-
$targetClass,
59-
$method,
60-
2,
61-
0,
62-
$builderClass,
63-
);
64-
}
65-
}
66127

67128
$rectorConfig->ruleWithConfiguration(
68129
AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector::class,
69130
$basicRuleConfiguration
70131
);
71132

72-
$rectorConfig->ruleWithConfiguration(
73-
AddParamTypeForFunctionLikeWithinCallLikeArgArrayValuesDeclarationRector::class,
74-
$arrayRuleConfiguration,
75-
);
76-
77133
$rectorConfig->ruleWithConfiguration(
78134
AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector::class,
79135
[
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace RectorLaravel\Util;
4+
5+
use PHPStan\Type\ObjectType;
6+
use Rector\TypeDeclaration\ValueObject\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration;
7+
8+
class AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRectorConfigGenerator
9+
{
10+
/**
11+
* @param array<int, string|int<0, max>> $callPositionsOrNames
12+
* @param class-string[] $targetClasses
13+
* @param int<0, max> $functionPosition
14+
* @param string $methodName
15+
* @param ObjectType $type
16+
* @return AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration[]
17+
*/
18+
public function generate(
19+
array $callPositionsOrNames,
20+
array $targetClasses,
21+
int $functionPosition,
22+
string $methodName,
23+
ObjectType $type
24+
): array {
25+
$configurations = [];
26+
27+
foreach ($callPositionsOrNames as $callPositionsOrName) {
28+
foreach ($targetClasses as $targetClass) {
29+
$configurations[] = new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
30+
$targetClass,
31+
$methodName,
32+
$callPositionsOrName,
33+
$functionPosition,
34+
$type
35+
);
36+
}
37+
}
38+
39+
return $configurations;
40+
}
41+
}

0 commit comments

Comments
 (0)