-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Copy method getPidForModTSconfig over (#1595)
Resolves: #1069
- Loading branch information
1 parent
ab761e7
commit 4a759b3
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../Rector/v9/v3/CopyMethodGetPidForModTSconfig/CopyMethodGetPidForModTSconfigRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...v9/v3/CopyMethodGetPidForModTSconfig/Fixture/copy_method_get_pid_for_mod_tsconfig.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
46 changes: 46 additions & 0 deletions
46
...etPidForModTSconfig/Fixture/copy_method_get_pid_for_mod_tsconfig_in_class_context.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} | ||
|
||
?> |