Skip to content

Commit f8369ec

Browse files
committed
FixableNodeRuleError interface with support in RuleErrorBuilder
1 parent 27dd6c0 commit f8369ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3818
-3
lines changed

src/Rules/FixableNodeRuleError.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PhpParser\Node;
6+
7+
interface FixableNodeRuleError extends RuleError
8+
{
9+
10+
/** @return callable(Node): Node */
11+
public function getNewNodeCallable(): callable;
12+
13+
}

src/Rules/RuleErrorBuilder.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules;
44

5+
use PhpParser\Node;
56
use PHPStan\Analyser\Error;
67
use PHPStan\ShouldNotHappenException;
78
use function array_map;
@@ -25,6 +26,7 @@ final class RuleErrorBuilder
2526
private const TYPE_IDENTIFIER = 16;
2627
private const TYPE_METADATA = 32;
2728
private const TYPE_NON_IGNORABLE = 64;
29+
private const TYPE_FIXABLE_NODE = 128;
2830

2931
private int $type;
3032

@@ -50,9 +52,9 @@ public static function getRuleErrorTypes(): array
5052
RuleError::class,
5153
[
5254
[
53-
'message',
54-
'string',
55-
'string',
55+
'message', // property name
56+
'string', // native type
57+
'string', // PHPDoc type
5658
],
5759
],
5860
],
@@ -115,6 +117,16 @@ public static function getRuleErrorTypes(): array
115117
NonIgnorableRuleError::class,
116118
[],
117119
],
120+
self::TYPE_FIXABLE_NODE => [
121+
FixableNodeRuleError::class,
122+
[
123+
[
124+
'newNodeCallable',
125+
null,
126+
'callable(\PhpParser\Node): \PhpParser\Node',
127+
],
128+
],
129+
],
118130
];
119131
}
120132

@@ -254,6 +266,20 @@ public function nonIgnorable(): self
254266
return $this;
255267
}
256268

269+
/**
270+
* @internal Experimental
271+
* @param callable(Node): Node $cb
272+
* @phpstan-this-out self<T&FixableNodeRuleError>
273+
* @return self<T&FixableNodeRuleError>
274+
*/
275+
public function fixNode(callable $cb): self
276+
{
277+
$this->properties['newNodeCallable'] = $cb;
278+
$this->type |= self::TYPE_FIXABLE_NODE;
279+
280+
return $this;
281+
}
282+
257283
/**
258284
* @return T
259285
*/

src/Rules/RuleErrors/RuleError129.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FixableNodeRuleError;
7+
use PHPStan\Rules\RuleError;
8+
9+
/**
10+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
11+
*/
12+
final class RuleError129 implements RuleError, FixableNodeRuleError
13+
{
14+
15+
public string $message;
16+
17+
/** @var callable(Node): Node */
18+
public $newNodeCallable;
19+
20+
public function getMessage(): string
21+
{
22+
return $this->message;
23+
}
24+
25+
/**
26+
* @return callable(Node): Node
27+
*/
28+
public function getNewNodeCallable(): callable
29+
{
30+
return $this->newNodeCallable;
31+
}
32+
33+
}

src/Rules/RuleErrors/RuleError131.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FixableNodeRuleError;
7+
use PHPStan\Rules\LineRuleError;
8+
use PHPStan\Rules\RuleError;
9+
10+
/**
11+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
12+
*/
13+
final class RuleError131 implements RuleError, LineRuleError, FixableNodeRuleError
14+
{
15+
16+
public string $message;
17+
18+
public int $line;
19+
20+
/** @var callable(Node): Node */
21+
public $newNodeCallable;
22+
23+
public function getMessage(): string
24+
{
25+
return $this->message;
26+
}
27+
28+
public function getLine(): int
29+
{
30+
return $this->line;
31+
}
32+
33+
/**
34+
* @return callable(Node): Node
35+
*/
36+
public function getNewNodeCallable(): callable
37+
{
38+
return $this->newNodeCallable;
39+
}
40+
41+
}

