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
9 changes: 3 additions & 6 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@
);

// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_filter_default_constant
$rectorConfig->ruleWithConfiguration(
RenameConstantRector::class,
[
'FILTER_DEFAULT' => 'FILTER_UNSAFE_RAW',
]
);
$rectorConfig->ruleWithConfiguration(RenameConstantRector::class, [
'FILTER_DEFAULT' => 'FILTER_UNSAFE_RAW',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class BeberleiTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureBeberlei');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/beberlei_assert.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

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\FixtureBeberlei;

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

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(AddAssertArrayFromClassMethodDocblockRector::class, [
AssertClassName::BEBERLEI,
]);
};
7 changes: 6 additions & 1 deletion rules/Assert/Enum/AssertClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ final class AssertClassName
/**
* @var string
*/
public const ASSERT = 'Webmozart\Assert\Assert';
public const WEBMOZART = 'Webmozart\Assert\Assert';

/**
* @var string
*/
public const BEBERLEI = 'Assert\Assertion';
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function resolve(ClassMethod $classMethod): array
continue;
}

if ($staticCall->class->toString() !== AssertClassName::ASSERT) {
if (! in_array(
$staticCall->class->toString(),
[AssertClassName::WEBMOZART, AssertClassName::BEBERLEI],
true
)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
use Rector\Assert\NodeAnalyzer\ExistingAssertStaticCallResolver;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @experimental Check generic array key/value types in runtime with assert. Generics for impatient people.
*
* @see \Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\AddAssertArrayFromClassMethodDocblockRectorTest
*/
final class AddAssertArrayFromClassMethodDocblockRector extends AbstractRector
final class AddAssertArrayFromClassMethodDocblockRector extends AbstractRector implements ConfigurableRectorInterface
{
private string $assertClass = AssertClassName::WEBMOZART;

public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ExistingAssertStaticCallResolver $existingAssertStaticCallResolver
Expand All @@ -46,9 +50,11 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add key and value assert based on docblock @param type declarations', [
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'Add key and value assert based on docblock @param type declarations (pick from "webmozart" or "beberlei" asserts)',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
<?php

class SomeClass
Expand All @@ -62,11 +68,13 @@ public function run(array $items)
}

CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
<?php

use Webmozart\Assert\Assert;class SomeClass
use Webmozart\Assert\Assert;

class SomeClass
{
/**
* @param int[] $items
Expand All @@ -77,8 +85,12 @@ public function run(array $items)
}
}
CODE_SAMPLE
),
]);
,
[AssertClassName::WEBMOZART]
),

]
);
}

public function getNodeTypes(): array
Expand Down Expand Up @@ -118,7 +130,7 @@ public function refactor(Node $node): ?ClassMethod
}

// handle arrays only
if ($param->type->name !== 'array') {
if (! $this->isName($param->type, 'array')) {
continue;
}

Expand Down Expand Up @@ -168,9 +180,27 @@ public function refactor(Node $node): ?ClassMethod
return $node;
}

/**
* @param array<string> $configuration
*/
public function configure(array $configuration): void
{
if ($configuration === []) {
// default
return;
}

Assert::count($configuration, 1);
Assert::inArray($configuration[0], [AssertClassName::BEBERLEI, AssertClassName::WEBMOZART]);

$this->assertClass = $configuration[0];
}

private function createAssertExpression(Expr $expr, string $methodName): Expression
{
$staticCall = new StaticCall(new FullyQualified(AssertClassName::ASSERT), $methodName, [new Arg($expr)]);
$assertFullyQualified = new FullyQualified($this->assertClass);

$staticCall = new StaticCall($assertFullyQualified, $methodName, [new Arg($expr)]);

return new Expression($staticCall);
}
Expand Down
Loading