-
-
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] Use addJsFile instead of loadJavascriptLib (#1604)
Resolves: #1096
- Loading branch information
1 parent
b9c86a1
commit ac8c4fd
Showing
6 changed files
with
170 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
69 changes: 69 additions & 0 deletions
69
src/Rector/v9/v4/UseAddJsFileInsteadOfLoadJavascriptLibRector.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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v9\v4; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Core\Page\PageRenderer; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.4/Deprecation-85701-MethodsInModuleTemplate.html | ||
*/ | ||
final class UseAddJsFileInsteadOfLoadJavascriptLibRector extends AbstractRector | ||
{ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isMethodStaticCallOrClassMethodObjectType($node, ModuleTemplate::class)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'loadJavascriptLib')) { | ||
return null; | ||
} | ||
|
||
return $this->createMethodCall($this->createStaticCall( | ||
GeneralUtility::class, 'makeInstance', [$this->createClassConstantReference(PageRenderer::class)] | ||
), 'addJsFile', $node->args); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition( | ||
'Use method addJsFile of class PageRenderer instead of method loadJavascriptLib of class ModuleTemplate', | ||
[ | ||
new CodeSample(<<<'PHP' | ||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); | ||
$moduleTemplate->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js'); | ||
PHP | ||
, <<<'PHP' | ||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Core\Page\PageRenderer; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); | ||
GeneralUtility::makeInstance(PageRenderer::class)->addJsFile('sysext/backend/Resources/Public/JavaScript/md5.js'); | ||
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,16 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
|
||
namespace TYPO3\CMS\Backend\Template; | ||
|
||
if (class_exists(ModuleTemplate::class)) { | ||
return; | ||
} | ||
|
||
final class ModuleTemplate | ||
{ | ||
public function loadJavascriptLib($lib): void | ||
{ | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
tests/Rector/v9/v4/UseAddJsFileInsteadOfLoadJavascriptLib/Fixture/use_add_js_file.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\v4\UseAddJsFileInsteadOfLoadJavascriptLib\Fixture; | ||
|
||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class MyClass | ||
{ | ||
/** | ||
* @var ModuleTemplate | ||
*/ | ||
private $moduleTemplate; | ||
|
||
public function __construct() | ||
{ | ||
$this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); | ||
$this->moduleTemplate->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v9\v4\UseAddJsFileInsteadOfLoadJavascriptLib\Fixture; | ||
|
||
use TYPO3\CMS\Core\Page\PageRenderer; | ||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class MyClass | ||
{ | ||
/** | ||
* @var ModuleTemplate | ||
*/ | ||
private $moduleTemplate; | ||
|
||
public function __construct() | ||
{ | ||
$this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); | ||
GeneralUtility::makeInstance(PageRenderer::class)->addJsFile('sysext/backend/Resources/Public/JavaScript/md5.js'); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...eAddJsFileInsteadOfLoadJavascriptLib/UseAddJsFileInsteadOfLoadJavascriptLibRectorTest.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\v4\UseAddJsFileInsteadOfLoadJavascriptLib; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Rector\v9\v4\UseAddJsFileInsteadOfLoadJavascriptLibRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseAddJsFileInsteadOfLoadJavascriptLibRectorTest 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 UseAddJsFileInsteadOfLoadJavascriptLibRector::class; | ||
} | ||
} |