Skip to content

Commit 30e22bb

Browse files
committed
add CurrentRectorProvider
1 parent ddf8de2 commit 30e22bb

9 files changed

Lines changed: 80 additions & 47 deletions

File tree

.github/workflows/annotated_checkstyle.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
tools: cs2pr
2020
- run: composer install --no-progress
2121
- run: |
22-
bin/rector p abz/DeadCode.php --set dead-code -n --autoload-file abz/DeadCode.php --output-format=checkstyle | cs2pr
22+
bin/rector process --config rector-ci.yaml --no-progress-bar --ansi --dry-run --output-format=checkstyle | cs2pr

abz/DeadCode.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

docs/AllRectorsOverview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7015,9 +7015,9 @@ Changes heredoc/nowdoc that contains closing word to safe wrapper name
70157015

70167016
<br>
70177017

7018-
### `SetcookieRector`
7018+
### `SetCookieRector`
70197019

7020-
- class: [`Rector\Php73\Rector\FuncCall\SetcookieRector`](/../master/rules/php-73/src/Rector/FuncCall/SetcookieRector.php)
7020+
- class: [`Rector\Php73\Rector\FuncCall\SetCookieRector`](/../master/rules/php-73/src/Rector/FuncCall/SetCookieRector.php)
70217021
- [test fixtures](/../master/rules/php-73/tests/Rector/FuncCall/SetcookieRector/Fixture)
70227022

70237023
Convert setcookie argument to PHP7.3 option array

packages/changes-reporting/src/Collector/RectorChangeCollector.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use PhpParser\Node;
88
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
99
use Rector\Core\Contract\Rector\RectorInterface;
10-
use Rector\Core\Exception\NotRectorException;
10+
use Rector\Core\Exception\ShouldNotHappenException;
11+
use Rector\Core\Logging\CurrentRectorProvider;
1112
use Rector\NodeTypeResolver\Node\AttributeKey;
1213
use Symplify\SmartFileSystem\SmartFileInfo;
1314

@@ -18,14 +19,20 @@ final class RectorChangeCollector
1819
*/
1920
private $rectorWithFileAndLineChanges = [];
2021

21-
public function addRectorClassWithLine(string $rectorClass, SmartFileInfo $smartFileInfo, int $line): void
22+
/**
23+
* @var CurrentRectorProvider
24+
*/
25+
private $currentRectorProvider;
26+
27+
public function __construct(CurrentRectorProvider $currentRectorProvider)
2228
{
23-
if (! is_a($rectorClass, RectorInterface::class, true)) {
24-
throw new NotRectorException($rectorClass);
25-
}
29+
$this->currentRectorProvider = $currentRectorProvider;
30+
}
2631

32+
public function addRectorClassWithLine(RectorInterface $rector, SmartFileInfo $smartFileInfo, int $line): void
33+
{
2734
$this->rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange(
28-
$rectorClass,
35+
$rector,
2936
$smartFileInfo->getRealPath(),
3037
$line
3138
);
@@ -53,6 +60,11 @@ public function notifyNodeFileInfo(Node $node): void
5360
return;
5461
}
5562

56-
$this->addRectorClassWithLine(static::class, $fileInfo, $node->getLine());
63+
$currentRector = $this->currentRectorProvider->getCurrentRector();
64+
if ($currentRector === null) {
65+
throw new ShouldNotHappenException();
66+
}
67+
68+
$this->addRectorClassWithLine($currentRector, $fileInfo, $node->getLine());
5769
}
5870
}

packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ private function writeFileErrors(FileDiff $fileDiff): void
6262
$this->symfonyStyle->writeln(sprintf('<file name="%s">', $this->escape($fileDiff->getRelativeFilePath())));
6363

6464
foreach ($fileDiff->getRectorChanges() as $rectorChange) {
65+
$message = $rectorChange->getRectorDefinitionsDescription() . ' (Reported by: ' . $rectorChange->getRectorClass() . ')';
66+
$message = $this->escape($message);
67+
6568
$error = sprintf(
6669
' <error line="%d" column="1" severity="error" message="%s" />',
6770
$this->escape((string) $rectorChange->getLine()),
68-
$this->escape((string) $rectorChange->getRectorClass())
71+
$message
6972
);
7073
$this->symfonyStyle->writeln($error);
7174
}

packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
namespace Rector\ChangesReporting\ValueObject;
66

77
use Rector\Core\Contract\Rector\RectorInterface;
8-
use Rector\Core\Exception\NotRectorException;
98

