Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/set/assert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddAssertArrayFromClassMethodDocblockRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;

final class FloatIntBool
{
/**
* @param int[] $items
* @param float[] $numbers
* @param bool[] $options
*/
public function run(array $items, array $numbers, array $options)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;

final class FloatIntBool
{
/**
* @param int[] $items
* @param float[] $numbers
* @param bool[] $options
*/
public function run(array $items, array $numbers, array $options)
{
\Webmozart\Assert\Assert::allInteger($items);
\Webmozart\Assert\Assert::allFloat($numbers);
\Webmozart\Assert\Assert::allBoolean($options);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

namespace Rector\Assert\Rector\ClassMethod;

use PhpParser\Node\Expr\Variable;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Assert\Enum\AssertClassName;
use Rector\Assert\NodeAnalyzer\ExistingAssertStaticCallResolver;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -133,19 +136,17 @@ public function refactor(Node $node): ?ClassMethod
continue;
}

// assert value
if ($paramDocType->getItemType() instanceof IntegerType) {
$assertStaticCallStmts[] = $this->createAssertExpression($param->var, 'allInteger');
} elseif ($paramDocType->getItemType() instanceof StringType) {
$assertStaticCallStmts[] = $this->createAssertExpression($param->var, 'allString');
$valueAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getItemType());

if (is_string($valueAssertMethod)) {
$assertStaticCallStmts[] = $this->createAssertExpression($param->var, $valueAssertMethod);
}

// assert keys
$arrayKeys = new FuncCall(new Name('array_keys'), [new Arg($param->var)]);
if ($paramDocType->getKeyType() instanceof StringType) {
$assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, 'allString');
} elseif ($paramDocType->getKeyType() instanceof IntegerType) {
$assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, 'allInteger');
$keyAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getKeyType());

if (is_string($keyAssertMethod)) {
$arrayKeys = new FuncCall(new Name('array_keys'), [new Arg($param->var)]);
$assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, $keyAssertMethod);
}
}

Expand Down Expand Up @@ -192,4 +193,25 @@ private function filterOutExistingStaticCall(array $assertStaticCallStmts, array
return ! in_array($currentStaticCallHash, $existingAssertCallHashes, true);
});
}

private function matchTypeToAssertMethod(Type $type): ?string
{
if ($type instanceof IntegerType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use $type->isInteger()->yes() so it works also with integer ranges ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to start with basic "strict" types first, test it in the wild and then we can iterate further.

return 'allInteger';
}

if ($type instanceof StringType) {
return 'allString';
}

if ($type instanceof FloatType) {
return 'allFloat';
}

if ($type instanceof BooleanType) {
return 'allBoolean';
}

return null;
}
}
7 changes: 7 additions & 0 deletions src/Set/ValueObject/SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,11 @@ final class SetList
* @var string
*/
public const BEHAT_ANNOTATIONS_TO_ATTRIBUTES = __DIR__ . '/../../../config/set/behat-annotations-to-attributes.php';

/**
* @experimental Check generic types in runtime with assert. Generics for impatient people.
*
* @var string
*/
public const ASSERT = __DIR__ . '/../../../config/set/assert.php';
}
Loading