Skip to content

Commit db8a1b2

Browse files
Print warning instead of throwing an error
1 parent c6fc91c commit db8a1b2

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

rules/Arguments/ArgumentDefaultValueReplacer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ private function processArgs(
108108
}
109109

110110
$argValue = $this->valueResolver->getValue($particularArg->value);
111-
112111
if (is_scalar(
113112
$replaceArgumentDefaultValue->getValueBefore()
114113
) && $argValue === $replaceArgumentDefaultValue->getValueBefore()) {
115114
$expr->args[$position] = $this->normalizeValueToArgument($replaceArgumentDefaultValue->getValueAfter());
116115
return $expr;
117-
} elseif (is_array($replaceArgumentDefaultValue->getValueBefore())) {
118-
$newArgs = $this->processArrayReplacement($expr->getArgs(), $replaceArgumentDefaultValue);
116+
}
119117

118+
if (is_array($replaceArgumentDefaultValue->getValueBefore())) {
119+
$newArgs = $this->processArrayReplacement($expr->getArgs(), $replaceArgumentDefaultValue);
120120
if (is_array($newArgs)) {
121121
$expr->args = $newArgs;
122122
return $expr;

src/Exception/Configuration/RectorRuleShouldNotBeAppliedException.php

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

src/Testing/PHPUnit/AbstractRectorTestCase.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Rector\Contract\DependencyInjection\ResetableInterface;
1919
use Rector\Contract\Rector\RectorInterface;
2020
use Rector\DependencyInjection\Laravel\ContainerMemento;
21-
use Rector\Exception\Configuration\RectorRuleShouldNotBeAppliedException;
2221
use Rector\Exception\ShouldNotHappenException;
2322
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
2423
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
@@ -166,6 +165,23 @@ protected function doTestFile(string $fixtureFilePath): void
166165
);
167166
}
168167

168+
protected function doTestFileExpectingWarningAboutRuleApplied(
169+
string $fixtureFilePath,
170+
string $expectedRuleApplied
171+
): void {
172+
ob_start();
173+
$this->doTestFile($fixtureFilePath);
174+
$content = ob_get_clean();
175+
$fixtureName = basename($fixtureFilePath);
176+
$testClass = static::class;
177+
$this->assertSame(
178+
PHP_EOL . "WARNING: On fixture file \"" . $fixtureName . '" for test "' . $testClass . "\"" . PHP_EOL .
179+
"File not changed but some Rector rules applied:" . PHP_EOL .
180+
' * ' . $expectedRuleApplied . PHP_EOL,
181+
$content
182+
);
183+
}
184+
169185
private function forgetRectorsRules(): void
170186
{
171187
$rectorConfig = self::getContainer();
@@ -208,9 +224,8 @@ private function doTestFileMatchesExpectedContent(
208224
$changedContents = $rectorTestResult->getChangedContents();
209225

210226
$fixtureFilename = basename($fixtureFilePath);
211-
$fixtureNameMessage = sprintf('Failed on fixture file "%s"', $fixtureFilename);
227+
$failureMessage = sprintf('Failed on fixture file "%s"', $fixtureFilename);
212228

213-
$failureMessage = $fixtureNameMessage;
214229
$numAppliedRectorClasses = count($rectorTestResult->getAppliedRectorClasses());
215230
// give more context about used rules in case of set testing
216231
$appliedRulesList = '';
@@ -234,9 +249,13 @@ private function doTestFileMatchesExpectedContent(
234249
}
235250

236251
if ($inputFileContents === $expectedFileContents && $numAppliedRectorClasses > 0) {
237-
$failureMessage = $fixtureNameMessage . PHP_EOL . PHP_EOL
252+
$failureMessage = PHP_EOL . sprintf(
253+
'WARNING: On fixture file "%s" for test "%s"',
254+
$fixtureFilename,
255+
static::class
256+
) . PHP_EOL
238257
. 'File not changed but some Rector rules applied:' . PHP_EOL . $appliedRulesList;
239-
throw new RectorRuleShouldNotBeAppliedException($failureMessage);
258+
echo $failureMessage;
240259
}
241260
}
242261

tests/Issues/ScopeNotAvailable/ForeachValueScopeTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
9-
use Rector\Exception\Configuration\RectorRuleShouldNotBeAppliedException;
109
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
use Rector\Tests\Issues\ScopeNotAvailable\Variable\ArrayItemForeachValueRector;
1111

1212
final class ForeachValueScopeTest extends AbstractRectorTestCase
1313
{
1414
#[DataProvider('provideData')]
1515
public function test(string $filePath): void
1616
{
17-
$this->expectException(RectorRuleShouldNotBeAppliedException::class);
18-
$this->doTestFile($filePath);
17+
$this->doTestFileExpectingWarningAboutRuleApplied($filePath, ArrayItemForeachValueRector::class);
1918
}
2019

2120
public static function provideData(): Iterator

tests/Testing/RectorRuleShouldNotBeApplied/RectorRuleShouldNotBeAppliedTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
9-
use Rector\Exception\Configuration\RectorRuleShouldNotBeAppliedException;
109
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
use Rector\Tests\Testing\RectorRuleShouldNotBeApplied\Source\NoChangeRector;
1111

1212
final class RectorRuleShouldNotBeAppliedTest extends AbstractRectorTestCase
1313
{
1414
#[DataProvider('provideData')]
1515
public function test(string $filePath): void
1616
{
17-
$this->expectException(RectorRuleShouldNotBeAppliedException::class);
18-
$this->expectExceptionMessage(
19-
'Failed on fixture file "no_change.php.inc"' . PHP_EOL . PHP_EOL
20-
. 'File not changed but some Rector rules applied:' . PHP_EOL
21-
. ' * Rector\Tests\Testing\RectorRuleShouldNotBeApplied\Source\NoChangeRector'
22-
);
23-
24-
$this->doTestFile($filePath);
17+
$this->doTestFileExpectingWarningAboutRuleApplied($filePath, NoChangeRector::class);
2518
}
2619

2720
public static function provideData(): Iterator

0 commit comments

Comments
 (0)