Skip to content

Commit

Permalink
[TASK] Use method getTSConfig instead of property userTS (#1593)
Browse files Browse the repository at this point in the history
Resolves: #1054
  • Loading branch information
sabbelasichon authored Nov 13, 2020
1 parent bcb3fe1 commit 6b07c66
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/Helper/Typo3NodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ final class Typo3NodeResolver
*/
public const TYPO3_DB = 'TYPO3_DB';

/**
* @var string
*/
public const BACKEND_USER = 'BE_USER';

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v3;

use PhpParser\Node;
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\Authentication\BackendUserAuthentication;

/**
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.3/Deprecation-84984-ProtectedUserTSconfigPropertiesInBackendUserAuthentication.html
*/
final class PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector 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->shouldSkip($node)) {
return null;
}

if (! $this->isName($node->name, 'userTS')) {
return null;
}

return $this->createMethodCall($node->var, 'getTSConfig');
}

/**
* @codeCoverageIgnore
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use method getTSConfig instead of property userTS', [
new CodeSample(<<<'PHP'
if(is_array($GLOBALS['BE_USER']->userTS['tx_news.']) && $GLOBALS['BE_USER']->userTS['tx_news.']['singleCategoryAcl'] === '1') {
return true;
}
PHP
, <<<'PHP'
if(is_array($GLOBALS['BE_USER']->getTSConfig()['tx_news.']) && $GLOBALS['BE_USER']->getTSConfig()['tx_news.']['singleCategoryAcl'] === '1') {
return true;
}
PHP
),
]);
}

private function shouldSkip(PropertyFetch $node): bool
{
if ($this->typo3NodeResolver->isPropertyFetchOnAnyPropertyOfGlobals($node, Typo3NodeResolver::BACKEND_USER)) {
return false;
}
return ! $this->isObjectType($node->var, BackendUserAuthentication::class);
}
}
26 changes: 26 additions & 0 deletions stubs/Core/Authentication/BackendUserAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace TYPO3\CMS\Core\Authentication;

if (class_exists(BackendUserAuthentication::class)) {
return;
}


final class BackendUserAuthentication
{
/**
* @var array
*/
public $userTS = [
'tx_news.' => [
'singleCategoryAcl' => 1
]
];

public function getTSConfig($objectString = null, $config = null): array
{
return [];
}
}
20 changes: 6 additions & 14 deletions stubs/Core/Charset/CharsetConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,27 @@ public function getPreferredClientLanguage($languageCodesList): string
return 'foo';
}

public function strlen($charset, $string)
public function strlen($charset, $string): void
{
return mb_strlen($string, $charset);
}

public function convCapitalize($charset, $string)
public function convCapitalize($charset, $string): void
{
return mb_convert_case($string, MB_CASE_TITLE, $charset);
}

public function substr($charset, $string, $start, $len = null)
public function substr($charset, $string, $start, $len = null): void
{
return mb_substr($string, $start, $len, $charset);
}

public function conv_case($charset, $string, $case)
public function conv_case($charset, $string, $case): void
{
return $case === 'toLower'
? mb_strtolower($string, $charset)
: mb_strtoupper($string, $charset);
}

public function utf8_strpos($haystack, $needle, $offset = 0)
public function utf8_strpos($haystack, $needle, $offset = 0): void
{
return mb_strpos($haystack, $needle, $offset, 'utf-8');
}

public function utf8_strrpos($haystack, $needle)
public function utf8_strrpos($haystack, $needle): void
{
return mb_strrpos($haystack, $needle, 'utf-8');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if(is_array($GLOBALS['BE_USER']->userTS['tx_news.']) && $GLOBALS['BE_USER']->userTS['tx_news.']['singleCategoryAcl'] === '1') {
return true;
}

?>
-----
<?php

if(is_array($GLOBALS['BE_USER']->getTSConfig()['tx_news.']) && $GLOBALS['BE_USER']->getTSConfig()['tx_news.']['singleCategoryAcl'] === '1') {
return true;
}

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

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication\Fixture;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;

class MyClass
{
/**
* @var BackendUserAuthentication
*/
private $backendUserAuthentication;

public function __construct()
{
$this->backendUserAuthentication = $GLOBALS['BE_USER'];
}

public function getUserTsConfig(): array
{
return $this->backendUserAuthentication->userTS;
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication\Fixture;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;

class MyClass
{
/**
* @var BackendUserAuthentication
*/
private $backendUserAuthentication;

public function __construct()
{
$this->backendUserAuthentication = $GLOBALS['BE_USER'];
}

public function getUserTsConfig(): array
{
return $this->backendUserAuthentication->getTSConfig();
}
}

?>
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\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthentication;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Ssch\TYPO3Rector\Rector\v9\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector;
use Symplify\SmartFileSystem\SmartFileInfo;

final class PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRectorTest 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 PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector::class;
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Ssch\TYPO3Rector\Stubs\StubLoader;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\Security\FileNameValidator;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
Expand All @@ -27,6 +28,7 @@
$GLOBALS['TSFE'] = $typoScriptFrontendController;

$GLOBALS['TT'] = new TimeTracker();
$GLOBALS['BE_USER'] = new BackendUserAuthentication();
$GLOBALS['TYPO3_LOADED_EXT'] = [];
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
$GLOBALS['PARSETIME_START'] = time();
Expand Down

0 comments on commit 6b07c66

Please sign in to comment.