Skip to content

Commit 9ab4f86

Browse files
authored
[ci] check extra after part in fixture, that show no change + check no PHP files in /Fixture dirs (#7134)
* [ci] check extra after part in fixture, that show no change * cleanup fixtures with no change * [ci] add script that checks no PHP files in /Fixture directory
1 parent 7843966 commit 9ab4f86

12 files changed

Lines changed: 219 additions & 132 deletions

File tree

.github/workflows/code_analysis.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ jobs:
5252
name: 'Finalize classes'
5353
run: vendor/bin/swiss-knife finalize-classes src tests --dry-run
5454

55+
-
56+
name: 'Check before/after test fixture on no-changes'
57+
run: php bin/check-before-after-same-fixtures.php
58+
59+
-
60+
name: 'Check no "*.php" files in rules Fixture directory'
61+
run: php bin/no-php-file-in-fixtures.php
62+
5563
-
5664
name: 'Detect composer dependency issues'
5765
run: vendor/bin/composer-dependency-analyser
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Utils\Strings;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
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;
13+
use Symfony\Component\Finder\Finder;
14+
use Symfony\Component\Finder\SplFileInfo;
15+
use Webmozart\Assert\Assert;
16+
17+
final readonly class SameBeforeAfterFixtureDetector
18+
{
19+
private SymfonyStyle $symfonyStyle;
20+
21+
public function __construct()
22+
{
23+
$this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput());
24+
}
25+
26+
/**
27+
* @param string[] $testDirectories
28+
* @return Command::SUCCESS|Command::FAILURE
29+
*/
30+
public function run(array $testDirectories): int
31+
{
32+
$fixtureFiles = $this->findFixtureFiles($testDirectories);
33+
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);
52+
53+
return Command::FAILURE;
54+
}
55+
56+
/**
57+
* @param string[] $directories
58+
* @return SplFileInfo[]
59+
*/
60+
private function findFixtureFiles(array $directories): array
61+
{
62+
Assert::allDirectory($directories);
63+
64+
$finder = (new Finder())
65+
->files()
66+
->in($directories)
67+
->name('*.php.inc')
68+
->sortByName();
69+
70+
return iterator_to_array($finder->getIterator());
71+
}
72+
73+
private function hasFileSameBeforeAndAfterPart(SplFileInfo $fixtureFile): bool
74+
{
75+
$parts = Strings::split($fixtureFile->getContents(), '#^\s*-----\s*$#m');
76+
if (count($parts) !== 2) {
77+
return false;
78+
}
79+
80+
return trim((string) $parts[0]) === trim((string) $parts[1]);
81+
}
82+
}
83+
84+
$sameBeforeAfterFixtureDetector = new SameBeforeAfterFixtureDetector();
85+
exit($sameBeforeAfterFixtureDetector->run([__DIR__ . '/../tests', __DIR__ . '/../rules-tests']));

bin/no-php-file-in-fixtures.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\ArgvInput;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
use Symfony\Component\Finder\Finder;
12+
use Symfony\Component\Finder\SplFileInfo;
13+
use Webmozart\Assert\Assert;
14+
15+
final readonly class NoPhpFileInFixturesDetector
16+
{
17+
/**
18+
* @var string[]
19+
*/
20+
private const EXCLUDED_FILES = [
21+
// on-purpose as same namespace text
22+
'rules-tests/Renaming/Rector/Name/RenameClassRector/FixtureAutoImportNames/SomeShort.php',
23+
];
24+
25+
private SymfonyStyle $symfonyStyle;
26+
27+
public function __construct()
28+
{
29+
$this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput());
30+
}
31+
32+
/**
33+
* @param string[] $testDirectories
34+
* @return Command::SUCCESS|Command::FAILURE
35+
*/
36+
public function run(array $testDirectories): int
37+
{
38+
$phpFiles = $this->findPhpFiles($testDirectories);
39+
40+
$allFixtureFiles = $this->findFixtureFiles($testDirectories);
41+
42+
$relativePhpFiles = [];
43+
foreach ($phpFiles as $phpFile) {
44+
$relativeFilePath = substr($phpFile->getRealPath(), strlen(getcwd()) + 1);
45+
46+
// should skip?
47+
if (in_array($relativeFilePath, self::EXCLUDED_FILES, true)) {
48+
continue;
49+
}
50+
51+
$relativePhpFiles[] = $relativeFilePath;
52+
}
53+
54+
if ($relativePhpFiles === []) {
55+
$this->symfonyStyle->success(sprintf('All %d fixtures are valid', count($allFixtureFiles)));
56+
return Command::SUCCESS;
57+
}
58+
59+
$this->symfonyStyle->error(
60+
'The following "*.php* files were found in /Fixtures directory, but only "*.php.inc" files are picked up and allowed. Rename their suffix or remove them'
61+
);
62+
$this->symfonyStyle->listing($relativePhpFiles);
63+
64+
return Command::FAILURE;
65+
}
66+
67+
/**
68+
* @param string[] $directories
69+
* @return SplFileInfo[]
70+
*/
71+
private function findPhpFiles(array $directories): array
72+
{
73+
Assert::allDirectory($directories);
74+
75+
$finder = (new Finder())
76+
->files()
77+
->in($directories)
78+
->path('/Fixture')
79+
->path('/Fixture*')
80+
->notPath('Source')
81+
->name('*.php')
82+
->sortByName();
83+
84+
return iterator_to_array($finder->getIterator());
85+
}
86+
87+
/**
88+
* @param string[] $directories
89+
* @return SplFileInfo[]
90+
*/
91+
private function findFixtureFiles(array $directories): array
92+
{
93+
Assert::allDirectory($directories);
94+
95+
$finder = (new Finder())
96+
->files()
97+
->in($directories)
98+
->path('Fixture')
99+
->path('Fixture*')
100+
->notPath('Source')
101+
->sortByName();
102+
103+
return iterator_to_array($finder->getIterator());
104+
}
105+
}
106+
107+
$noPhpFileInFixturesDetector = new NoPhpFileInFixturesDetector();
108+
109+
exit($noPhpFileInFixturesDetector->run([__DIR__ . '/../rules-tests']));

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/Php80/Rector/NotIdentical/StrContainsRector/Fixture/offset_variable_zero_equal_strpos.php renamed to rules-tests/Php80/Rector/NotIdentical/StrContainsRector/Fixture/offset_variable_zero_equal_strpos.php.inc

File renamed without changes.

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/Renaming/Rector/Cast/RenameCastRector/RenameCastRectorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Rector\Tests\Renaming\Rector\Cast\RenameCastRector;
46

57
use Iterator;
68
use PHPUnit\Framework\Attributes\DataProvider;
79
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
810

9-
class RenameCastRectorTest extends AbstractRectorTestCase
11+
final class RenameCastRectorTest extends AbstractRectorTestCase
1012
{
1113
#[DataProvider('provideData')]
1214
public function test(string $filePath): void

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-
?>

0 commit comments

Comments
 (0)