Skip to content

Commit 00abb0a

Browse files
committed
[code-quality] Add AssertArrayCastedObjectToAssertSameRector
1 parent 04809d0 commit 00abb0a

File tree

10 files changed

+296
-7
lines changed

10 files changed

+296
-7
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
"php": ">=8.2"
88
},
99
"require-dev": {
10-
"phpecs/phpecs": "^2.1.1",
10+
"phpecs/phpecs": "^2.2",
1111
"phpstan/extension-installer": "^1.4",
12-
"phpstan/phpstan": "^2.1.8",
12+
"phpstan/phpstan": "^2.1.31",
1313
"phpstan/phpstan-deprecation-rules": "^2.0",
1414
"phpstan/phpstan-webmozart-assert": "^2.0",
1515
"phpunit/phpunit": "^11.5",
1616
"rector/rector-src": "dev-main",
1717
"rector/swiss-knife": "^1.0",
18-
"rector/type-perfect": "^2.0",
18+
"rector/type-perfect": "^2.1",
1919
"symplify/phpstan-extensions": "^12.0",
20-
"symplify/vendor-patches": "^11.4",
20+
"symplify/vendor-patches": "^11.5",
2121
"tomasvotruba/class-leak": "^1.2",
2222
"tracy/tracy": "^2.10"
2323
},

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
1818
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
1919
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
20+
use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
2021
use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector;
2122
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector;
2223
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector;
@@ -97,6 +98,7 @@
9798

9899
// avoid call on nullable object
99100
AddInstanceofAssertForNullableInstanceRector::class,
101+
AssertArrayCastedObjectToAssertSameRector::class,
100102

