Skip to content

Commit be61386

Browse files
committed
cleanup fixtures with no change
1 parent 972239e commit be61386

7 files changed

Lines changed: 76 additions & 155 deletions

File tree

bin/check-before-after-same-fixtures.php

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,87 @@
22

33
declare(strict_types=1);
44

5+
use Nette\Utils\Strings;
6+
57
require __DIR__ . '/../vendor/autoload.php';
68

79
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\ArgvInput;
11+
use Symfony\Component\Console\Output\ConsoleOutput;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
813
use Symfony\Component\Finder\Finder;
14+
use Symfony\Component\Finder\SplFileInfo;
15+
use Webmozart\Assert\Assert;
916

10-
$invalidFixturePaths = [];
17+
final class SameBeforeAfterFixtureDetector
18+
{
19+
private SymfonyStyle $symfonyStyle;
1120

12-
$symfonyStyle = new \Symfony\Component\Console\Style\SymfonyStyle(
13-
new \Symfony\Component\Console\Input\ArgvInput(),
14-
new \Symfony\Component\Console\Output\ConsoleOutput()
15-
);
21+
public function __construct()
22+
{
23+
$this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput());
24+
}
1625

17-
$testFixtureFiles = (new Finder())
18-
->files()
19-
->in([__DIR__ . '/../tests', __DIR__ . '/../rules-tests'])
20-
->name('*.php.inc')
21-
->getIterator();
26+
/**
27+
* @param string[] $testDirectories
28+
* @return Command::*
29+
*/
30+
public function run(array $testDirectories): int
31+
{
32+
$fixtureFiles = $this->findFixtureFiles($testDirectories);
2233

23-
foreach ($testFixtureFiles as $testFixtureFile) {
24-
if (! str_contains($testFixtureFile->getContents(), '-----')) {
25-
continue;
26-
}
34+
$invalidFixturePaths = [];
35+
foreach ($fixtureFiles as $fixtureFile) {
36+
if (! $this->hasFileSameBeforeAndAfterPart($fixtureFile)) {
37+
continue;
38+
}
39+
40+
$invalidFixturePaths[] = substr($fixtureFile->getRealPath(), strlen(getcwd()) + 1);
41+
}
42+
43+
if ($invalidFixturePaths === []) {
44+
$this->symfonyStyle->success('All fixtures are valid');
45+
return Command::SUCCESS;
46+
}
47+
48+
$this->symfonyStyle->error(
49+
'The following fixtures have the same before and after content. Remove the part after "-----" to fix them'
50+
);
51+
$this->symfonyStyle->listing($invalidFixturePaths);
2752

28-
$parts = preg_split('/^\s*-{5,}\s*$/m', file_get_contents($testFixtureFile->getRealPath()));
29-
if (count($parts) !== 2) {
30-
continue;
53+
return Command::FAILURE;
3154
}
3255

33-
if (trim($parts[0]) !== trim($parts[1])) {
34-
continue;
56+
/**
57+
* @return SplFileInfo[]
58+
*/
59+
private function findFixtureFiles(array $directories): array
60+
{
61+
Assert::allDirectory($directories);
62+
63+
$finder = (new Finder())
64+
->files()
65+
->in($directories)
66+
->name('*.php.inc')
67+
->sortByName();
68+
69+
return iterator_to_array($finder->getIterator());
3570
}
3671

37-
$invalidFixturePaths[] = $testFixtureFile->getRealPath();
38-
}
72+
private function hasFileSameBeforeAndAfterPart(SplFileInfo $fixtureFile): bool
73+
{
74+
$parts = Strings::split($fixtureFile->getContents(), '#^\s*-----\s*$#m');
75+
if (count($parts) !== 2) {
76+
return false;
77+
}
3978

40-
if ($invalidFixturePaths === []) {
41-
$symfonyStyle->success('All fixtures are valid');
42-
exit(Command::SUCCESS);
79+
return trim((string) $parts[0]) === trim((string) $parts[1]);
80+
}
4381
}
4482

45-
$symfonyStyle->error('The following fixtures have the same before and after content. Remove the part after "-----" to fix them');
46-
47-
$symfonyStyle->listing($invalidFixturePaths);
4883

49-
exit(Command::FAILURE);
84+
$sameBeforeAfterFixtureDetector = new SameBeforeAfterFixtureDetector();
85+
exit($sameBeforeAfterFixtureDetector->run([
86+
__DIR__ . '/../tests',
87+
__DIR__ . '/../rules-tests',
88+
]));

rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/static_property.php.inc

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,3 @@ class StaticProperty
1212
return $this::$privateProperty;
1313
}
1414
}
15-
16-
?>
17-
-----
18-
<?php
19-
20-
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;
21-
22-
class StaticProperty
23-
{
24-
public static $publicProperty;
25-
protected static $protectedProperty;
26-
27-
public function foo()
28-
{
29-
return $this::$privateProperty;
30-
}
31-
}
32-
33-
?>

rules-tests/Php80/Rector/ClassMethod/FinalPrivateToPrivateVisibilityRector/Fixture/final_private_constructor.php.inc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,3 @@ abstract class FinalPrivate
88
{
99
}
1010
}
11-
12-
?>
13-
-----
14-
<?php
15-
16-
namespace Rector\Tests\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector\Fixture;
17-
18-
abstract class FinalPrivate
19-
{
20-
final private function __construct()
21-
{
22-
}
23-
}
24-
25-
?>