src/Rules/RuleErrors/RuleError133.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FileRuleError;
7+
use PHPStan\Rules\FixableNodeRuleError;
8+
use PHPStan\Rules\RuleError;
9+
10+
/**
11+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
12+
*/
13+
final class RuleError133 implements RuleError, FileRuleError, FixableNodeRuleError
14+
{
15+
16+
public string $message;
17+
18+
public string $file;
19+
20+
public string $fileDescription;
21+
22+
/** @var callable(Node): Node */
23+
public $newNodeCallable;
24+
25+
public function getMessage(): string
26+
{
27+
return $this->message;
28+
}
29+
30+
public function getFile(): string
31+
{
32+
return $this->file;
33+
}
34+
35+
public function getFileDescription(): string
36+
{
37+
return $this->fileDescription;
38+
}
39+
40+
/**
41+
* @return callable(Node): Node
42+
*/
43+
public function getNewNodeCallable(): callable
44+
{
45+
return $this->newNodeCallable;
46+
}
47+
48+
}

src/Rules/RuleErrors/RuleError135.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FileRuleError;
7+
use PHPStan\Rules\FixableNodeRuleError;
8+
use PHPStan\Rules\LineRuleError;
9+
use PHPStan\Rules\RuleError;
10+
11+
/**
12+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
13+
*/
14+
final class RuleError135 implements RuleError, LineRuleError, FileRuleError, FixableNodeRuleError
15+
{
16+
17+
public string $message;
18+
19+
public int $line;
20+
21+
public string $file;
22+
23+
public string $fileDescription;
24+
25+
/** @var callable(Node): Node */
26+
public $newNodeCallable;
27+
28+
public function getMessage(): string
29+
{
30+
return $this->message;
31+
}
32+
33+
public function getLine(): int
34+
{
35+
return $this->line;
36+
}
37+
38+
public function getFile(): string
39+
{
40+
return $this->file;
41+
}
42+
43+
public function getFileDescription(): string
44+
{
45+
return $this->fileDescription;
46+
}
47+
48+
/**
49+
* @return callable(Node): Node
50+
*/
51+
public function getNewNodeCallable(): callable
52+
{
53+
return $this->newNodeCallable;
54+
}
55+
56+
}

src/Rules/RuleErrors/RuleError137.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FixableNodeRuleError;
7+
use PHPStan\Rules\RuleError;
8+
use PHPStan\Rules\TipRuleError;
9+
10+
/**
11+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
12+
*/
13+
final class RuleError137 implements RuleError, TipRuleError, FixableNodeRuleError
14+
{
15+
16+
public string $message;
17+
18+
public string $tip;
19+
20+
/** @var callable(Node): Node */
21+
public $newNodeCallable;
22+
23+
public function getMessage(): string
24+
{
25+
return $this->message;
26+
}
27+
28+
public function getTip(): string
29+
{
30+
return $this->tip;
31+
}
32+
33+
/**
34+
* @return callable(Node): Node
35+
*/
36+
public function getNewNodeCallable(): callable
37+
{
38+
return $this->newNodeCallable;
39+
}
40+
41+
}

src/Rules/RuleErrors/RuleError139.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RuleErrors;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Rules\FixableNodeRuleError;
7+
use PHPStan\Rules\LineRuleError;
8+
use PHPStan\Rules\RuleError;
9+
use PHPStan\Rules\TipRuleError;
10+
11+
/**
12+
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
13+
*/
14+
final class RuleError139 implements RuleError, LineRuleError, TipRuleError, FixableNodeRuleError
15+
{
16+
17+
public string $message;
18+
19+
public int $line;
20+
21+
public string $tip;
22+
23+
/** @var callable(Node): Node */
24+
public $newNodeCallable;
25+
26+
public function getMessage(): string
27+
{
28+
return $this->message;
29+
}
30+
31+
public function getLine(): int
32+
{
33+
return $this->line;
34+
}
35+
36+
public function getTip(): string
37+
{
38+
return $this->tip;
39+
}
40+
41+
/**
42+
* @return callable(Node): Node
43+
*/
44+
public function getNewNodeCallable(): callable
45+
{
46+
return $this->newNodeCallable;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)