-
-
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.
[FEATURE] Add MigrateGeneralUtilityHmacToHashServiceHmacRector
Resolves #4248
- Loading branch information
1 parent
d4e7804
commit 47260a0
Showing
8 changed files
with
168 additions
and
7 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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Ssch\TYPO3Rector\TYPO313\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../config.php'); | ||
$rectorConfig->rule(MigrateGeneralUtilityHmacToHashServiceHmacRector::class); | ||
}; |
75 changes: 75 additions & 0 deletions
75
rules/TYPO313/v1/MigrateGeneralUtilityHmacToHashServiceHmacRector.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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\TYPO313\v1; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.1/Deprecation-102762-GeneralUtilityhmac.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v13\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector\MigrateGeneralUtilityHmacToHashServiceHmacRectorTest | ||
*/ | ||
final class MigrateGeneralUtilityHmacToHashServiceHmacRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Migrate GeneralUtility::hmac to HashService::hmac', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$hmac = GeneralUtility::hmac('some-input', 'some-secret'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use TYPO3\CMS\Core\Crypto\HashService; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$hmac = GeneralUtility::makeInstance(HashService::class)->hmac('some-input', 'some-secret'); | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$this->nodeFactory->createStaticCall('TYPO3\CMS\Core\Utility\GeneralUtility', 'makeInstance', [ | ||
$this->nodeFactory->createClassConstReference('TYPO3\CMS\Core\Crypto\HashService'), | ||
]), | ||
'hmac', | ||
$node->getArgs() | ||
); | ||
} | ||
|
||
private function shouldSkip(StaticCall $staticCall): bool | ||
{ | ||
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( | ||
$staticCall, | ||
new ObjectType('TYPO3\CMS\Core\Utility\GeneralUtility') | ||
)) { | ||
return true; | ||
} | ||
|
||
return ! $this->isName($staticCall->name, 'hmac'); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,28 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TYPO3\CMS\Core\Crypto; | ||
|
||
if(class_exists('TYPO3\CMS\Core\Crypto\HashService')) { | ||
if (class_exists('TYPO3\CMS\Core\Crypto\HashService')) { | ||
return; | ||
} | ||
|
||
final class HashService | ||
{ | ||
public function hmac(string $input, string $additionalSecret): void | ||
public function hmac(string $input, string $additionalSecret): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function appendHmac(string $string, string $additionalSecret): void | ||
public function appendHmac(string $string, string $additionalSecret): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function validateHmac(string $string, string $additionalSecret, string $hmac): void | ||
public function validateHmac(string $string, string $additionalSecret, string $hmac): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function validateAndStripHmac(string $string, string $additionalSecret): void | ||
public function validateAndStripHmac(string $string, string $additionalSecret): string | ||
{ | ||
|
||
return ''; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
tests/Rector/v13/v1/MigrateGeneralUtilityHmacToHashServiceHmacRector/Fixture/fixture.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,20 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$hmac = GeneralUtility::hmac('some-input', 'some-secret'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Crypto\HashService; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$hmac = GeneralUtility::makeInstance(HashService::class)->hmac('some-input', 'some-secret'); | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ilityHmacToHashServiceHmacRector/MigrateGeneralUtilityHmacToHashServiceHmacRectorTest.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MigrateGeneralUtilityHmacToHashServiceHmacRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Rector/v13/v1/MigrateGeneralUtilityHmacToHashServiceHmacRector/config/configured_rule.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Ssch\TYPO3Rector\TYPO313\v1\MigrateGeneralUtilityHmacToHashServiceHmacRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(MigrateGeneralUtilityHmacToHashServiceHmacRector::class); | ||
}; |