Skip to content

Commit 58c8f17

Browse files
authored
[CodingStyle] Add DataProviderArrayItemsNewlinedRector (rectorphp#3271)
1 parent e8dd953 commit 58c8f17

File tree

16 files changed

+281
-26
lines changed

16 files changed

+281
-26
lines changed

packages/ChangesReporting/Output/ConsoleOutputFormatter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private function reportRemovedFilesAndNodes(ProcessResult $processResult): void
143143
private function normalizePathsToRelativeWithLine(string $errorMessage): string
144144
{
145145
$regex = '#' . preg_quote(getcwd(), '#') . '/#';
146-
$errorMessage = Strings::replace($errorMessage, $regex, '');
147-
return Strings::replace($errorMessage, self::ON_LINE_REGEX, ':');
146+
$errorMessage = Strings::replace($errorMessage, $regex);
147+
return Strings::replace($errorMessage, self::ON_LINE_REGEX);
148148
}
149149

150150
private function reportRemovedNodes(ProcessResult $processResult): void

packages/NodeTypeResolver/Node/AttributeKey.php

+6
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,10 @@ final class AttributeKey
186186
* @var string
187187
*/
188188
public const DOC_LABEL = 'docLabel';
189+
190+
/**
191+
* Prints array in newlined fastion, one item per line
192+
* @var string
193+
*/
194+
public const NEWLINED_ARRAY_PRINT = 'newlined_array_print';
189195
}

phpstan.neon

+3
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,6 @@ parameters:
877877
-
878878
message: '#"Tests" namespace cannot be used outside of "tests" directory#'
879879
path: utils-tests
880+
881+
# trusted items
882+
- '#Parameter \#1 \$nodes of method PhpParser\\PrettyPrinterAbstract\:\:pCommaSeparatedMultiline\(\) expects array<PhpParser\\Node>, array<PhpParser\\Node\\Expr\\ArrayItem\|null> given#'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector;
6+
7+
use Iterator;
8+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
9+
10+
final class DataProviderArrayItemsNewlinedRectorTest extends AbstractRectorTestCase
11+
{
12+
/**
13+
* @dataProvider provideData()
14+
*/
15+
public function test(string $filePath): void
16+
{
17+
$this->doTestFile($filePath);
18+
}
19+
20+
/**
21+
* @return Iterator<string>
22+
*/
23+
public function provideData(): Iterator
24+
{
25+
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
26+
}
27+
28+
public function provideConfigFilePath(): string
29+
{
30+
return __DIR__ . '/config/configured_rule.php';
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipNoArray extends TestCase
8+
{
9+
public function provideData(): array
10+
{
11+
return 'content' . 8;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
4+
5+
final class SkipNonPhpUnit
6+
{
7+
public function provideData(): array
8+
{
9+
return [['content', 8], ['content123', 11]];
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ImageBinaryTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider provideData()
11+
*/
12+
public function testGetBytesSize(string $content, int $number): void
13+
{
14+
// ...
15+
}
16+
17+
public function provideData(): array
18+
{
19+
return [['content', 8], ['content123', 11]];
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
28+
29+
use PHPUnit\Framework\TestCase;
30+
31+
final class ImageBinaryTest extends TestCase
32+
{
33+
/**
34+
* @dataProvider provideData()
35+
*/
36+
public function testGetBytesSize(string $content, int $number): void
37+
{
38+
// ...
39+
}
40+
41+
public function provideData(): array
42+
{
43+
return [
44+
['content', 8],
45+
['content123', 11],
46+
];
47+
}
48+
}
49+
50+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DataProviderArrayItemsNewlinedRector::class);
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodingStyle\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Array_;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Return_;
11+
use Rector\Core\Rector\AbstractRector;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
13+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\DataProviderArrayItemsNewlinedRectorTest
19+
*/
20+
final class DataProviderArrayItemsNewlinedRector extends AbstractRector
21+
{
22+
public function __construct(
23+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
24+
) {
25+
}
26+
27+
public function getRuleDefinition(): RuleDefinition
28+
{
29+
return new RuleDefinition('Change data provider in PHPUnit test case to newline per item', [
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
use PHPUnit\Framework\TestCase;
33+
34+
final class ImageBinaryTest extends TestCase
35+
{
36+
/**
37+
* @dataProvider provideData()
38+
*/
39+
public function testGetBytesSize(string $content, int $number): void
40+
{
41+
// ...
42+
}
43+
44+
public function provideData(): array
45+
{
46+
return [['content', 8], ['content123', 11]];
47+
}
48+
}
49+
CODE_SAMPLE
50+
51+
,
52+
<<<'CODE_SAMPLE'
53+
use PHPUnit\Framework\TestCase;
54+
55+
final class ImageBinaryTest extends TestCase
56+
{
57+
/**
58+
* @dataProvider provideData()
59+
*/
60+
public function testGetBytesSize(string $content, int $number): void
61+
{
62+
// ...
63+
}
64+
65+
public function provideData(): array
66+
{
67+
return [
68+
['content', 8],
69+
['content123', 11]
70+
];
71+
}
72+
}
73+
CODE_SAMPLE
74+
),
75+
]);
76+
}
77+
78+
/**
79+
* @return array<class-string<Node>>
80+
*/
81+
public function getNodeTypes(): array
82+
{
83+
return [ClassMethod::class];
84+
}
85+
86+
/**
87+
* @param ClassMethod $node
88+
*/
89+
public function refactor(Node $node): ?Node
90+
{
91+
if (! $node->isPublic()) {
92+
return null;
93+
}
94+
95+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
96+
return null;
97+
}
98+
99+
// skip test methods
100+
if ($this->isName($node, 'test*')) {
101+
return null;
102+
}
103+
104+
// find array in data provider - must contain a return node
105+
106+
/** @var Return_[] $returns */
107+
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class);
108+
109+
$hasChanged = false;
110+
111+
foreach ($returns as $return) {
112+
if (! $return->expr instanceof Array_) {
113+
continue;
114+
}
115+
116+
$array = $return->expr;
117+
if ($array->items === []) {
118+
continue;
119+
}
120+
121+
// ensure newlined printed
122+
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, true);
123+
124+
// invoke reprint
125+
$array->setAttribute(AttributeKey::ORIGINAL_NODE, null);
126+
127+
$hasChanged = true;
128+
}
129+
130+
if ($hasChanged) {
131+
return $node;
132+
}
133+
134+
return null;
135+
}
136+
}

rules/Naming/Naming/PropertyNaming.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,6 @@ public function fqnToVariableName(ThisType | ObjectType | string $objectType): s
123123
return $this->prolongIfTooShort($variableName, $className);
124124
}
125125

126-
/**
127-
* @api symfony
128-
* @see https://stackoverflow.com/a/2792045/1348344
129-
*/
130-
public function underscoreToName(string $underscoreName): string
131-
{
132-
$uppercaseWords = ucwords($underscoreName, '_');
133-
$pascalCaseName = str_replace('_', '', $uppercaseWords);
134-
135-
return lcfirst($pascalCaseName);
136-
}
137-
138126
private function resolveShortClassName(string $className): string
139127
{
140128
if (\str_contains($className, '\\')) {
@@ -273,7 +261,7 @@ private function normalizeShortClassName(string $shortClassName): string
273261
}
274262

275263
// remove "_"
276-
$shortClassName = Strings::replace($shortClassName, '#_#', '');
264+
$shortClassName = Strings::replace($shortClassName, '#_#');
277265
return $this->normalizeUpperCase($shortClassName);
278266
}
279267

rules/PSR4/FileInfoAnalyzer/FileInfoDeletionAnalyzer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public function isClassLikeAndFileInfoMatch(File $file, ClassLike $classLike): b
3939

4040
private function clearNameFromTestingPrefix(string $name): string
4141
{
42-
return Strings::replace($name, self::TESTING_PREFIX_REGEX, '');
42+
return Strings::replace($name, self::TESTING_PREFIX_REGEX);
4343
}
4444
}

