Skip to content

Commit 22d2e27

Browse files
authored
[TASK] Add MigrateTypoScriptConditionGetTSFEFractor (#360)
Resolves: sabbelasichon/typo3-rector#4653
1 parent d528a58 commit 22d2e27

File tree

6 files changed

+154
-1
lines changed

6 files changed

+154
-1
lines changed

packages/typo3-fractor/config/typo3-14.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use a9f\Typo3Fractor\TYPO3v14\Fluid\ChangeLogoutHandlingInFeLoginFractor;
66
use a9f\Typo3Fractor\TYPO3v14\Htaccess\RemoveUploadsFromDefaultHtaccessFractor;
7+
use a9f\Typo3Fractor\TYPO3v14\TypoScript\MigrateTypoScriptConditionGetTSFEFractor;
78
use a9f\Typo3Fractor\TYPO3v14\TypoScript\RemoveExposeNonexistentUserInForgotPasswordDialogSettingInFeLoginFractor;
89
use a9f\Typo3Fractor\TYPO3v14\TypoScript\RemoveModWebLayoutDefLangBindingFractor;
910
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@@ -18,4 +19,5 @@
1819
$services->set(RemoveModWebLayoutDefLangBindingFractor::class);
1920
$services->set(ChangeLogoutHandlingInFeLoginFractor::class);
2021
$services->set(RemoveUploadsFromDefaultHtaccessFractor::class);
22+
$services->set(MigrateTypoScriptConditionGetTSFEFractor::class);
2123
};

packages/typo3-fractor/docs/typo3-fractor-rules.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 34 Rules Overview
1+
# 35 Rules Overview
22

33
## AbstractMessageGetSeverityFluidFractor
44

@@ -418,6 +418,21 @@ Migrates option cols to size for TCA type none
418418

419419
<br>
420420

421+
## MigrateTypoScriptConditionGetTSFEFractor
422+
423+
Migrate TypoScript condition function `getTSFE()`
424+
425+
- class: [`a9f\Typo3Fractor\TYPO3v14\TypoScript\MigrateTypoScriptConditionGetTSFEFractor`](../rules/TYPO3v14/TypoScript/MigrateTypoScriptConditionGetTSFEFractor.php)
426+
427+
```diff
428+
-[getTSFE() && getTSFE().id == 42]
429+
+[request?.getPageArguments()?.getPageId() == 42]
430+
temp.foo = 42
431+
[end]
432+
```
433+
434+
<br>
435+
421436
## RemoveConfigDisablePageExternalUrlFractor
422437

423438
Remove config.disablePageExternalUrl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[getTSFE() && getTSFE().id == 42]
2+
temp.foo = 42
3+
[end]
4+
5+
[getTSFE().id == 43]
6+
temp.foo = 43
7+
[end]
8+
9+
[getTSFE()?.id == 44]
10+
temp.foo = 44
11+
[end]
12+
-----
13+
[request?.getPageArguments()?.getPageId() == 42]
14+
temp.foo = 42
15+
[end]
16+
17+
[request?.getPageArguments()?.getPageId() == 43]
18+
temp.foo = 43
19+
[end]
20+
21+
[request?.getPageArguments()?.getPageId() == 44]
22+
temp.foo = 44
23+
[end]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Typo3Fractor\Tests\TYPO3v14\TypoScript\MigrateTypoScriptConditionGetTSFEFractor;
6+
7+
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
final class MigrateTypoScriptConditionGetTSFEFractorTest extends AbstractFractorTestCase
11+
{
12+
#[DataProvider('provideData')]
13+
public function test(string $filePath): void
14+
{
15+
$this->doTestFile($filePath);
16+
}
17+
18+
public static function provideData(): \Iterator
19+
{
20+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.typoscript.fixture');
21+
}
22+
23+
public function provideConfigFilePath(): string
24+
{
25+
return __DIR__ . '/config/fractor.php';
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use a9f\Fractor\Configuration\FractorConfiguration;
6+
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
7+
use a9f\Typo3Fractor\TYPO3v14\TypoScript\MigrateTypoScriptConditionGetTSFEFractor;
8+
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConditionTermination;
9+
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;
10+
11+
return FractorConfiguration::configure()
12+
->withRules([MigrateTypoScriptConditionGetTSFEFractor::class])
13+
->withOptions([
14+
TypoScriptProcessorOption::INDENT_SIZE => 4,
15+
TypoScriptProcessorOption::INDENT_CHARACTER => PrettyPrinterConfiguration::INDENTATION_STYLE_SPACES,
16+
TypoScriptProcessorOption::ADD_CLOSING_GLOBAL => false,
17+
TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS => true,
18+
TypoScriptProcessorOption::INDENT_CONDITIONS => true,
19+
TypoScriptProcessorOption::CONDITION_TERMINATION => PrettyPrinterConditionTermination::Keep,
20+
]);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Typo3Fractor\TYPO3v14\TypoScript;
6+
7+
use a9f\FractorTypoScript\AbstractTypoScriptFractor;
8+
use Helmich\TypoScriptParser\Parser\AST\ConditionalStatement;
9+
use Helmich\TypoScriptParser\Parser\AST\Statement;
10+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
11+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
12+
13+
/**
14+
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/14.0/Breaking-107473-TypoScriptConditionFunctionGetTSFERemoved.html
15+
* @see \a9f\Typo3Fractor\Tests\TYPO3v14\TypoScript\MigrateTypoScriptConditionGetTSFEFractor\MigrateTypoScriptConditionGetTSFEFractorTest
16+
*/
17+
final class MigrateTypoScriptConditionGetTSFEFractor extends AbstractTypoScriptFractor
18+
{
19+
public function getRuleDefinition(): RuleDefinition
20+
{
21+
return new RuleDefinition('Migrate TypoScript condition function getTSFE()', [new CodeSample(
22+
<<<'CODE_SAMPLE'
23+
[getTSFE() && getTSFE().id == 42]
24+
temp.foo = 42
25+
[end]
26+
CODE_SAMPLE
27+
,
28+
<<<'CODE_SAMPLE'
29+
[request?.getPageArguments()?.getPageId() == 42]
30+
temp.foo = 42
31+
[end]
32+
CODE_SAMPLE
33+
)]);
34+
}
35+
36+
public function refactor(Statement $statement): ?Statement
37+
{
38+
if ($this->shouldSkip($statement)) {
39+
return null;
40+
}
41+
42+
/** @var ConditionalStatement $statement */
43+
$condition = str_replace('getTSFE() && ', '', $statement->condition);
44+
45+
$statement->condition = str_replace(
46+
['getTSFE().id', 'getTSFE()?.id'],
47+
'request?.getPageArguments()?.getPageId()',
48+
$condition
49+
);
50+
51+
return $statement;
52+
}
53+
54+
private function shouldSkip(Statement $statement): bool
55+
{
56+
if (! $statement instanceof ConditionalStatement) {
57+
return true;
58+
}
59+
60+
if (! str_contains($statement->condition, 'getTSFE')) {
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
}

0 commit comments

Comments
 (0)