Skip to content

Commit

Permalink
[FEATURE] Add MigrateGeneralUtilityHmacToHashServiceHmacRector
Browse files Browse the repository at this point in the history
Resolves #4248
  • Loading branch information
simonschaufi committed May 16, 2024
1 parent d4e7804 commit 47260a0
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/typo3-13.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
$rectorConfig->import(__DIR__ . '/v13/tca-130.php');
$rectorConfig->import(__DIR__ . '/v13/typo3-130.php');
$rectorConfig->import(__DIR__ . '/v13/typo3-130-extbase-hash-service-core-hash-service.php');
$rectorConfig->import(__DIR__ . '/v13/typo3-131.php');
};
11 changes: 11 additions & 0 deletions config/v13/typo3-131.php
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);
};
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');
}
}
16 changes: 10 additions & 6 deletions stubs/TYPO3/CMS/Core/Crypto/HashService.php
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 '';
}
}
9 changes: 8 additions & 1 deletion stubs/TYPO3/CMS/Core/Utility/GeneralUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static function getApplicationContext()
* @phpstan-param mixed $constructorArguments
* @phpstan-return T
*/
public static function makeInstance($className, ...$constructorArguments);
public static function makeInstance($className, ...$constructorArguments)
{
}

/**
* @return void
Expand Down Expand Up @@ -440,4 +442,9 @@ public static function _POST($var = null)
{
return $var;
}

public static function hmac($input, $additionalSecret = '')
{
return '';
}
}
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');

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

0 comments on commit 47260a0

Please sign in to comment.