rules/Php55/RegexMatcher.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function resolveModifiers(string $modifiersCandidate): string
8787

8888
private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers): string
8989
{
90-
$modifiersWithoutE = Strings::replace($modifiers, '#e#', '');
90+
$modifiersWithoutE = Strings::replace($modifiers, '#e#');
9191

9292
return Strings::before($pattern, $delimiter, -1) . $delimiter . $modifiersWithoutE;
9393
}

rules/Php73/Rector/String_/SensitiveHereNowDocRector.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ final class SensitiveHereNowDocRector extends AbstractRector implements MinPhpVe
2424
*/
2525
private const WRAP_SUFFIX = '_WRAP';
2626

27-
/**
28-
* @var string
29-
*/
30-
private const ATTRIBUTE_DOC_LABEL = 'docLabel';
31-
3227
public function provideMinPhpVersion(): int
3328
{
3429
return PhpVersionFeature::SENSITIVE_HERE_NOW_DOC;
@@ -76,13 +71,13 @@ public function refactor(Node $node): ?Node
7671

7772
// the doc label is not in the string → ok
7873
/** @var string $docLabel */
79-
$docLabel = $node->getAttribute(self::ATTRIBUTE_DOC_LABEL);
74+
$docLabel = $node->getAttribute(AttributeKey::DOC_LABEL);
8075

8176
if (! \str_contains($node->value, $docLabel)) {
8277
return null;
8378
}
8479

85-
$node->setAttribute(self::ATTRIBUTE_DOC_LABEL, $this->uniquateDocLabel($node->value, $docLabel));
80+
$node->setAttribute(AttributeKey::DOC_LABEL, $this->uniquateDocLabel($node->value, $docLabel));
8681

8782
// invoke redraw
8883
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ private function shouldSkipPropertyType(Type $propertyType): bool
182182
if ($propertyType instanceof MixedType) {
183183
return true;
184184
}
185+
185186
return $this->isDoctrineCollectionType($propertyType);
186187
}
187188
}

src/PhpParser/Printer/BetterStandardPrinter.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ protected function pArray(
234234
return $content;
235235
}
236236

237-
return Strings::replace($content, self::EXTRA_SPACE_BEFORE_NOP_REGEX, '');
237+
return Strings::replace($content, self::EXTRA_SPACE_BEFORE_NOP_REGEX);
238238
}
239239

240240
/**
@@ -313,6 +313,13 @@ protected function pExpr_Array(Array_ $array): string
313313
$array->setAttribute(AttributeKey::KIND, Array_::KIND_SHORT);
314314
}
315315

316+
if ($array->getAttribute(AttributeKey::NEWLINED_ARRAY_PRINT) === true) {
317+
$printedArray = '[';
318+
$printedArray .= $this->pCommaSeparatedMultiline($array->items, true);
319+
320+
return $printedArray . ($this->nl . ']');
321+
}
322+
316323
return parent::pExpr_Array($array);
317324
}
318325

@@ -372,7 +379,7 @@ protected function pStmt_Declare(Declare_ $declare): string
372379
{
373380
$declareString = parent::pStmt_Declare($declare);
374381

375-
return Strings::replace($declareString, '#\s+#', '');
382+
return Strings::replace($declareString, '#\s+#');
376383
}
377384

378385
protected function pExpr_Ternary(Ternary $ternary): string

0 commit comments

Comments
 (0)