Skip to content

Commit

Permalink
Inline Skipper (#2877)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Aug 31, 2022
1 parent e7c96a4 commit 2ffad93
Show file tree
Hide file tree
Showing 47 changed files with 791 additions and 22 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"symplify/easy-parallel": "^11.1",
"symplify/package-builder": "dev-main",
"symplify/rule-doc-generator-contracts": "^11.1",
"symplify/skipper": "^11.1",
"symplify/smart-file-system": "^11.1",
"webmozart/assert": "^1.11"
},
Expand Down
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Php\TypeChecker;
use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
use Symplify\PackageBuilder\Yaml\ParametersMerger;
use Symplify\SmartFileSystem\FileSystemFilter;
use Symplify\SmartFileSystem\FileSystemGuard;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\Normalizer\PathNormalizer;

return static function (RectorConfig $rectorConfig): void {
// make use of https://github.com/symplify/easy-parallel
Expand Down Expand Up @@ -242,4 +244,8 @@
$services->set(\PHPStan\PhpDocParser\Lexer\Lexer::class);
$services->set(TypeParser::class);
$services->set(ConstExprParser::class);

// skipper
$services->set(ClassLikeExistenceChecker::class);
$services->set(PathNormalizer::class);
};
2 changes: 2 additions & 0 deletions easy-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Rector\ReadWrite\Contract\ParentNodeReadAnalyzerInterface;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;
use Rector\Set\Contract\SetListInterface;
use Rector\Skipper\Contract\SkipVoterInterface;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\Testing\PHPUnit\AbstractTestCase;
Expand All @@ -50,6 +51,7 @@

