mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-02 15:43:31 +00:00
[CodingStyle] Escape percent when encapsed strings to sprintf (#265)
This commit is contained in:
parent
a41384bc00
commit
7c30a9713b
|
@ -82,7 +82,7 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
|
|||
return StaticFixtureFinder::yieldDirectoryExclusively($directory, $suffix);
|
||||
}
|
||||
|
||||
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo): void
|
||||
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo, bool $allowMatches = true): void
|
||||
{
|
||||
$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
|
||||
$fixtureFileInfo
|
||||
|
@ -92,7 +92,7 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
|
|||
$this->originalTempFileInfo = $inputFileInfo;
|
||||
|
||||
$expectedFileInfo = $inputFileInfoAndExpectedFileInfo->getExpectedFileInfo();
|
||||
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo);
|
||||
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo, $allowMatches);
|
||||
}
|
||||
|
||||
protected function getFixtureTempDirectory(): string
|
||||
|
@ -103,7 +103,8 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
|
|||
private function doTestFileMatchesExpectedContent(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
SmartFileInfo $expectedFileInfo,
|
||||
SmartFileInfo $fixtureFileInfo
|
||||
SmartFileInfo $fixtureFileInfo,
|
||||
bool $allowMatches = true
|
||||
): void {
|
||||
$this->parameterProvider->changeParameter(Option::SOURCE, [$originalFileInfo->getRealPath()]);
|
||||
|
||||
|
@ -118,7 +119,10 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
|
|||
|
||||
try {
|
||||
$this->assertStringEqualsFile($expectedFileInfo->getRealPath(), $changedContent, $relativeFilePathFromCwd);
|
||||
} catch (ExpectationFailedException) {
|
||||
} catch (ExpectationFailedException $expectationFailedException) {
|
||||
if ($allowMatches === false) {
|
||||
throw $expectationFailedException;
|
||||
}
|
||||
StaticFixtureUpdater::updateFixtureContent($originalFileInfo, $changedContent, $fixtureFileInfo);
|
||||
$contents = $expectedFileInfo->getContents();
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ final class EncapsedStringsToSprintfRectorTest extends AbstractRectorTestCase
|
|||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
$this->doTestFileInfo($fileInfo, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,7 +19,7 @@ final class EncapsedStringsToSprintfShouldEscapePercent
|
|||
{
|
||||
public function run(string $value)
|
||||
{
|
||||
return sprintf('%s%', $value);
|
||||
return sprintf('%s%%', $value);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\CodingStyle\Rector\Encapsed;
|
||||
|
||||
use const PHP_EOL;
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
|
@ -99,7 +100,7 @@ CODE_SAMPLE
|
|||
return;
|
||||
}
|
||||
|
||||
$this->sprintfFormat .= $stringValue;
|
||||
$this->sprintfFormat .= Strings::replace($stringValue, '#%#', '%%');
|
||||
}
|
||||
|
||||
private function collectExpr(Expr $expr): void
|
||||
|
|
Loading…
Reference in New Issue
Block a user