Skip to content

Commit

Permalink
[DX] Add input validation for method, property and function name to a…
Browse files Browse the repository at this point in the history
…void invalid output ast (#2668)
  • Loading branch information
TomasVotruba committed Jul 16, 2022
1 parent e8d0d57 commit 6577da6
Show file tree
Hide file tree
Showing 31 changed files with 147 additions and 13 deletions.
10 changes: 5 additions & 5 deletions packages-tests/NodeCollector/BinaryOpConditionsCollectorTest.php
Expand Up @@ -24,7 +24,7 @@ public function testLeftAssociative(): void

$result = $binaryOpConditionsCollector->findConditions($abcPlus, Plus::class);

$this->assertEquals([
$this->assertSame([
2 => $a,
1 => $b,
0 => $c,
Expand All @@ -44,7 +44,7 @@ public function testRightAssociative(): void

$result = $binaryOpConditionsCollector->findConditions($abcPlus, Plus::class);

$this->assertEquals([
$this->assertSame([
1 => $a,
0 => $bcPlus,
], $result);
Expand All @@ -62,7 +62,7 @@ public function testWrongRootOp(): void

$result = $binaryOpConditionsCollector->findConditions($abcMinus, Plus::class);

$this->assertEquals([
$this->assertSame([
0 => $abcMinus,
], $result);
}
Expand All @@ -75,7 +75,7 @@ public function testTrivialCase(): void

$result = $binaryOpConditionsCollector->findConditions($variable, Plus::class);

$this->assertEquals([
$this->assertSame([
0 => $variable,
], $result);
}
Expand All @@ -93,7 +93,7 @@ public function testInnerNodeDifferentOp(): void

$result = $binaryOpConditionsCollector->findConditions($abcPlus, Plus::class);

$this->assertEquals([
$this->assertSame([
1 => $abMinus,
0 => $c,
], $result);
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Expand Up @@ -663,3 +663,8 @@ parameters:
- '#Class method ".+\(\)" is never used#'

- '#Parameters should use "(.*?)" types as the only types passed to this method#'

# regex re-validation
-
message: '#Call to static method Webmozart\\Assert\\Assert\:\:allString\(\) with array<string> will always evaluate to true#'
path: rules/Transform/ValueObject/ParentClassToTraits.php
Expand Up @@ -11,7 +11,6 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(PropertyFetchToMethodCallRector::class, [

new PropertyFetchToMethodCall(Translator::class, 'locale', 'getLocale', 'setLocale'),
new PropertyFetchToMethodCall(Generator::class, 'word', 'word'),
new PropertyFetchToMethodCall(
Expand Down
Expand Up @@ -159,6 +159,7 @@ private function refactorPropertyFetch(PropertyFetch $propertyFetch, Scope $scop
if (! $callerType->hasMethod($possibleGetterMethodName)->yes()) {
continue;
}

$methodReflection = $callerType->getMethod($possibleGetterMethodName, $scope);

$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
Expand Down
4 changes: 4 additions & 0 deletions rules/Naming/ValueObject/PropertyRename.php
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Validation\RectorAssert;
use Rector\Naming\Contract\RenamePropertyValueObjectInterface;

final class PropertyRename implements RenamePropertyValueObjectInterface
Expand All @@ -19,6 +20,9 @@ public function __construct(
private readonly string $classLikeName,
private readonly PropertyProperty $propertyProperty
) {
// name must be valid
RectorAssert::propertyName($currentName);
RectorAssert::propertyName($expectedName);
}

public function getProperty(): Property
Expand Down
2 changes: 2 additions & 0 deletions rules/Renaming/ValueObject/RenameProperty.php
Expand Up @@ -15,6 +15,8 @@ public function __construct(
private readonly string $newProperty
) {
RectorAssert::className($type);
RectorAssert::propertyName($oldProperty);
RectorAssert::propertyName($newProperty);
}

public function getObjectType(): ObjectType
Expand Down
Expand Up @@ -16,6 +16,7 @@ public function __construct(
private readonly ?string $methodIfNoArgs = null
) {
RectorAssert::className($class);
RectorAssert::functionName($function);
}

public function getFunction(): string
Expand Down
1 change: 1 addition & 0 deletions rules/Transform/ValueObject/ArrayFuncCallToMethodCall.php
Expand Up @@ -22,6 +22,7 @@ public function __construct(
private readonly string $nonArrayMethod
) {
RectorAssert::className($class);
RectorAssert::functionName($function);
}

public function getFunction(): string
Expand Down
2 changes: 2 additions & 0 deletions rules/Transform/ValueObject/DimFetchAssignToMethodCall.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;

final class DimFetchAssignToMethodCall
{
Expand All @@ -13,6 +14,7 @@ public function __construct(
private readonly string $itemClass,
private readonly string $addMethod
) {
RectorAssert::methodName($addMethod);
}

public function getListObjectType(): ObjectType
Expand Down
5 changes: 5 additions & 0 deletions rules/Transform/ValueObject/FuncCallToMethodCall.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;

final class FuncCallToMethodCall
{
Expand All @@ -13,6 +14,10 @@ public function __construct(
private readonly string $newClassName,
private readonly string $newMethodName
) {
RectorAssert::functionName($oldFuncName);

RectorAssert::className($newClassName);
RectorAssert::methodName($newMethodName);
}

public function getOldFuncName(): string
Expand Down
6 changes: 6 additions & 0 deletions rules/Transform/ValueObject/FuncCallToStaticCall.php
Expand Up @@ -4,13 +4,19 @@

namespace Rector\Transform\ValueObject;

use Rector\Core\Validation\RectorAssert;

final class FuncCallToStaticCall
{
public function __construct(
private readonly string $oldFuncName,
private readonly string $newClassName,
private readonly string $newMethodName
) {
RectorAssert::functionName($oldFuncName);

RectorAssert::className($newClassName);
RectorAssert::methodName($newMethodName);
}

public function getOldFuncName(): string
Expand Down
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/GetAndSetToMethodCall.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;

final class GetAndSetToMethodCall
{
Expand All @@ -16,6 +17,8 @@ public function __construct(
private readonly string $getMethod,
private readonly string $setMethod
) {
RectorAssert::methodName($getMethod);
RectorAssert::methodName($setMethod);
}

public function getGetMethod(): string
Expand Down
Expand Up @@ -19,6 +19,8 @@ public function __construct(
private readonly array $newArguments
) {
RectorAssert::className($type);
RectorAssert::methodName($oldMethod);
RectorAssert::methodName($newMethod);
}

public function getObjectType(): ObjectType
Expand Down
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/MethodCallToMethodCall.php
Expand Up @@ -19,7 +19,10 @@ public function __construct(
private readonly string $newMethod,
) {
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);

RectorAssert::className($newType);
RectorAssert::methodName($newMethod);
}

public function getOldType(): string
Expand Down
2 changes: 2 additions & 0 deletions rules/Transform/ValueObject/MethodCallToPropertyFetch.php
Expand Up @@ -15,6 +15,8 @@ public function __construct(
private readonly string $newProperty,
) {
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);
RectorAssert::propertyName($newProperty);
}

public function getOldObjectType(): ObjectType
Expand Down
6 changes: 6 additions & 0 deletions rules/Transform/ValueObject/MethodCallToStaticCall.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;

final class MethodCallToStaticCall
{
Expand All @@ -14,6 +15,11 @@ public function __construct(
private readonly string $newClass,
private readonly string $newMethod
) {
RectorAssert::className($oldClass);
RectorAssert::className($oldMethod);

RectorAssert::className($newClass);
RectorAssert::className($newMethod);
}

public function getOldObjectType(): ObjectType
Expand Down
6 changes: 2 additions & 4 deletions rules/Transform/ValueObject/NewArgToMethodCall.php
Expand Up @@ -9,15 +9,13 @@

final class NewArgToMethodCall
{
/**
* @param mixed $value
*/
public function __construct(
private readonly string $type,
private $value,
private readonly mixed $value,
private readonly string $methodCall
) {
RectorAssert::className($type);
RectorAssert::className($methodCall);
}

public function getObjectType(): ObjectType
Expand Down
4 changes: 4 additions & 0 deletions rules/Transform/ValueObject/NewToMethodCall.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;

final class NewToMethodCall
{
Expand All @@ -13,6 +14,9 @@ public function __construct(
private readonly string $serviceType,
private readonly string $serviceMethod
) {
RectorAssert::className($newType);
RectorAssert::className($serviceType);
RectorAssert::methodName($serviceMethod);
}

public function getNewObjectType(): ObjectType
Expand Down
2 changes: 2 additions & 0 deletions rules/Transform/ValueObject/NewToStaticCall.php
Expand Up @@ -15,7 +15,9 @@ public function __construct(
private readonly string $staticCallMethod
) {
RectorAssert::className($type);

RectorAssert::className($staticCallClass);
RectorAssert::methodName($staticCallMethod);
}

public function getObjectType(): ObjectType
Expand Down
5 changes: 5 additions & 0 deletions rules/Transform/ValueObject/ParentClassToTraits.php
Expand Up @@ -4,6 +4,9 @@

namespace Rector\Transform\ValueObject;

use Rector\Core\Validation\RectorAssert;
use Webmozart\Assert\Assert;

final class ParentClassToTraits
{
/**
Expand All @@ -13,6 +16,8 @@ public function __construct(
private readonly string $parentType,
private readonly array $traitNames
) {
RectorAssert::className($parentType);
Assert::allString($traitNames);
}

public function getParentType(): string
Expand Down
4 changes: 4 additions & 0 deletions rules/Transform/ValueObject/PropertyAndClassMethodName.php
Expand Up @@ -4,12 +4,16 @@

namespace Rector\Transform\ValueObject;

use Rector\Core\Validation\RectorAssert;

final class PropertyAndClassMethodName
{
public function __construct(
private readonly string $propertyName,
private readonly string $classMethodName
) {
RectorAssert::propertyName($propertyName);
RectorAssert::methodName($classMethodName);
}

public function getPropertyName(): string
Expand Down
2 changes: 2 additions & 0 deletions rules/Transform/ValueObject/PropertyAssignToMethodCall.php
Expand Up @@ -15,6 +15,8 @@ public function __construct(
private readonly string $newMethodName
) {
RectorAssert::className($class);
RectorAssert::propertyName($oldPropertyName);
RectorAssert::methodName($newMethodName);
}

public function getObjectType(): ObjectType
Expand Down
6 changes: 6 additions & 0 deletions rules/Transform/ValueObject/PropertyFetchToMethodCall.php
Expand Up @@ -20,6 +20,12 @@ public function __construct(
private readonly array $newGetArguments = []
) {
RectorAssert::className($oldType);
RectorAssert::propertyName($oldProperty);

RectorAssert::methodName($newGetMethod);
if (is_string($newSetMethod)) {
RectorAssert::methodName($newSetMethod);
}
}

public function getOldObjectType(): ObjectType
Expand Down
Expand Up @@ -15,6 +15,8 @@ public function __construct(
private readonly string $property
) {
RectorAssert::className($class);
RectorAssert::methodName($method);
RectorAssert::propertyName($property);
}

public function getObjectType(): ObjectType
Expand Down
Expand Up @@ -15,6 +15,8 @@ public function __construct(
private readonly string $serviceType
) {
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);

RectorAssert::className($serviceType);
}

Expand Down
4 changes: 4 additions & 0 deletions rules/Transform/ValueObject/StaticCallRecipe.php
Expand Up @@ -4,12 +4,16 @@

namespace Rector\Transform\ValueObject;

use Rector\Core\Validation\RectorAssert;

final class StaticCallRecipe
{
public function __construct(
private readonly string $className,
private readonly string $methodName,
) {
RectorAssert::className($className);
RectorAssert::methodName($methodName);
}

public function getClassName(): string
Expand Down
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/StaticCallToFuncCall.php
Expand Up @@ -15,6 +15,9 @@ public function __construct(
private readonly string $function
) {
RectorAssert::className($class);
RectorAssert::methodName($method);

RectorAssert::functionName($function);
}

public function getObjectType(): ObjectType
Expand Down

0 comments on commit 6577da6

Please sign in to comment.