Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Refactor deprecated properties of TSFE #1606

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/v9/typo3-95.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Ssch\TYPO3Rector\Rector\Migrations\RenameClassMapAliasRector;
use Ssch\TYPO3Rector\Rector\v9\v4\RefactorPropertiesOfTypoScriptFrontendControllerRector;

use Ssch\TYPO3Rector\Rector\v9\v5\RefactorProcessOutputRector;
use Ssch\TYPO3Rector\Rector\v9\v5\RemoveFlushCachesRector;
Expand Down Expand Up @@ -59,4 +60,6 @@
$services->set(RemoveInitMethodFromPageRepositoryRector::class);

$services->set(RefactorProcessOutputRector::class);

$services->set(RefactorPropertiesOfTypoScriptFrontendControllerRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v5;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.5/Deprecation-86047-TSFEPropertiesMethodsAndChangeVisibility.html
*/
final class RefactorPropertiesOfTypoScriptFrontendControllerRector extends AbstractRector
{
/**
* @var Typo3NodeResolver
*/
private $typo3NodeResolver;

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

public function getNodeTypes(): array
{
return [PropertyFetch::class];
}

/**
* @param PropertyFetch $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node->var, TypoScriptFrontendController::class)
&& ! $this->typo3NodeResolver->isPropertyFetchOnAnyPropertyOfGlobals(
$node,
Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER
)) {
return null;
}

$parentNode = $node->getAttribute('parent');

// Check if we have an assigment to the property, if so do not change it
if ($parentNode instanceof Assign && $parentNode->var instanceof PropertyFetch) {
return null;
}

if (! $this->isNames($node->name, ['ADMCMD_preview_BEUSER_uid', 'workspacePreview', 'loginAllowedInBranch'])) {
return null;
}

if ($this->isName($node->name, 'loginAllowedInBranch')) {
return $this->createMethodCall($node->var, 'checkIfLoginAllowedInBranch');
}

$contextInstanceNode = $this->createStaticCall(GeneralUtility::class, 'makeInstance', [
$this->createClassConstantReference(Context::class),
]);

if ($this->isName($node->name, 'ADMCMD_preview_BEUSER_uid')) {
return $this->createMethodCall($contextInstanceNode, 'getPropertyFromAspect', ['backend.user', 'id', 0]);
}

return $this->createMethodCall($contextInstanceNode, 'getPropertyFromAspect', ['workspace', 'id', 0]);
}

/**
* @codeCoverageIgnore
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Refactor some properties of TypoScriptFrontendController', [
new CodeSample(<<<'PHP'
$previewBeUserUid = $GLOBALS['TSFE']->ADMCMD_preview_BEUSER_uid;
$workspacePreview = $GLOBALS['TSFE']->workspacePreview;
$loginAllowedInBranch = $GLOBALS['TSFE']->loginAllowedInBranch;
PHP
, <<<'PHP'
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;
$previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0);
$workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0);
$loginAllowedInBranch = $GLOBALS['TSFE']->checkIfLoginAllowedInBranch();
PHP
),
]);
}
}
21 changes: 21 additions & 0 deletions stubs/Frontend/Controller/TypoScriptFrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TYPO3\CMS\Frontend\Controller;

use TYPO3\CMS\Core\Charset\CharsetConverter;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\TypoScript\TemplateService;
Expand Down Expand Up @@ -102,10 +103,30 @@ final class TypoScriptFrontendController
*/
public $sys_language_contentOL = 0;

/**
* @var int
*/
public $ADMCMD_preview_BEUSER_uid = 0;

/**
* @var int
*/
public $workspacePreview = 0;

/**
* @var bool
*/
public $loginAllowedInBranch = false;

public function initTemplate(): void
{
}

public function checkIfLoginAllowedInBranch(): bool
{
return false;
}

public function __construct()
{
//fake template object, otherwise tests cannot access this property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$GLOBALS['TSFE']->loginAllowedInBranch = false;
$previewBeUserUid = $GLOBALS['TSFE']->ADMCMD_preview_BEUSER_uid;
$workspacePreview = $GLOBALS['TSFE']->workspacePreview;
$loginAllowedInBranch = $GLOBALS['TSFE']->loginAllowedInBranch;

?>
-----
<?php

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;
$GLOBALS['TSFE']->loginAllowedInBranch = false;
$previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0);
$workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0);
$loginAllowedInBranch = $GLOBALS['TSFE']->checkIfLoginAllowedInBranch();
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v5\RefactorPropertiesOfTypoScriptFrontendController\Fixture;

use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class FooBarBazTypoScriptFrontendController
{
public function doSomething(): void
{
$this->getTypoScriptFrontendController()->loginAllowedInBranch = false;
$previewBeUserUid = $this->getTypoScriptFrontendController()->ADMCMD_preview_BEUSER_uid;
$workspacePreview = $this->getTypoScriptFrontendController()->workspacePreview;
$loginAllowedInBranch = $this->getTypoScriptFrontendController()->loginAllowedInBranch;
}

/**
* @return TypoScriptFrontendController
*/
private function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v5\RefactorPropertiesOfTypoScriptFrontendController\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class FooBarBazTypoScriptFrontendController
{
public function doSomething(): void
{
$this->getTypoScriptFrontendController()->loginAllowedInBranch = false;
$previewBeUserUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'id', 0);
$workspacePreview = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0);
$loginAllowedInBranch = $this->getTypoScriptFrontendController()->checkIfLoginAllowedInBranch();
}

/**
* @return TypoScriptFrontendController
*/
private function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v5\RefactorPropertiesOfTypoScriptFrontendController;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Ssch\TYPO3Rector\Rector\v9\v5\RefactorPropertiesOfTypoScriptFrontendControllerRector;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RefactorPropertiesOfTypoScriptFrontendControllerRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return RefactorPropertiesOfTypoScriptFrontendControllerRector::class;
}
}