101103
/**
102104
* Improve direct testing of your code, without mock creep. Make it simple, clear and easy to maintain:

phpstan.neon

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ parameters:
3838
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
3939
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'
4040

41-
-
42-
identifier: instanceof.alwaysTrue
41+
- identifier: instanceof.alwaysTrue
42+
- identifier: assign.propertyType
43+
44+
# not relevant
45+
- '#Method (.*?)provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#'
4346

47+
# chicken-egg false positive
4448
-
45-
identifier: assign.propertyType
49+
message: '#Parameter \#1 \$value of static method Webmozart\\Assert\\Assert\:\:isAOf\(\) expects object\|string, PhpParser\\Node\\Stmt\\Class_\|null given#'
50+
path: rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AssertArrayCastedObjectToAssertSameRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Source\SomeObjectWithPublicProperties;
6+
7+
final class SomeTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public function test()
10+
{
11+
$someObject = new SomeObjectWithPublicProperties();
12+
13+
$this->assertSame([
14+
'name' => 'John',
15+
'surname' => 'Doe',
16+
], (array) $someObject);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Fixture;
25+
26+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Source\SomeObjectWithPublicProperties;
27+
28+
final class SomeTest extends \PHPUnit\Framework\TestCase
29+
{
30+
public function test()
31+
{
32+
$someObject = new SomeObjectWithPublicProperties();
33+
$this->assertSame('John', $someObject->name);
34+
$this->assertSame('Doe', $someObject->surname);
35+
}
36+
}
37+
38+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Source\SomeObjectWithPublicProperties;
6+
7+
final class SkipCastOnNonObject extends \PHPUnit\Framework\TestCase
8+
{
9+
public function test()
10+
{
11+
$someObject = 'some string';
12+
13+
$this->assertSame([
14+
'name' => 'John',
15+
'surname' => 'Doe',
16+
], (array) $someObject);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\Source;
4+
5+
final class SomeObjectWithPublicProperties
6+
{
7+
public $name;
8+
9+
public $surname;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AssertArrayCastedObjectToAssertSameRector::class);
10+
};

rules-tests/CodeQuality/Rector/Foreach_/SimplifyForeachInstanceOfRector/Fixture/fixture.php.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector\Fixture;
4+
35
final class MyOtherTest extends \PHPUnit\Framework\TestCase
46
{
57
public function test()
@@ -27,6 +29,8 @@ final class MyOtherTest extends \PHPUnit\Framework\TestCase
2729
-----
2830
<?php
2931

32+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector\Fixture;
33+
3034
final class MyOtherTest extends \PHPUnit\Framework\TestCase
3135
{
3236
public function test()
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\Expression;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Cast\Array_;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\PropertyFetch;
11+
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Identifier;
13+
use PhpParser\Node\Stmt\Expression;
14+
use PHPStan\Type\Constant\ConstantArrayType;
15+
use PHPStan\Type\ObjectType;
16+
use Rector\PhpParser\Node\Value\ValueResolver;
17+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
18+
use Rector\Rector\AbstractRector;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
/**
23+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector\AssertArrayCastedObjectToAssertSameRectorTest
24+
*/
25+
final class AssertArrayCastedObjectToAssertSameRector extends AbstractRector
26+
{
27+
public function __construct(
28+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
29+
private readonly ValueResolver $valueResolver,
30+
) {
31+
}
32+
33+
public function getRuleDefinition(): RuleDefinition
34+
{
35+
return new RuleDefinition(
36+
'Split object casted to array to assert public properties to standalone assert calls',
37+
[
38+
new CodeSample(
39+
<<<'CODE_SAMPLE'
40+
use PHPUnit\Framework\TestCase;
41+
42+
final class SomeTest extends TestClass
43+
{
44+
public function test()
45+
{
46+
$someObject = new SomeObject();
47+
48+
$this->assertSame([
49+
'name' => 'John',
50+
'surname' => 'Doe',
51+
], (array) $someObject));
52+
}
53+
}
54+
CODE_SAMPLE
55+
,
56+
<<<'CODE_SAMPLE'
57+
use PHPUnit\Framework\TestCase;
58+
59+
final class SomeTest extends TestClass
60+
{
61+
public function test()
62+
{
63+
$someObject = new SomeObject();
64+
65+
$this->assertSame('John', $someObject->name);
66+
$this->assertSame('Doe', $someObject->surname);
67+
}
68+
}
69+
CODE_SAMPLE
70+
),
71+
]
72+
);
73+
}
74+
75+
/**
76+
* @return array<class-string<Node>>
77+
*/
78+
public function getNodeTypes(): array
79+
{
80+
return [Expression::class];
81+
}
82+
83+
/**
84+
* @param Expression $node
85+
* @return Expression[]|null
86+
*/
87+
public function refactor(Node $node): ?array
88+
{
89+
if (! $node->expr instanceof MethodCall) {
90+
return null;
91+
}
92+
93+
$methodCall = $node->expr;
94+
if ($methodCall->isFirstClassCallable()) {
95+
return null;
96+
}
97+
98+
if (! $this->isName($methodCall->name, 'assertSame')) {
99+
return null;
100+
}
101+
102+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
103+
return null;
104+
}
105+
106+
$args = $methodCall->getArgs();
107+
$firstArg = $args[0];
108+
if (! $firstArg->value instanceof Node\Expr\Array_) {
109+
return null;
110+
}
111+
112+
// we need assert to non empty arary
113+
$array = $firstArg->value;
114+
if ($array->items === []) {
115+
return null;
116+
}
117+
118+
$arrayType = $this->getType($array);
119+
if (! $arrayType instanceof ConstantArrayType) {
120+
return null;
121+
}
122+
123+
$secondArg = $args[1];
124+
125+
if (! $secondArg->value instanceof Array_) {
126+
return null;
127+
}
128+
129+
$castArray = $secondArg->value;
130+
131+
$castedExpr = $castArray->expr;
132+
133+
$castedExprType = $this->getType($castedExpr);
134+
if (! $castedExprType instanceof ObjectType) {
135+
return null;
136+
}
137+
138+
$assertedArrayValues = $this->valueResolver->getValue($array);
139+
if (! $this->hasAllKeysString($assertedArrayValues)) {
140+
return null;
141+
}
142+
143+
$standaloneExpressions = [];
144+
145+
foreach ($assertedArrayValues as $propertyName => $expectedValue) {
146+
$args = $this->nodeFactory->createArgs([
147+
$expectedValue,
148+
new PropertyFetch($castedExpr, new Identifier($propertyName)),
149+
]);
150+
151+
$assertSameMethodCall = new MethodCall(new Variable('this'), 'assertSame', $args);
152+
153+
$standaloneExpressions[] = new Expression($assertSameMethodCall);
154+
}
155+
156+
return $standaloneExpressions;
157+
}
158+
159+
/**
160+
* @param array<mixed, mixed> $assertedArrayValues
161+
*/
162+
private function hasAllKeysString(array $assertedArrayValues): bool
163+
{
164+
// all keys must be string
165+
foreach (array_keys($assertedArrayValues) as $key) {
166+
if (! is_string($key)) {
167+
return false;
168+
}
169+
}
170+
171+
return true;
172+
}
173+
}

0 commit comments

Comments
 (0)