Skip to content

Commit

Permalink
[TASK] Use addJsFile instead of loadJavascriptLib (#1604)
Browse files Browse the repository at this point in the history
Resolves: #1096
  • Loading branch information
sabbelasichon authored Nov 16, 2020
1 parent b9c86a1 commit ac8c4fd
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/v9/typo3-94.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Ssch\TYPO3Rector\Rector\v9\v4\RemoveInitMethodTemplateServiceRector;
use Ssch\TYPO3Rector\Rector\v9\v4\RemoveInitTemplateMethodCallRector;
use Ssch\TYPO3Rector\Rector\v9\v4\SystemEnvironmentBuilderConstantsRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseAddJsFileInsteadOfLoadJavascriptLibRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseContextApiForVersioningWorkspaceIdRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseContextApiRector;
use Ssch\TYPO3Rector\Rector\v9\v4\UseGetMenuInsteadOfGetFirstWebPageRector;
Expand Down Expand Up @@ -53,4 +54,6 @@
$services->set(RemoveInitMethodGraphicalFunctionsRector::class);

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

$services->set(UseAddJsFileInsteadOfLoadJavascriptLibRector::class);
};
69 changes: 69 additions & 0 deletions src/Rector/v9/v4/UseAddJsFileInsteadOfLoadJavascriptLibRector.php
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
),

]);
}
}
16 changes: 16 additions & 0 deletions stubs/Backend/Template/ModuleTemplate.php
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
{
}
}
5 changes: 5 additions & 0 deletions stubs/Core/Page/PageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public function addCssFile($file, $rel = 'stylesheet', $media = 'all', $title =
{

}

public function addJsFile($file, $type = 'text/javascript', $compress = true, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $async = false, $integrity = '', $defer = false, $crossorigin = ''): void
{

}
}
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');
}
}

?>
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;
}
}

0 comments on commit ac8c4fd

Please sign in to comment.