Skip to content

Commit

Permalink
TASK: Change extbase validators signatures (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored Nov 22, 2022
1 parent 89363f0 commit 5a69021
Show file tree
Hide file tree
Showing 10 changed files with 542 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/v12/typo3-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v12\v0\typo3\ReplacePageRepoOverlayFunctionRector::class);
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v12\v0\typo3\ImplementSiteLanguageAwareInterfaceRector::class);
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v12\v0\typo3\RegisterExtbaseTypeConvertersAsServicesRector::class);
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v12\v0\typo3\ChangeExtbaseValidatorsRector::class);
};
260 changes: 260 additions & 0 deletions src/Rector/v12/v0/typo3/ChangeExtbaseValidatorsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v12\v0\typo3;

use PhpParser\Builder\Method;
use PhpParser\Builder\Param;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/12.0/Breaking-96998-ExtbaseValidatorInterfaceChanged.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\ChangeExtbaseValidatorsRector\ChangeExtbaseValidatorsRectorTest
*/
final class ChangeExtbaseValidatorsRector extends AbstractScopeAwareRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope)
{
$classReflection = $scope->getClassReflection();

if (! $classReflection instanceof ClassReflection) {
return null;
}

if (! $classReflection->implementsInterface('TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface')) {
return null;
}

$isSubClassOfAbstractValidator = $classReflection->isSubclassOf(
'TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator'
);

$assignToOptionsProperty = $this->manipulateConstructor($node);

// Add setOptions method only if validator is not already subclass of AbstractValidator
$setOptionsClassMethod = $node->getMethod('setOptions');
if (! $setOptionsClassMethod instanceof ClassMethod && ! $isSubClassOfAbstractValidator) {
$node->stmts[] = $this->createSetOptionsClassMethod($assignToOptionsProperty);
}

if ($isSubClassOfAbstractValidator) {
$this->manipulateIsValidMethod($node);
}

$this->manipulateValidateMethod($node);

return $node;
}

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Adapt extbase validators to new interface', [
new CodeSample(
<<<'CODE_SAMPLE'
final class MyCustomValidatorWithOptions implements ValidatorInterface
{
private array $options;
private \MyDependency $myDependency;
public function __construct(array $options, \MyDependency $myDependency)
{
$this->options = $options;
$this->myDependency = $myDependency;
}
public function validate($value)
{
// Do something
}
public function getOptions(): array
{
return $this->options;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
use TYPO3\CMS\Extbase\Validation\ValidatorResolver\ValidatorResolver;
final class MyCustomValidatorWithoutOptions implements ValidatorInterface
{
private array $options;
private \MyDependency $myDependency;
public function __construct(\MyDependency $myDependency)
{
$this->myDependency = $myDependency;
}
public function validate($value)
{
// Do something
}
public function getOptions(): array
{
return $this->options;
}
public function setOptions(array $options): void
{
$this->options = $options;
}
}
CODE_SAMPLE
),
]);
}

private function createSetOptionsClassMethod(bool $assignToOptionsProperty): ClassMethod
{
$paramBuilder = new Param('options');
$paramBuilder->setType('array');

$methodBuilder = new Method('setOptions');
$methodBuilder->makePublic();
$methodBuilder->addParam($paramBuilder->getNode());
$methodBuilder->setReturnType('void');

$methodNode = $methodBuilder->getNode();

if ($assignToOptionsProperty) {
$methodNode->stmts[] = new Expression($this->nodeFactory->createPropertyAssignment('options'));
}

return $methodNode;
}

private function shouldKeepConstructorStatement(Stmt $constructorStmt): bool
{
if (! $constructorStmt instanceof Expression) {
return true;
}

if (! $constructorStmt->expr instanceof Assign && ! $constructorStmt->expr instanceof StaticCall) {
return true;
}

if ($constructorStmt->expr instanceof StaticCall) {
return $this->shouldKeepStaticCall($constructorStmt->expr);
}

return $this->shouldKeepAssignment($constructorStmt->expr);
}

private function manipulateConstructor(Class_ $node): bool
{
$constructorMethod = $node->getMethod(MethodName::CONSTRUCT);

if (! $constructorMethod instanceof ClassMethod) {
return false;
}

$assignToOptionsProperty = false;
$constructorParams = [];
foreach ($constructorMethod->getParams() as $param) {
if ($this->nodeNameResolver->isName($param, 'options')) {
$assignToOptionsProperty = true;
continue;
}

$constructorParams[] = $param;
}

$constructorStatementsToKeep = [];
if (is_array($constructorMethod->stmts)) {
foreach ($constructorMethod->stmts as $constructorStmt) {
if ($this->shouldKeepConstructorStatement($constructorStmt)) {
$constructorStatementsToKeep[] = $constructorStmt;
}
}
}

$constructorMethod->params = $constructorParams;
$constructorMethod->stmts = $constructorStatementsToKeep;

return $assignToOptionsProperty;
}

private function shouldKeepAssignment(Assign $assign): bool
{
if (! $assign->var instanceof PropertyFetch) {
return true;
}

return ! $this->nodeNameResolver->isName($assign->var, 'options');
}

private function shouldKeepStaticCall(StaticCall $staticCall): bool
{
if (! $staticCall->class instanceof Name) {
return true;
}

return ! $this->isName($staticCall->class, ObjectReference::PARENT);
}

private function manipulateIsValidMethod(Class_ $node): void
{
$isValidClassMethod = $node->getMethod('isValid');

if (! $isValidClassMethod instanceof ClassMethod) {
return;
}

if (null !== $isValidClassMethod->returnType) {
return;
}

$isValidClassMethod->returnType = new Identifier('void');
}

private function manipulateValidateMethod(Class_ $node): void
{
$validateClassMethod = $node->getMethod('validate');

if (! $validateClassMethod instanceof ClassMethod) {
return;
}

if (null !== $validateClassMethod->returnType) {
return;
}

$validateClassMethod->returnType = new FullyQualified('TYPO3\CMS\Extbase\Error\Result');
}
}
11 changes: 11 additions & 0 deletions stubs/TYPO3/CMS/Extbase/Error/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace TYPO3\CMS\Extbase\Error;

if (class_exists('TYPO3\CMS\Extbase\Error\Result')) {
return;
}

class Result
{

}
25 changes: 25 additions & 0 deletions stubs/TYPO3/CMS/Extbase/Validation/Validator/AbstractValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TYPO3\CMS\Extbase\Validation\Validator;

if (class_exists('TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator')) {
return;
}

abstract class AbstractValidator implements ValidatorInterface
{
public function validate($value)
{

}

public function getOptions(): array
{
return [];
}

public function setOptions(array $options): void
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TYPO3\CMS\Extbase\Validation\Validator;

if (interface_exists('TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface')) {
return;
}

interface ValidatorInterface
{
/**
* Checks if the given value is valid according to the validator, and returns
* the Error Messages object which occurred.
*
* @param mixed $value The value that should be validated
*/
public function validate($value);

/**
* Receive validator options from framework.
*/
public function setOptions(array $options): void;

/**
* Returns the options of this validator which can be specified by setOptions().
*/
public function getOptions(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\ChangeExtbaseValidatorsRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ChangeExtbaseValidatorsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Loading

0 comments on commit 5a69021

Please sign in to comment.