Skip to content

Commit

Permalink
TASK: Return nodes TemplateGetFileNameToFilePathSanitizerRector (#3999)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored Jan 9, 2024
1 parent 8e35fd5 commit 72ebfe5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 128 deletions.
118 changes: 5 additions & 113 deletions src/Rector/v9/v4/TemplateGetFileNameToFilePathSanitizerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,9 @@
namespace Ssch\TYPO3Rector\Rector\v9\v4;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_ as ScalarString_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToAddCollector;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,20 +18,14 @@
*/
final class TemplateGetFileNameToFilePathSanitizerRector extends AbstractRector
{
/**
* @readonly
*/
public NodesToAddCollector $nodesToAddCollector;

/**
* @readonly
*/
private Typo3NodeResolver $typo3NodeResolver;

public function __construct(Typo3NodeResolver $typo3NodeResolver, NodesToAddCollector $nodesToAddCollector)
public function __construct(Typo3NodeResolver $typo3NodeResolver)
{
$this->typo3NodeResolver = $typo3NodeResolver;
$this->nodesToAddCollector = $nodesToAddCollector;
}

/**
Expand Down Expand Up @@ -75,30 +57,9 @@ public function refactor(Node $node): ?Node
return null;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

if (! $parentNode instanceof Assign) {
return null;
}

$filePath = new String_($node->args[0]->value);

// First of all remove the node
$this->removeNode($parentNode);

$assignmentNode = $this->createSanitizeMethod($parentNode, $filePath);
$assignmentNodeNull = $this->createNullAssignment($parentNode);

$catches = [
$this->createCatchBlockToIgnore($assignmentNodeNull),
$this->createCatchBlockToLog([$assignmentNodeNull, $this->createIfLog()]),
];

$tryCatch = new TryCatch([$assignmentNode], $catches);

$this->nodesToAddCollector->addNodeBeforeNode($tryCatch, $node);

return $node;
return $this->createSanitizeMethod($filePath);
}

/**
Expand All @@ -113,91 +74,22 @@ public function getRuleDefinition(): RuleDefinition
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
use TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException;
use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\Exception\InvalidFileException;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
try {
$fileName = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string) 'foo.text');
} catch (InvalidFileNameException $e) {
$fileName = null;
} catch (InvalidPathException|FileDoesNotExistException|InvalidFileException $e) {
$fileName = null;
if ($GLOBALS['TSFE']->tmpl->tt_track) {
GeneralUtility::makeInstance(TimeTracker::class)->setTSlogMessage($e->getMessage(), 3);
}
}
$fileName = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string) 'foo.text');
CODE_SAMPLE
),
]);
}

private function createSanitizeMethod(Assign $parentNode, String_ $filePath): Expression
private function createSanitizeMethod(String_ $filePath): MethodCall
{
return new Expression(new Assign($parentNode->var, $this->nodeFactory->createMethodCall(
return $this->nodeFactory->createMethodCall(
$this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\GeneralUtility',
'makeInstance',
[$this->nodeFactory->createClassConstReference('TYPO3\CMS\Frontend\Resource\FilePathSanitizer')]
),
'sanitize',
[$filePath]
)));
}

private function createNullAssignment(Assign $parentNode): Expression
{
return new Expression(new Assign($parentNode->var, $this->nodeFactory->createNull()));
}

private function createTimeTrackerLogMessage(): Expression
{
$makeInstanceOfTimeTracker = $this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\GeneralUtility',
'makeInstance',
[$this->nodeFactory->createClassConstReference('TYPO3\CMS\Core\TimeTracker\TimeTracker')]
);

return new Expression($this->nodeFactory->createMethodCall($makeInstanceOfTimeTracker, 'setTSlogMessage', [
$this->nodeFactory->createMethodCall(new Variable('e'), 'getMessage'),
$this->nodeFactory->createArg(3),
]));
}

/**
* @param Stmt[] $stmts
*/
private function createCatchBlockToLog(array $stmts): Catch_
{
return new Catch_([
new Name('TYPO3\CMS\Core\Resource\Exception\InvalidPathException'),
new Name('TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException'),
new Name('TYPO3\CMS\Core\Resource\Exception\InvalidFileException'),
], new Variable('e'), $stmts);
}

private function createCatchBlockToIgnore(Expression $assignmentNodeNull): Catch_
{
return new Catch_([new Name('TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException')], new Variable('e'), [
$assignmentNodeNull,
]);
}

private function createIfLog(): If_
{
$if = new If_($this->nodeFactory->createPropertyFetch(
$this->nodeFactory->createPropertyFetch(new ArrayDimFetch(
new Variable('GLOBALS'),
new ScalarString_(Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER)
), 'tmpl'),
'tt_track'
));

$if->stmts[] = $this->createTimeTrackerLogMessage();

return $if;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,4 @@ $fileName = $GLOBALS['TSFE']->tmpl->getFileName('foo.text');

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
use TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException;
use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\Exception\InvalidFileException;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
try {
$fileName = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string) 'foo.text');
} catch (InvalidFileNameException $e) {
$fileName = null;
} catch (InvalidPathException|FileDoesNotExistException|InvalidFileException $e) {
$fileName = null;
if ($GLOBALS['TSFE']->tmpl->tt_track) {
GeneralUtility::makeInstance(TimeTracker::class)->setTSlogMessage($e->getMessage(), 3);
}
}
$fileName = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string) 'foo.text');

0 comments on commit 72ebfe5

Please sign in to comment.