Skip to content

Commit

Permalink
Changed process of excluding code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
shudd3r committed Oct 4, 2022
1 parent 0787dcd commit f54566f
Show file tree
Hide file tree
Showing 30 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: "Coding standard CodeSniffer checks"
run: |
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist src
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist --ignore=tests/CodeSamples/* tests
vendor/bin/phpcs --extensions=php --standard=phpcs.xml.dist --ignore=*/code-samples/* tests
- name: "Package skeleton validation"
run: php polymorphine-skeleton check
- name: "Run PhpUnit tests with coverage"
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"autoload-dev": {
"psr-4": {
"Polymorphine\\Dev\\Tests\\": "tests/"
"Polymorphine\\Dev\\Tests\\": "tests/",
"Polymorphine\\Dev\\CodeSamples\\": "tests/Fixtures/code-samples/"
}
},
"bin": [
Expand All @@ -35,7 +36,7 @@
"test-cs": [
"php-cs-fixer --dry-run -v --config=cs-fixer.php.dist --path-mode=intersection fix src tests",
"phpcs --extensions=php --standard=phpcs.xml.dist src",
"phpcs --extensions=php --standard=phpcs.xml.dist --ignore=tests/CodeSamples/* tests"
"phpcs --extensions=php --standard=phpcs.xml.dist --ignore=*/code-samples/* tests"
],
"test-php": "phpunit",
"test-skeleton": "@php polymorphine-skeleton check"
Expand Down
7 changes: 1 addition & 6 deletions cs-fixer.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@

use Polymorphine\Dev\FixerFactory;

$filter = function (SplFileInfo $file) {
$samples = __DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'CodeSamples';
return strpos($file->getPath(), $samples) !== 0;
};

return FixerFactory::createFor('Polymorphine/Dev', __DIR__, [$filter]);
return FixerFactory::createFor('Polymorphine/Dev', __DIR__);
20 changes: 11 additions & 9 deletions src/FixerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use SplFileInfo;


final class FixerFactory
Expand Down Expand Up @@ -83,13 +84,12 @@ final class FixerFactory
];

/**
* @param string $packageName
* @param string $workingDir
* @param callable[] $filters fn(SplFileInfo) => bool - false will ignore file
* @param string $packageName
* @param string $workingDir
*
* @return Config
*/
public static function createFor(string $packageName, string $workingDir, array $filters = []): Config
public static function createFor(string $packageName, string $workingDir): Config
{
self::$rules['header_comment']['header'] = str_replace('{{name}}', $packageName, self::HEADER);
self::$rules['no_extra_blank_lines']['tokens'] = [
Expand All @@ -108,16 +108,18 @@ public static function createFor(string $packageName, string $workingDir, array
self::$rules['Polymorphine/declare_strict_first_line'] = true;
self::$rules['Polymorphine/brace_after_multiline_param_method'] = true;

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

$config = new Config();
return $config
->setRiskyAllowed(true)
->setRules(self::$rules)
->setFinder($finder)
->setFinder(Finder::create()->in($workingDir)->filter($excludeSamples))
->setUsingCache(false)
->registerCustomFixers([
new Fixer\DoubleLineBeforeClassDefinitionFixer(),
Expand Down
2 changes: 1 addition & 1 deletion template/.github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: "Coding standard CodeSniffer checks"
run: |
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} src
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} --ignore=tests/CodeSamples/* tests
vendor/bin/phpcs --extensions=php --standard=${tpl.PHPCS} --ignore=*/code-samples/* tests
- name: "Package skeleton validation"
run: ${tpl.PHP_EXEC}polymorphine-skeleton check
- name: "Run PhpUnit tests with coverage"
Expand Down
2 changes: 1 addition & 1 deletion template/composer.json.sk_file
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test-cs": [
"php-cs-fixer --dry-run -v --config=cs-fixer.php.dist --path-mode=intersection fix src tests",
"phpcs --extensions=php --standard={$tpl.PHPCS} src",
"phpcs --extensions=php --standard={$tpl.PHPCS} --ignore=tests/CodeSamples/* tests"
"phpcs --extensions=php --standard={$tpl.PHPCS} --ignore=*/code-samples/* tests"
],
"test-php": "phpunit",
"test-skeleton": "{$tpl.PHP_EXEC}polymorphine-skeleton check"
Expand Down
4 changes: 2 additions & 2 deletions template/cs-fixer.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
*/

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

return FixerFactory::createFor('{package.name}', __DIR__);
4 changes: 2 additions & 2 deletions tests/CompoundFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function testFixedFiles_MatchExpectations(string $fileExpected, string $f
public function fileList(): array
{
$files = [];
foreach (array_diff(scandir(__DIR__ . '/CodeSamples/Fixer'), ['..', '.']) as $file) {
foreach (array_diff(scandir(__DIR__ . '/Fixtures/code-samples/Fixer'), ['..', '.']) as $file) {
[$type, $index] = explode('-', $file, 2) + [false, false];
$id = ($type === 'expected') ? 0 : 1;
isset($files[$index]) or $files[$index] = [];
$files[$index][$id] = __DIR__ . '/CodeSamples/Fixer/' . $file;
$files[$index][$id] = __DIR__ . '/Fixtures/code-samples/Fixer/' . $file;
}

return $files;
Expand Down
15 changes: 13 additions & 2 deletions tests/FixerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ class FixerFactoryTest extends TestCase
{
public function testConfigInstantiation()
{
$filters = [fn () => true];
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor('package/name', __DIR__, $filters));
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor('package/name', __DIR__));
}

public function testConfigFinder_IgnoresCodeSamples()
{
$excluded = __DIR__ . str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/code-samples');
$finder = FixerFactory::createFor('package/name', dirname(__DIR__))->getFinder();

$this->assertTrue($finder->hasResults());

foreach ($finder->getIterator() as $file) {
$this->assertFalse(strpos($file->getPath(), $excluded));
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Polymorphine\Dev\Tests\CodeSamples\Sniffs;
namespace Polymorphine\Dev\CodeSamples\Sniffs;


class PhpDocRequiredForClassApi extends PhpDocRequiredForParentApi implements PhpDocRequiredForInterfaceApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Polymorphine\Dev\Tests\CodeSamples\Sniffs;
namespace Polymorphine\Dev\CodeSamples\Sniffs;


interface PhpDocRequiredForInterfaceApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Polymorphine\Dev\Tests\CodeSamples\Sniffs;
namespace Polymorphine\Dev\CodeSamples\Sniffs;


class PhpDocRequiredForInvalidClass extends NotExistingParent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Polymorphine\Dev\Tests\CodeSamples\Sniffs;
namespace Polymorphine\Dev\CodeSamples\Sniffs;


class PhpDocRequiredForParentApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InvalidMixedArrayTypeSniffTest extends SnifferTest
{
public function testAssocArrayWithNonAssocValuesGivesWarning()
{
$this->assertWarningLines('./tests/CodeSamples/Sniffs/InvalidArrays.php', [5, 20, 22, 27]);
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/InvalidArrays.php', [5, 20, 22, 27]);
}

protected function sniffer(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CallableDefinitionSniffTest extends SnifferTest
public function testCallableParamDocWithoutDefinitionGivesWarning(array $properties, array $expectedWarningLines)
{
$this->setProperties($properties);
$this->assertWarningLines('./tests/CodeSamples/Sniffs/PhpDocCallableDefinitions.php', $expectedWarningLines);
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/PhpDocCallableDefinitions.php', $expectedWarningLines);
}

public function properties(): array
Expand Down
8 changes: 4 additions & 4 deletions tests/Sniffer/Sniffs/PhpDoc/RequiredForPublicApiSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function testInterfaceWarnings(string $filename, array $warningLines)
public function classFileWarnings(): array
{
return [
'interface' => ['./tests/CodeSamples/Sniffs/PhpDocRequiredForInterfaceApi.php', [12]],
'class' => ['./tests/CodeSamples/Sniffs/PhpDocRequiredForClassApi.php', [14]],
'parent' => ['./tests/CodeSamples/Sniffs/PhpDocRequiredForParentApi.php', [8]],
'invalid' => ['./tests/CodeSamples/Sniffs/PhpDocRequiredForInvalidClass.php', [8]]
'interface' => ['./tests/Fixtures/code-samples/Sniffs/PhpDocRequiredForInterfaceApi.php', [12]],
'class' => ['./tests/Fixtures/code-samples/Sniffs/PhpDocRequiredForClassApi.php', [14]],
'parent' => ['./tests/Fixtures/code-samples/Sniffs/PhpDocRequiredForParentApi.php', [8]],
'invalid' => ['./tests/Fixtures/code-samples/Sniffs/PhpDocRequiredForInvalidClass.php', [8]]
];
}

Expand Down

0 comments on commit f54566f

Please sign in to comment.