Skip to content

Commit

Permalink
[TASK] Copy method getPidForModTSconfig over (#1595)
Browse files Browse the repository at this point in the history
Resolves: #1069
  • Loading branch information
sabbelasichon authored Nov 13, 2020
1 parent ab761e7 commit 4a759b3
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/v9/typo3-93.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\v9\v3\BackendUtilityGetModuleUrlRector;
use Ssch\TYPO3Rector\Rector\v9\v3\CopyMethodGetPidForModTSconfigRector;
use Ssch\TYPO3Rector\Rector\v9\v3\PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector;
use Ssch\TYPO3Rector\Rector\v9\v3\RemoveColPosParameterRector;
use Ssch\TYPO3Rector\Rector\v9\v3\UseMethodGetPageShortcutDirectlyFromSysPageRector;
Expand Down Expand Up @@ -36,4 +37,5 @@
$services->set(BackendUtilityGetModuleUrlRector::class);
$services->set(PropertyUserTsToMethodGetTsConfigOfBackendUserAuthenticationRector::class);
$services->set(UseMethodGetPageShortcutDirectlyFromSysPageRector::class);
$services->set(CopyMethodGetPidForModTSconfigRector::class);
};
76 changes: 76 additions & 0 deletions src/Rector/v9/v3/CopyMethodGetPidForModTSconfigRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v3;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\MathUtility;

/**
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.3/Deprecation-84994-BackendUtilitygetPidForModTSconfigDeprecated.html
*/
final class CopyMethodGetPidForModTSconfigRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isMethodStaticCallOrClassMethodObjectType($node, BackendUtility::class)) {
return null;
}

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

$tableVariableNode = $node->args[0]->value;
if ($tableVariableNode instanceof String_) {
$tableVariableNode = new Variable('table');
$this->addNodeBeforeNode(new Assign($tableVariableNode, $node->args[0]->value), $node);
}

return new Ternary(new BooleanAnd(
new Identical($tableVariableNode, new String_('pages')),
$this->createStaticCall(MathUtility::class, 'canBeInterpretedAsInteger', [$node->args[1]])
), $node->args[1]->value, $node->args[2]->value);
}

/**
* @codeCoverageIgnore
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Copy method getPidForModTSconfig of class BackendUtility over', [
new CodeSample(<<<'PHP'
use TYPO3\CMS\Backend\Utility\BackendUtility;BackendUtility::getPidForModTSconfig('pages', 1, 2);
PHP
, <<<'PHP'
use TYPO3\CMS\Core\Utility\MathUtility;
$table = 'pages';
$uid = 1;
$pid = 2;
$table === 'pages' && MathUtility::canBeInterpretedAsInteger($uid) ? $uid : $pid;
PHP
),
]);
}
}
5 changes: 5 additions & 0 deletions stubs/Backend/Utility/BackendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public static function shortcutExists(string $url): bool
{
return true;
}

public static function getPidForModTSconfig($table, $uid, $pid): int
{
return 1;
}
}
18 changes: 18 additions & 0 deletions stubs/Core/Utility/MathUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);


namespace TYPO3\CMS\Core\Utility;

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

final class MathUtility
{

public static function canBeInterpretedAsInteger($uid): bool
{
return true;
}
}
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\CopyMethodGetPidForModTSconfig;

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

final class CopyMethodGetPidForModTSconfigRectorTest 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 CopyMethodGetPidForModTSconfigRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use TYPO3\CMS\Backend\Utility\BackendUtility;

$modPid1 = BackendUtility::getPidForModTSconfig('pages', 1, 2);

$table = 'pages';
$uid = 1;
$pid = 2;
$modPid2 = BackendUtility::getPidForModTSconfig($table, $uid, $pid);

?>
-----
<?php

use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
$table = 'pages';
$modPid1 = $table === 'pages' && MathUtility::canBeInterpretedAsInteger(1) ? 1 : 2;
$table = 'pages';
$uid = 1;
$pid = 2;
$modPid2 = $table === 'pages' && MathUtility::canBeInterpretedAsInteger($uid) ? $uid : $pid;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

use TYPO3\CMS\Backend\Utility\BackendUtility;

class MyClass
{
/**
* @var string
*/
private $table = 'pages';

public function __construct()
{
$modPid1 = BackendUtility::getPidForModTSconfig($this->table, 1, 2);
}


}

?>
-----
<?php

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

use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;

class MyClass
{
/**
* @var string
*/
private $table = 'pages';

public function __construct()
{
$modPid1 = $this->table === 'pages' && MathUtility::canBeInterpretedAsInteger(1) ? 1 : 2;
}


}

?>

0 comments on commit 4a759b3

Please sign in to comment.