Skip to content

Commit

Permalink
#65 - purge mode (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak authored May 30, 2023
1 parent e7d822f commit f858620
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.composer
/.idea
/playground
/vendor

composer.lock
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"Blumilk\\Codestyle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Blumilk\\Codestyle\\Tests\\": "tests/codestyle/"
}
},
"scripts": {
"cs": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config codestyle.php",
"csf": "./vendor/bin/php-cs-fixer fix --diff --config codestyle.php",
Expand Down
12 changes: 11 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths;
use Blumilk\Codestyle\Configuration\Paths;
use Blumilk\Codestyle\Configuration\Rules;
use Blumilk\Codestyle\Configuration\Utils\Rule;
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use Blumilk\Codestyle\Fixers\NoCommentFixer;
use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer;
use JetBrains\PhpStorm\ArrayShape;
use PhpCsFixer\Config as PhpCsFixerConfig;
Expand Down Expand Up @@ -42,8 +44,8 @@ public function config(): PhpCsFixerConfig
}

$finder = Finder::create()->directories()->append($files);

$config = new PhpCsFixerConfig("Blumilk codestyle standard");

return $config->setFinder($finder)
->setUsingCache(false)
->registerCustomFixers(new PhpCsFixerCustomFixers())
Expand All @@ -61,6 +63,13 @@ public function options(): array
];
}

public function purgeMode(): static
{
$this->rules->add(new Rule(NoCommentFixer::class));

return $this;
}

protected function getAllFiles(array &$paths, string $path): void
{
if (is_file($path) || !is_dir($path)) {
Expand All @@ -83,6 +92,7 @@ protected function getCustomFixers(): array
return [
new DoubleQuoteFixer(),
new NoLaravelMigrationsGeneratedCommentFixer(),
new NoCommentFixer(),
];
}
}
3 changes: 3 additions & 0 deletions src/Configuration/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Rule
{
/**
* @param class-string $fixer
*/
public function __construct(
protected string $fixer,
protected ?array $options = null,
Expand Down
80 changes: 80 additions & 0 deletions src/Fixers/NoCommentFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Fixers;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixerCustomFixers\TokenRemover;
use SplFileInfo;

final class NoCommentFixer implements FixerInterface
{
public function getDefinition(): FixerDefinitionInterface
{
$codeSample = <<<'EOF'
<?php
class Migration
{
public function up()
{
Schema::create("sessions", function (Blueprint $table) {
// test
$table->string("id")->primary();
$table->text("payload");
});
}
};
EOF;

return new FixerDefinition(
"There can be no comments.",
[
new CodeSample($codeSample),
],
);
}

public function getName(): string
{
return "Blumilk/no_comments";
}

public function getPriority(): int
{
$fixer = new NoExtraBlankLinesFixer();
return $fixer->getPriority() + 1;
}

public function supports(SplFileInfo $file): bool
{
return true;
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
}

public function isRisky(): bool
{
return true;
}

public function fix(SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index > 0; $index--) {
if (!$tokens[$index]->isGivenKind([T_COMMENT])) {
continue;
}

TokenRemover::removeWithLinesIfPossible($tokens, $index);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

declare(strict_types=1);

namespace Blumilk\Codestyle\Tests;

use Exception;
use PhpCsFixer\Console\Application;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

class CodestyleTest extends TestCase
abstract class CodestyleTestCase extends TestCase
{
protected function setUp(): void
{
Expand All @@ -19,75 +22,6 @@ protected function tearDown(): void
$this->clearTempDirectory();
}

/**
* @dataProvider providePhp80Fixtures
* @requires PHP >= 8.0
* @throws Exception
*/
public function testPhp80Fixtures(string $name): void
{
$this->testFixture($name);
}

/**
* @dataProvider providePhp81Fixtures
* @requires PHP >= 8.1
* @throws Exception
*/
public function testPhp81Fixtures(string $name): void
{
$this->testFixture($name);
}

/**
* @dataProvider providePhp82Fixtures
* @requires PHP >= 8.2
* @throws Exception
*/
public function testPhp82Fixtures(string $name): void
{
$this->testFixture($name);
}

public static function providePhp80Fixtures(): array
{
return [
["noExtraBlankLines"],
["nullableTypeForDefaultNull"],
["operatorSpacing"],
["singleQuotes"],
["strictTypes"],
["trailingCommas"],
["unionTypes"],
["references"],
["classAttributesSeparation"],
["uselessParenthesis"],
["laravelMigrations"],
["phpdocs"],
["yodaStyle"],
["objectOperators"],
["anonymousFunctions"],
["namespaces"],
["emptyLines"],
["importsOrder"],
];
}

public static function providePhp81Fixtures(): array
{
return [
["enums"],
["readonlies"],
];
}

public static function providePhp82Fixtures(): array
{
return [
["php82"],
];
}

/**
* @dataProvider providePhp80Fixtures
* @throws Exception
Expand Down Expand Up @@ -124,7 +58,8 @@ protected function runFixer(bool $fix = false): bool
$application->setAutoExit(false);

$output = new BufferedOutput();
$result = $application->run(new StringInput("fix {$dryRun} --diff --config ./tests/codestyle/config.php"), $output);
$config = $this->getConfigPath();
$result = $application->run(new StringInput("fix {$dryRun} --diff --config $config"), $output);

return $result === 0;
}
Expand All @@ -136,4 +71,9 @@ protected function clearTempDirectory(): void
unlink($file);
}
}

protected function getConfigPath(): string
{
return "./tests/codestyle/config/config.php";
}
}
76 changes: 76 additions & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
namespace Blumilk\Codestyle\Tests;

class CommonRulesetTest extends CodestyleTestCase
{
/**
* @dataProvider providePhp80Fixtures
* @requires PHP >= 8.0
* @throws Exception
*/
public function testPhp80Fixtures(string $name): void
{
$this->testFixture($name);
}

/**
* @dataProvider providePhp81Fixtures
* @requires PHP >= 8.1
* @throws Exception
*/
public function testPhp81Fixtures(string $name): void
{
$this->testFixture($name);
}

/**
* @dataProvider providePhp82Fixtures
* @requires PHP >= 8.2
* @throws Exception
*/
public function testPhp82Fixtures(string $name): void
{
$this->testFixture($name);
}

public static function providePhp80Fixtures(): array
{
return [
["noExtraBlankLines"],
["nullableTypeForDefaultNull"],
["operatorSpacing"],
["singleQuotes"],
["strictTypes"],
["trailingCommas"],
["unionTypes"],
["references"],
["classAttributesSeparation"],
["uselessParenthesis"],
["laravelMigrations"],
["phpdocs"],
["yodaStyle"],
["objectOperators"],
["anonymousFunctions"],
["namespaces"],
["emptyLines"],
["importsOrder"],
];
}

public static function providePhp81Fixtures(): array
{
return [
["enums"],
["readonlies"],
];
}

public static function providePhp82Fixtures(): array
{
return [
["php82"],
];
}
}
20 changes: 20 additions & 0 deletions tests/codestyle/PurgeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Tests;
class PurgeTest extends CodestyleTestCase
{
/**
* @throws Exception
*/
public function testPhp82Fixtures(): void
{
$this->testFixture("noComments");
}

protected function getConfigPath(): string
{
return "./tests/codestyle/config/config.purge.php";
}
}
File renamed without changes.
12 changes: 12 additions & 0 deletions tests/codestyle/config/config.purge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\Paths;

$config = new Config(
paths: new Paths("tests/codestyle/tmp"),
);

return $config->purgeMode()->config();
Loading

0 comments on commit f858620

Please sign in to comment.