-
-
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 the new ExtensionConfiguration API (#1649)
Resolves: #925
- Loading branch information
1 parent
d59f230
commit 8c97a7d
Showing
8 changed files
with
222 additions
and
1 deletion.
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
134 changes: 134 additions & 0 deletions
134
src/Rector/v9/v0/UseExtensionConfigurationApiRector.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,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v9\v0; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver; | ||
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.0/Deprecation-82254-DeprecateGLOBALSTYPO3_CONF_VARSEXTextConf.html | ||
*/ | ||
final class UseExtensionConfigurationApiRector extends AbstractRector | ||
{ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node->name, 'unserialize')) { | ||
return null; | ||
} | ||
|
||
$firstArgument = array_shift($node->args); | ||
|
||
if (null === $firstArgument) { | ||
return null; | ||
} | ||
|
||
if (! $firstArgument->value instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
$extensionConfiguration = $firstArgument->value; | ||
|
||
if ($this->shouldSkip($extensionConfiguration)) { | ||
return null; | ||
} | ||
|
||
return $this->createMethodCall($this->createStaticCall(GeneralUtility::class, 'makeInstance', [ | ||
$this->createClassConstantReference(ExtensionConfiguration::class), | ||
]), 'get', [$extensionConfiguration->dim]); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition( | ||
'Use the new ExtensionConfiguration API instead of $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][\'foo\']', | ||
[ | ||
new CodeSample(<<<'PHP' | ||
$extensionConfiguration2 = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['foo'], ['allowed_classes' => false]); | ||
PHP | ||
, <<<'PHP' | ||
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$extensionConfiguration2 = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('foo'); | ||
PHP | ||
), | ||
|
||
]); | ||
} | ||
|
||
private function shouldSkip(ArrayDimFetch $node): bool | ||
{ | ||
$extConf = $node->var; | ||
if (! $extConf instanceof ArrayDimFetch) { | ||
return true; | ||
} | ||
|
||
if (null === $extConf->dim) { | ||
return true; | ||
} | ||
|
||
if (! $this->isValue($extConf->dim, 'extConf')) { | ||
return true; | ||
} | ||
|
||
if (! property_exists($node->var, 'var')) { | ||
return true; | ||
} | ||
|
||
$ext = $node->var->var; | ||
if (! $ext instanceof ArrayDimFetch) { | ||
return true; | ||
} | ||
|
||
if (null === $ext->dim) { | ||
return true; | ||
} | ||
|
||
if (! $this->isValue($ext->dim, 'EXT')) { | ||
return true; | ||
} | ||
|
||
$typo3ConfVars = $node->var->var->var; | ||
if (! $typo3ConfVars instanceof ArrayDimFetch) { | ||
return true; | ||
} | ||
|
||
if (null === $typo3ConfVars->dim) { | ||
return true; | ||
} | ||
|
||
if (! $this->isValue($typo3ConfVars->dim, 'TYPO3_CONF_VARS')) { | ||
return true; | ||
} | ||
|
||
$globals = $node->var->var->var->var; | ||
if (! $this->isName($globals, Typo3NodeResolver::GLOBALS)) { | ||
return true; | ||
} | ||
|
||
if (null === $node->dim) { | ||
return true; | ||
} | ||
|
||
return ! $this->isName($node->dim, '_EXTKEY') && null === $this->getValue($node->dim); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TYPO3\CMS\Core\Configuration; | ||
|
||
|
||
if (class_exists(ExtensionConfiguration::class)) { | ||
return; | ||
} | ||
|
||
final class ExtensionConfiguration | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function get(string $extension, string $path = '') | ||
{ | ||
return []; | ||
} | ||
|
||
public function set(string $extension, string $path = '', $value = null): 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
23 changes: 23 additions & 0 deletions
23
...Rector/v9/v0/UseExtensionConfigurationApi/Fixture/use_extension_configuration_api.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 | ||
|
||
$extensionConfiguration2 = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['foo'], ['allowed_classes' => false]); | ||
|
||
$_EXTKEY = 'foo'; | ||
call_user_func(static function () use($_EXTKEY) { | ||
$_EXTCONF = isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY]) ? unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY], ['allowed_classes' => false]) : []; | ||
}); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; | ||
$extensionConfiguration2 = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('foo'); | ||
$_EXTKEY = 'foo'; | ||
call_user_func(static function () use($_EXTKEY) { | ||
$_EXTCONF = isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY]) ? GeneralUtility::makeInstance(ExtensionConfiguration::class)->get($_EXTKEY) : []; | ||
}); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
tests/Rector/v9/v0/UseExtensionConfigurationApi/UseExtensionConfigurationApiRectorTest.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\v0\UseExtensionConfigurationApi; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Rector\v9\v0\UseExtensionConfigurationApiRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseExtensionConfigurationApiRectorTest 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 UseExtensionConfigurationApiRector::class; | ||
} | ||
} |
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