Skip to content

Commit f54566f

Browse files
committed
Changed process of excluding code samples
1 parent 0787dcd commit f54566f

30 files changed

+45
-36
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
- name: "Coding standard CodeSniffer checks"
5151
run: |
5252
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist src
53-
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist --ignore=tests/CodeSamples/* tests
53+
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist --ignore=*/code-samples/* tests
5454
- name: "Package skeleton validation"
5555
run: php polymorphine-skeleton check
5656
- name: "Run PhpUnit tests with coverage"

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},
2626
"autoload-dev": {
2727
"psr-4": {
28-
"Polymorphine\\Dev\\Tests\\": "tests/"
28+
"Polymorphine\\Dev\\Tests\\": "tests/",
29+
"Polymorphine\\Dev\\CodeSamples\\": "tests/Fixtures/code-samples/"
2930
}
3031
},
3132
"bin": [
@@ -35,7 +36,7 @@
3536
"test-cs": [
3637
"php-cs-fixer --dry-run -v --config=cs-fixer.php.dist --path-mode=intersection fix src tests",
3738
"phpcs --extensions=php --standard=phpcs.xml.dist src",
38-
"phpcs --extensions=php --standard=phpcs.xml.dist --ignore=tests/CodeSamples/* tests"
39+
"phpcs --extensions=php --standard=phpcs.xml.dist --ignore=*/code-samples/* tests"
3940
],
4041
"test-php": "phpunit",
4142
"test-skeleton": "@php polymorphine-skeleton check"

cs-fixer.php.dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@
1111

1212
use Polymorphine\Dev\FixerFactory;
1313

14-
$filter = function (SplFileInfo $file) {
15-
$samples = __DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'CodeSamples';
16-
return strpos($file->getPath(), $samples) !== 0;
17-
};
18-
19-
return FixerFactory::createFor('Polymorphine/Dev', __DIR__, [$filter]);
14+
return FixerFactory::createFor('Polymorphine/Dev', __DIR__);

src/FixerFactory.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PhpCsFixer\Config;
1515
use PhpCsFixer\Finder;
16+
use SplFileInfo;
1617

1718

1819
final class FixerFactory
@@ -83,13 +84,12 @@ final class FixerFactory
8384
];
8485

8586
/**
86-
* @param string $packageName
87-
* @param string $workingDir
88-
* @param callable[] $filters fn(SplFileInfo) => bool - false will ignore file
87+
* @param string $packageName
88+
* @param string $workingDir
8989
*
9090
* @return Config
9191
*/
92-
public static function createFor(string $packageName, string $workingDir, array $filters = []): Config
92+
public static function createFor(string $packageName, string $workingDir): Config
9393
{
9494
self::$rules['header_comment']['header'] = str_replace('{{name}}', $packageName, self::HEADER);
9595
self::$rules['no_extra_blank_lines']['tokens'] = [
@@ -108,16 +108,18 @@ public static function createFor(string $packageName, string $workingDir, array
108108
self::$rules['Polymorphine/declare_strict_first_line'] = true;
109109
self::$rules['Polymorphine/brace_after_multiline_param_method'] = true;
110110

111-
$finder = Finder::create()->in($workingDir);
112-
foreach ($filters as $filter) {
113-
$finder = $finder->filter($filter);
114-
}
111+
$excludeSamples = function (SplFileInfo $file) use ($workingDir) {
112+
$filePath = $file->getPath();
113+
$testsPath = $workingDir . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR;
114+
$samplesDir = DIRECTORY_SEPARATOR . 'code-samples' . DIRECTORY_SEPARATOR;
115+
return strpos($filePath, $testsPath) !== 0 || strpos($filePath, $samplesDir) === false;
116+
};
115117

116118
$config = new Config();
117119
return $config
118120
->setRiskyAllowed(true)
119121
->setRules(self::$rules)
120-
->setFinder($finder)
122+
->setFinder(Finder::create()->in($workingDir)->filter($excludeSamples))
121123
->setUsingCache(false)
122124
->registerCustomFixers([
123125
new Fixer\DoubleLineBeforeClassDefinitionFixer(),

template/.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
- name: "Coding standard CodeSniffer checks"
5151
run: |
5252
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} src
53-
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} --ignore=tests/CodeSamples/* tests
53+
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} --ignore=*/code-samples/* tests
5454
- name: "Package skeleton validation"
5555
run: ${tpl.PHP_EXEC}polymorphine-skeleton check
5656
- name: "Run PhpUnit tests with coverage"

template/composer.json.sk_file

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"test-cs": [
3333
"php-cs-fixer --dry-run -v --config=cs-fixer.php.dist --path-mode=intersection fix src tests",
3434
"phpcs --extensions=php --standard={$tpl.PHPCS} src",
35-
"phpcs --extensions=php --standard={$tpl.PHPCS} --ignore=tests/CodeSamples/* tests"
35+
"phpcs --extensions=php --standard={$tpl.PHPCS} --ignore=*/code-samples/* tests"
3636
],
3737
"test-php": "phpunit",
3838
"test-skeleton": "{$tpl.PHP_EXEC}polymorphine-skeleton check"

template/cs-fixer.php.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
*/
1111

1212
use Polymorphine\Dev\FixerFactory;
13-
{original.content}
14-
return FixerFactory::createFor('{package.name}', __DIR__{original.content});
13+
14+
return FixerFactory::createFor('{package.name}', __DIR__);

tests/CompoundFixerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function testFixedFiles_MatchExpectations(string $fileExpected, string $f
4444
public function fileList(): array
4545
{
4646
$files = [];
47-
foreach (array_diff(scandir(__DIR__ . '/CodeSamples/Fixer'), ['..', '.']) as $file) {
47+
foreach (array_diff(scandir(__DIR__ . '/Fixtures/code-samples/Fixer'), ['..', '.']) as $file) {
4848
[$type, $index] = explode('-', $file, 2) + [false, false];
4949
$id = ($type === 'expected') ? 0 : 1;
5050
isset($files[$index]) or $files[$index] = [];
51-
$files[$index][$id] = __DIR__ . '/CodeSamples/Fixer/' . $file;
51+
$files[$index][$id] = __DIR__ . '/Fixtures/code-samples/Fixer/' . $file;
5252
}
5353

5454
return $files;

tests/FixerFactoryTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ class FixerFactoryTest extends TestCase
2020
{
2121
public function testConfigInstantiation()
2222
{
23-
$filters = [fn () => true];
24-
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor('package/name', __DIR__, $filters));
23+
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor('package/name', __DIR__));
24+
}
25+
26+
public function testConfigFinder_IgnoresCodeSamples()
27+
{
28+
$excluded = __DIR__ . str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/code-samples');
29+
$finder = FixerFactory::createFor('package/name', dirname(__DIR__))->getFinder();
30+
31+
$this->assertTrue($finder->hasResults());
32+
33+
foreach ($finder->getIterator() as $file) {
34+
$this->assertFalse(strpos($file->getPath(), $excluded));
35+
}
2536
}
2637
}

0 commit comments

Comments
 (0)