rules-tests/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector/Fixture/skip_different_cases.php.inc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,3 @@ function bar($otherObject): void
2121
$finfo->buffer($fileContents, FILEINFO_NONE, context: $context);
2222
$finfo->buffer($fileContents, flags: FILEINFO_NONE, context: $context);
2323
}
24-
25-
?>
26-
-----
27-
<?php
28-
29-
declare(strict_types=1);
30-
31-
namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\Fixture;
32-
33-
finfo_file($finfo, $fileContents, FILEINFO_NONE, $context);
34-
finfo_file($finfo, $fileContents, FILEINFO_NONE, context: $context);
35-
finfo_file($finfo, $fileContents, flags: FILEINFO_NONE, context: $context);
36-
37-
function foo(\finfo $finfo): void
38-
{
39-
$finfo->file($fileContents, FILEINFO_NONE, $context);
40-
$finfo->file($fileContents, FILEINFO_NONE, context: $context);
41-
$finfo->file($fileContents, flags: FILEINFO_NONE, context: $context);
42-
}
43-
44-
function bar($otherObject): void
45-
{
46-
$finfo->buffer($fileContents, FILEINFO_NONE, $context);
47-
$finfo->buffer($fileContents, FILEINFO_NONE, context: $context);
48-
$finfo->buffer($fileContents, flags: FILEINFO_NONE, context: $context);
49-
}
50-
51-
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureUnion/array_reverse_index.php.inc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,3 @@ final class ArrayReverseIndex
2121
return sprintf('%d-%s', $index, $uri);
2222
}
2323
}
24-
25-
?>
26-
-----
27-
<?php
28-
29-
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\FixtureUnion;
30-
31-
final class ArrayReverseIndex
32-
{
33-
public function run()
34-
{
35-
$parts = array_reverse(explode('/', '/some/test/url'));
36-
foreach ($parts as $index => $uri) {
37-
if ($index === 0) {
38-
continue;
39-
}
40-
41-
$this->someOtherMethod($index, $uri);
42-
}
43-
}
44-
45-
private function someOtherMethod(int $index, string $uri)
46-
{
47-
return sprintf('%d-%s', $index, $uri);
48-
}
49-
}
50-
51-
?>

rules-tests/TypeDeclaration/Rector/Closure/AddClosureVoidReturnTypeWhereNoReturnRector/Fixture/return_inside_inner_function.php.inc

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,3 @@ final class ReturnInsideInnerFunction
1616
};
1717
}
1818
}
19-
20-
?>
21-
-----
22-
<?php
23-
24-
namespace Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector\Fixture;
25-
26-
final class ReturnInsideInnerFunction
27-
{
28-
public function getValues()
29-
{
30-
$result = function () {
31-
$value = 1000;
32-
if ($value) {
33-
return;
34-
}
35-
36-
return 10;
37-
};
38-
}
39-
}
40-
41-
?>

rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9-
use PhpParser\Node\Expr;
109
use PhpParser\Node\Expr\CallLike;
1110
use PhpParser\Node\Expr\FuncCall;
1211
use PhpParser\Node\Expr\MethodCall;
@@ -55,14 +54,10 @@ public function getNodeTypes(): array
5554
}
5655

5756
/**
58-
* @param FuncCall $node
57+
* @param MethodCall|FuncCall $node
5958
*/
6059
public function refactor(Node $node): ?Node
6160
{
62-
if ($node->name instanceof Expr) {
63-
return null;
64-
}
65-
6661
if ($node instanceof FuncCall && ! $this->isName($node->name, 'finfo_buffer')) {
6762
return null;
6863
}
@@ -88,44 +83,44 @@ public function provideMinPhpVersion(): int
8883
}
8984

9085
/**
91-
* @param FuncCall|MethodCall $funcCall
86+
* @param FuncCall|MethodCall $callLike
9287
*/
93-
private function removeContextArg(CallLike $funcCall): bool
88+
private function removeContextArg(CallLike $callLike): bool
9489
{
9590
// In `finfo::buffer` method calls, the first parameter, compared to `finfo_buffer`, does not exist.
9691
$methodArgCorrection = 0;
97-
if ($funcCall instanceof MethodCall) {
92+
if ($callLike instanceof MethodCall) {
9893
$methodArgCorrection = -1;
9994
}
10095

101-
if (count($funcCall->args) <= 2 + $methodArgCorrection) {
96+
if (count($callLike->args) <= 2 + $methodArgCorrection) {
10297
return false;
10398
}
10499

105100
// Cannot handle variadic args
106-
foreach ($funcCall->args as $position => $arg) {
101+
foreach ($callLike->args as $position => $arg) {
107102
if (! $arg instanceof Arg) {
108103
return false;
109104
}
110105
}
111106

112107
/** @var array<Arg> $args */
113-
$args = $funcCall->args;
108+
$args = $callLike->args;
114109

115110
// Argument 3 ($flags) and argument 4 ($context) are optional, thus named parameters must be considered
116111
if (! $this->argsAnalyzer->hasNamedArg($args)) {
117112
if (count($args) < 4 + $methodArgCorrection) {
118113
return false;
119114
}
120115

121-
unset($funcCall->args[3 + $methodArgCorrection]);
116+
unset($callLike->args[3 + $methodArgCorrection]);
122117

123118
return true;
124119
}
125120

126121
foreach ($args as $position => $arg) {
127122
if ($arg->name instanceof Identifier && $arg->name->name === 'context') {
128-
unset($funcCall->args[$position]);
123+
unset($callLike->args[$position]);
129124

130125
return true;
131126
}

0 commit comments

Comments
 (0)