From fb46b9d2b90241d61128b64401a6cbd3ad5932eb Mon Sep 17 00:00:00 2001 From: Philipp Kitzberger Date: Thu, 15 Jun 2023 10:21:29 +0200 Subject: [PATCH] Tweak replace v:varible.set * Only migrate the simple cases by the 'strict' pattern * Print warning about left over cases --- .../vhs/ReplaceVariableSetFluidRector.php | 30 +++++++++++++++++-- .../Fixture/fixture.html.inc | 6 ---- .../Fixture/fixture.html.inc | 14 +++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector.php b/src/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector.php index 9be4ee009..cc9248249 100644 --- a/src/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector.php +++ b/src/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector.php @@ -5,6 +5,7 @@ namespace Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs; use Nette\Utils\Strings; +use Rector\Core\Console\Output\RectorOutputStyle; use Rector\Core\ValueObject\Application\File; use Ssch\TYPO3Rector\Contract\FileProcessor\Fluid\Rector\FluidRectorInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -15,18 +16,41 @@ final class ReplaceVariableSetFluidRector implements FluidRectorInterface /** * @var string */ - private const PATTERN = '#(v|vhs):variable.set#imsU'; + private const PATTERN_STRICT = '#(v|vhs):(variable.set)([\s(]name[:=]["\'])([a-z]+)(["\'])#imsU'; /** * @var string */ - private const REPLACEMENT = 'f:variable'; + private const PATTERN_DOT = '#(v|vhs):(variable.set)([\s(]name[:=]["\'])([a-z\.]+)(["\'])#imsU'; + + /** + * @var string + */ + private const PATTERN_LEFTOVERS = '#(v|vhs):(variable.set)#imsU'; + + /** + * @readonly + */ + private RectorOutputStyle $rectorOutputStyle; + + public function __construct(RectorOutputStyle $rectorOutputStyle) + { + $this->rectorOutputStyle = $rectorOutputStyle; + } public function transform(File $file): void { $content = $file->getFileContent(); - $content = Strings::replace($content, self::PATTERN, self::REPLACEMENT); + $content = Strings::replace($content, self::PATTERN_STRICT, 'f:variable$3$4$5'); + + if (Strings::matchAll($content, self::PATTERN_DOT)) { + $this->rectorOutputStyle->warning('There\'s occurrences of v:variable.set that contain a dot in its name attribute and thus cannot be migrated to f:variable!'); + } + + if (Strings::matchAll($content, self::PATTERN_LEFTOVERS)) { + $this->rectorOutputStyle->warning('There\'s occurrences of v:variable.set that couldn\'t be migrated automatically. Migrate them manually!'); + } $file->changeFileContent($content); } diff --git a/tests/FileProcessor/Fluid/Rector/vhs/ReplaceUriImageFluidRector/Fixture/fixture.html.inc b/tests/FileProcessor/Fluid/Rector/vhs/ReplaceUriImageFluidRector/Fixture/fixture.html.inc index 3897f6cf5..f4ff96fb3 100644 --- a/tests/FileProcessor/Fluid/Rector/vhs/ReplaceUriImageFluidRector/Fixture/fixture.html.inc +++ b/tests/FileProcessor/Fluid/Rector/vhs/ReplaceUriImageFluidRector/Fixture/fixture.html.inc @@ -1,9 +1,3 @@ {v:uri.image(src:image.uid, treatIdAsReference: 1)} -{v:uri.image(src:image.uid, treatIdAsReference: 1, relative: 1)} -{v:uri.image(src:image.uid, treatIdAsReference: 1, relative: 0)} -{v:uri.image(src:image.uid, treatIdAsReference: 1, maxW: 250, maxH: 250)} ----- {f:uri.image(src:image.uid, treatIdAsReference: 1)} -{f:uri.image(src:image.uid, treatIdAsReference: 1)} -{f:uri.image(src:image.uid, treatIdAsReference: 1, absolute: 1)} -{f:uri.image(src:image.uid, treatIdAsReference: 1, maxWidth: 250, maxHeight: 250)} diff --git a/tests/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector/Fixture/fixture.html.inc b/tests/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector/Fixture/fixture.html.inc index 5278475e4..1166ca331 100644 --- a/tests/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector/Fixture/fixture.html.inc +++ b/tests/FileProcessor/Fluid/Rector/vhs/ReplaceVariableSetFluidRector/Fixture/fixture.html.inc @@ -1,5 +1,15 @@ - +Simple (and easily migratable) cases: {myvariable -> v:variable.set(name:'othervariable')} + + +More complex cases (and unmigratable) cases: + + ----- - +Simple (and easily migratable) cases: {myvariable -> f:variable(name:'othervariable')} + + +More complex cases (and unmigratable) cases: + +