return static function (EasyCIConfig $easyCiConfig): void {
$easyCiConfig->typesToSkip([
SkipVoterInterface::class,
AttributeDecoratorInterface::class,
ArrayItemNode::class,
PhpDocNodeDecoratorInterface::class,
Expand Down
Empty file.
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\FileSystem;

use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Skipper\FileSystem\FnMatchPathNormalizer;

final class FnMatchPathNormalizerTest extends TestCase
{
private FnMatchPathNormalizer $fnMatchPathNormalizer;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$container = $rectorKernel->create();

$this->fnMatchPathNormalizer = $container->get(FnMatchPathNormalizer::class);
}

/**
* @dataProvider providePaths
*/
public function testPaths(string $path, string $expectedNormalizedPath): void
{
$normalizedPath = $this->fnMatchPathNormalizer->normalizeForFnmatch($path);
$this->assertSame($expectedNormalizedPath, $normalizedPath);
}

public function providePaths(): Iterator
{
yield ['path/with/no/asterisk', 'path/with/no/asterisk'];
yield ['*path/with/asterisk/begin', '*path/with/asterisk/begin*'];
yield ['path/with/asterisk/end*', '*path/with/asterisk/end*'];
yield ['*path/with/asterisk/begin/and/end*', '*path/with/asterisk/begin/and/end*'];
yield [__DIR__ . '/Fixture/path/with/../in/it', __DIR__ . '/Fixture/path/in/it'];
yield [__DIR__ . '/Fixture/path/with/../../in/it', __DIR__ . '/Fixture/in/it'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
you
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\SkipCriteriaResolver\SkippedPathsResolver;

use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;

final class SkippedPathsResolverTest extends TestCase
{
private SkippedPathsResolver $skippedPathsResolver;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);

$this->skippedPathsResolver = $containerBuilder->get(SkippedPathsResolver::class);
}

public function test(): void
{
$skippedPaths = $this->skippedPathsResolver->resolve();
$this->assertCount(2, $skippedPaths);

$this->assertSame('*/Mask/*', $skippedPaths[1]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->skip([
// windows slashes
__DIR__ . '\non-existing-path',
__DIR__ . '/../Fixture',
'*\Mask\*',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yes, you can
17 changes: 17 additions & 0 deletions packages-tests/Skipper/Skipper/Skip/Fixture/skip.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

function foo() {
if (rand(0,1)) {
return 1;
} else {
return 2;
}
}

function bar() {
if (rand(0,1)) {
return 1;
} else {
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

echo 'hi';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

echo 'hello';
1 change: 1 addition & 0 deletions packages-tests/Skipper/Skipper/Skip/Fixture/someFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some text
1 change: 1 addition & 0 deletions packages-tests/Skipper/Skipper/Skip/Fixture/someOtherFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some text
60 changes: 60 additions & 0 deletions packages-tests/Skipper/Skipper/Skip/SkipSkipperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skip;

use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Skipper\Skipper\Skipper;
use Rector\Tests\Skipper\Skipper\Skip\Source\AnotherClassToSkip;
use Rector\Tests\Skipper\Skipper\Skip\Source\NotSkippedClass;
use Rector\Tests\Skipper\Skipper\Skip\Source\SomeClassToSkip;

final class SkipSkipperTest extends TestCase
{
private Skipper $skipper;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);

$this->skipper = $containerBuilder->get(Skipper::class);
}

/**
* @dataProvider provideCheckerAndFile()
* @dataProvider provideAnythingAndFilePath()
*/
public function test(string $element, string $filePath, bool $expectedSkip): void
{
$resolvedSkip = $this->skipper->shouldSkipElementAndFileInfo($element, $filePath);
$this->assertSame($expectedSkip, $resolvedSkip);
}

/**
* @return Iterator<string[]|bool[]|class-string<AnotherClassToSkip>[]|class-string<NotSkippedClass>[]|class-string<SomeClassToSkip>[]>
*/
public function provideCheckerAndFile(): Iterator
{
yield [SomeClassToSkip::class, __DIR__ . '/Fixture', true];

yield [AnotherClassToSkip::class, __DIR__ . '/Fixture/someFile', true];
yield [AnotherClassToSkip::class, __DIR__ . '/Fixture/someDirectory/anotherFile.php', true];
yield [AnotherClassToSkip::class, __DIR__ . '/Fixture/someDirectory/anotherFile.php', true];

yield [NotSkippedClass::class, __DIR__ . '/Fixture/someFile', false];
yield [NotSkippedClass::class, __DIR__ . '/Fixture/someOtherFile', false];
}

/**
* @return Iterator<string[]|bool[]>
*/
public function provideAnythingAndFilePath(): Iterator
{
yield ['anything', __DIR__ . '/Fixture/AlwaysSkippedPath/some_file.txt', true];
yield ['anything', __DIR__ . '/Fixture/PathSkippedWithMask/another_file.txt', true];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skip\Source;

final class AnotherClassToSkip
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skip\Source;

final class NotSkippedClass
{
}
10 changes: 10 additions & 0 deletions packages-tests/Skipper/Skipper/Skip/Source/SomeClassToSkip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skip\Source;

final class SomeClassToSkip
{

}
21 changes: 21 additions & 0 deletions packages-tests/Skipper/Skipper/Skip/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Tests\Skipper\Skipper\Skip\Source\AnotherClassToSkip;
use Rector\Tests\Skipper\Skipper\Skip\Source\SomeClassToSkip;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->skip([
// classes
SomeClassToSkip::class,

// classes only in specific paths
AnotherClassToSkip::class => ['Fixture/someFile', '*/someDirectory/*'],

// file paths
__DIR__ . '/../Fixture/AlwaysSkippedPath',
'*\PathSkippedWithMask\*',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element;


final class FifthElement
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element;


final class SixthSense
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element;


final class ThreeMan
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
any content
67 changes: 67 additions & 0 deletions packages-tests/Skipper/Skipper/Skipper/SkipperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Skipper\Skipper\Skipper;

use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Skipper\Skipper\Skipper;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\FifthElement;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\SixthSense;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\ThreeMan;
use Symplify\SmartFileSystem\SmartFileInfo;

final class SkipperTest extends TestCase
{
private Skipper $skipper;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);

$this->skipper = $containerBuilder->get(Skipper::class);
}

/**
* @dataProvider provideDataShouldSkipFileInfo()
*/
public function testSkipFileInfo(string $filePath, bool $expectedSkip): void
{
$smartFileInfo = new SmartFileInfo($filePath);

$resultSkip = $this->skipper->shouldSkipFileInfo($smartFileInfo);
$this->assertSame($expectedSkip, $resultSkip);
}

/**
* @return Iterator<string[]|bool[]>
*/
public function provideDataShouldSkipFileInfo(): Iterator
{
yield [__DIR__ . '/Fixture/SomeRandom/file.txt', false];
yield [__DIR__ . '/Fixture/SomeSkipped/any.txt', true];
}

/**
* @param object|class-string $element
* @dataProvider provideDataShouldSkipElement()
*/
public function testSkipElement(string|object $element, bool $expectedSkip): void
{
$resultSkip = $this->skipper->shouldSkipElement($element);
$this->assertSame($expectedSkip, $resultSkip);
}

/**
* @return Iterator<bool[]|class-string<SixthSense>[]|class-string<ThreeMan>[]|FifthElement[]>
*/
public function provideDataShouldSkipElement(): Iterator
{
yield [ThreeMan::class, false];
yield [SixthSense::class, true];
yield [new FifthElement(), true];
}
}

0 comments on commit 2ffad93

Please sign in to comment.