109
final class RectorWithFileAndLineChange
1110
{
1211
/**
13-
* @var string
12+
* @var RectorInterface
1413
*/
15-
private $rectorClass;
14+
private $rector;
1615

1716
/**
1817
* @var int
@@ -24,20 +23,26 @@ final class RectorWithFileAndLineChange
2423
*/
2524
private $realPath;
2625

27-
public function __construct(string $rectorClass, string $realPath, int $line)
26+
public function __construct(RectorInterface $rector, string $realPath, int $line)
2827
{
29-
if (! is_a($rectorClass, RectorInterface::class, true)) {
30-
throw new NotRectorException($rectorClass);
31-
}
32-
33-
$this->rectorClass = $rectorClass;
28+
$this->rector = $rector;
3429
$this->line = $line;
3530
$this->realPath = $realPath;
3631
}
3732

33+
public function getRector(): RectorInterface
34+
{
35+
return $this->rector;
36+
}
37+
38+
public function getRectorDefinitionsDescription(): string
39+
{
40+
return $this->rector->getDefinition()->getDescription();
41+
}
42+
3843
public function getRectorClass(): string
3944
{
40-
return $this->rectorClass;
45+
return get_class($this->rector);
4146
}
4247

4348
public function getLine(): int
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Core\Logging;
6+
7+
use Rector\Core\Contract\Rector\RectorInterface;
8+
9+
final class CurrentRectorProvider
10+
{
11+
/**
12+
* @var RectorInterface|null
13+
*/
14+
private $currentRector;
15+
16+
public function changeCurrentRector(RectorInterface $rector): void
17+
{
18+
$this->currentRector = $rector;
19+
}
20+
21+
public function getCurrentRector(): ?RectorInterface
22+
{
23+
return $this->currentRector;
24+
}
25+
}

src/Rector/AbstractRector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Rector\Core\Configuration\Option;
2121
use Rector\Core\Contract\Rector\PhpRectorInterface;
2222
use Rector\Core\Exclusion\ExclusionManager;
23+
use Rector\Core\Logging\CurrentRectorProvider;
2324
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
2425
use Rector\NodeTypeResolver\Node\AttributeKey;
2526
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
@@ -85,6 +86,11 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
8586
*/
8687
protected $staticTypeMapper;
8788

89+
/**
90+
* @var CurrentRectorProvider
91+
*/
92+
private $currentRectorProvider;
93+
8894
/**
8995
* @var string[]
9096
*/
@@ -122,7 +128,8 @@ public function autowireAbstractRectorDependencies(
122128
PhpDocInfoPrinter $phpDocInfoPrinter,
123129
DocBlockManipulator $docBlockManipulator,
124130
StaticTypeMapper $staticTypeMapper,
125-
ParameterProvider $parameterProvider
131+
ParameterProvider $parameterProvider,
132+
CurrentRectorProvider $currentRectorProvider
126133
): void {
127134
$this->symfonyStyle = $symfonyStyle;
128135
$this->phpVersionProvider = $phpVersionProvider;
@@ -134,6 +141,7 @@ public function autowireAbstractRectorDependencies(
134141
$this->docBlockManipulator = $docBlockManipulator;
135142
$this->staticTypeMapper = $staticTypeMapper;
136143
$this->parameterProvider = $parameterProvider;
144+
$this->currentRectorProvider = $currentRectorProvider;
137145
}
138146

139147
/**
@@ -145,6 +153,8 @@ final public function enterNode(Node $node)
145153
return null;
146154
}
147155

156+
$this->currentRectorProvider->changeCurrentRector($this);
157+
148158
// show current Rector class on --debug
149159
if ($this->symfonyStyle->isDebug()) {
150160
// indented on purpose to improve log nesting under [refactoring]

tests/Standalone/RectorStandaloneRunnerTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,15 @@
55
namespace Rector\Core\Tests\Standalone;
66

77
use PHPUnit\Framework\TestCase;
8-
use Rector\Core\Standalone\RectorStandaloneRunner;
98
use Rector\Core\Standalone\RectorStandaloneRunnerStaticFactory;
109

1110
final class RectorStandaloneRunnerTest extends TestCase
1211
{
13-
/**
14-
* @var RectorStandaloneRunner
15-
*/
16-
private $rectorStandaloneRunner;
17-
18-
protected function setUp(): void
19-
{
20-
$this->rectorStandaloneRunner = RectorStandaloneRunnerStaticFactory::create();
21-
}
22-
2312
public function test(): void
2413
{
25-
$errorAndDiffCollector = $this->rectorStandaloneRunner->processSourceWithSet(
14+
$rectorStandaloneRunner = RectorStandaloneRunnerStaticFactory::create();
15+
16+
$errorAndDiffCollector = $rectorStandaloneRunner->processSourceWithSet(
2617
[__DIR__ . '/Source/LowQualityFile.php'],
2718
'code-quality',
2819
true,

0 commit comments

Comments
 (0)