Skip to content

Commit

Permalink
Tweak replace v:varible.set
Browse files Browse the repository at this point in the history
* Only migrate the simple cases by the 'strict' pattern
* Print warning about left over cases
  • Loading branch information
kitzberger committed Jul 15, 2023
1 parent df2eba0 commit fb46b9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<v:variable.set name="myvariable" value="a string value" />
Simple (and easily migratable) cases:
{myvariable -> v:variable.set(name:'othervariable')}
<v:variable.set name="myvariable" value="a string value" />

More complex cases (and unmigratable) cases:
<v:variable.set name="myarray.property" value="a string value" />
<v:variable.set value="a string value" name="myvariable" />
-----
<f:variable name="myvariable" value="a string value" />
Simple (and easily migratable) cases:
{myvariable -> f:variable(name:'othervariable')}
<f:variable name="myvariable" value="a string value" />

More complex cases (and unmigratable) cases:
<v:variable.set name="myarray.property" value="a string value" />
<v:variable.set value="a string value" name="myvariable" />

0 comments on commit fb46b9d

Please sign in to comment.