[CodingStyle] Escape percent when encapsed strings to sprintf (#265)

This commit is contained in:
Zing 2021-06-22 21:01:54 +08:00 committed by GitHub
parent a41384bc00
commit 7c30a9713b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 7 deletions

View File

@ -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();

View File

@ -15,7 +15,7 @@ final class EncapsedStringsToSprintfRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
$this->doTestFileInfo($fileInfo, false);
}
/**

View File

@ -19,7 +19,7 @@ final class EncapsedStringsToSprintfShouldEscapePercent
{
public function run(string $value)
{
return sprintf('%s%', $value);
return sprintf('%s%%', $value);
}
}
?>

View File

@ -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