Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#103 - purging doc blocks option for no comment fixer #116

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function options(): array
];
}

public function purgeMode(): static
public function purgeMode(bool $purgeDocComments = true): static
{
$this->rules->add(new Rule(NoCommentFixer::class));
$this->rules->add(new Rule(NoCommentFixer::class, ["doc_comment" => $purgeDocComments]));

return $this;
}
Expand Down
21 changes: 17 additions & 4 deletions src/Fixers/NoCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@

namespace Blumilk\Codestyle\Fixers;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
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
final class NoCommentFixer extends AbstractFixer implements ConfigurableFixerInterface
{
public function getConfigurationDefinition(): FixerConfigurationResolver
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder("doc_comment", "Docblock comments should be removed."))
->setAllowedTypes(["bool"])
->setDefault(false)
->getOption(),
]);
}

public function getDefinition(): FixerDefinitionInterface
{
$codeSample = <<<'EOF'
Expand Down Expand Up @@ -68,10 +81,10 @@ public function isRisky(): bool
return true;
}

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

Expand Down
16 changes: 14 additions & 2 deletions tests/codestyle/PurgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@

class PurgeTest extends CodestyleTestCase
{
private string $config;

/**
* @throws Exception
*/
public function testPhp82Fixtures(): void
public function testPhp82PurgeMode(): void
{
$this->config = "./tests/codestyle/config/config.purge.php";
$this->testFixture("noComments");
}

/**
* @throws Exception
*/
public function testPhp82PurgeWithoutDocCommentsTest(): void
{
$this->config = "./tests/codestyle/config/config.purge.without.doc.comments.php";
$this->testFixture("noCommentsWithoutDocComments");
}

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

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\Paths;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;

$paths = new Paths("tests/codestyle/tmp");
$rules = new CommonRules();

$config = new Config(
paths: $paths,
rules: $rules->filter(PsrAutoloadingFixer::class),
);

return $config->purgeMode(false)->config();
5 changes: 5 additions & 0 deletions tests/fixtures/noComments/actual.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

use Illuminate\Support\Str;



/**
| This is configuration file for Laravel application.
*/
return [
/*
|--------------------------------------------------------------------------
Expand Down
153 changes: 153 additions & 0 deletions tests/fixtures/noCommentsWithoutDocComments/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Str;



/**
* This is configuration file for Laravel application.
* @see https://laravel.com/docs/11.x/database
*/
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

"default" => env("DB_CONNECTION", "mysql"),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],

"mysql" => [
"driver" => "mysql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "3306"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"unix_socket" => env("DB_SOCKET", ""),
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => true,
"engine" => null,
"options" => extension_loaded("pdo_mysql") ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
]) : [],
],

"pgsql" => [
"driver" => "pgsql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "5432"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
"search_path" => "public",
"sslmode" => "prefer",
],

"sqlsrv" => [
"driver" => "sqlsrv",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "localhost"),
"port" => env("DB_PORT", "1433"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
// 'encrypt' => env('DB_ENCRYPT', 'yes'),
// 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'),
],
],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

"migrations" => "migrations",

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

"redis" => [
"client" => env("REDIS_CLIENT", "phpredis"),

"options" => [
"cluster" => env("REDIS_CLUSTER", "redis"),
"prefix" => env("REDIS_PREFIX", Str::slug(env("APP_NAME", "laravel"), "_") . "_database_"),
],

"default" => [
"url" => env("REDIS_URL"),
"host" => env("REDIS_HOST", "127.0.0.1"),
"username" => env("REDIS_USERNAME"),
"password" => env("REDIS_PASSWORD"),
"port" => env("REDIS_PORT", "6379"),
"database" => env("REDIS_DB", "0"),
],

"cache" => [
"url" => env("REDIS_URL"),
"host" => env("REDIS_HOST", "127.0.0.1"),
"username" => env("REDIS_USERNAME"),
"password" => env("REDIS_PASSWORD"),
"port" => env("REDIS_PORT", "6379"),
"database" => env("REDIS_CACHE_DB", "1"),
],
],
];
100 changes: 100 additions & 0 deletions tests/fixtures/noCommentsWithoutDocComments/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Str;

/**
* This is configuration file for Laravel application.
* @see https://laravel.com/docs/11.x/database
*/
return [
"default" => env("DB_CONNECTION", "mysql"),

"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],

"mysql" => [
"driver" => "mysql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "3306"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"unix_socket" => env("DB_SOCKET", ""),
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => true,
"engine" => null,
"options" => extension_loaded("pdo_mysql") ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
]) : [],
],

"pgsql" => [
"driver" => "pgsql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "5432"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
"search_path" => "public",
"sslmode" => "prefer",
],

"sqlsrv" => [
"driver" => "sqlsrv",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "localhost"),
"port" => env("DB_PORT", "1433"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
],
],

"migrations" => "migrations",

"redis" => [
"client" => env("REDIS_CLIENT", "phpredis"),

"options" => [
"cluster" => env("REDIS_CLUSTER", "redis"),
"prefix" => env("REDIS_PREFIX", Str::slug(env("APP_NAME", "laravel"), "_") . "_database_"),
],

"default" => [
"url" => env("REDIS_URL"),
"host" => env("REDIS_HOST", "127.0.0.1"),
"username" => env("REDIS_USERNAME"),
"password" => env("REDIS_PASSWORD"),
"port" => env("REDIS_PORT", "6379"),
"database" => env("REDIS_DB", "0"),
],

"cache" => [
"url" => env("REDIS_URL"),
"host" => env("REDIS_HOST", "127.0.0.1"),
"username" => env("REDIS_USERNAME"),
"password" => env("REDIS_PASSWORD"),
"port" => env("REDIS_PORT", "6379"),
"database" => env("REDIS_CACHE_DB", "1"),
],
],
];
Loading