From 086e7d5f766191a592a798c81a2c2bb7fb1eb4cb Mon Sep 17 00:00:00 2001 From: Sink Date: Mon, 18 Nov 2024 01:34:52 +0800 Subject: [PATCH 1/2] feat: #22 lint by pmd --- README.md | 4 ++- composer.json | 3 ++- src/LintCodeCommand.php | 20 +++++--------- src/LintPhpcsCommand.php | 48 ++++++++++++++++++++++++++++++++++ src/LintPmdCommand.php | 47 +++++++++++++++++++++++++++++++++ src/LintServiceProvider.php | 2 ++ tests/LintPhpcsCommandTest.php | 27 +++++++++++++++++++ tests/LintPmdCommandTest.php | 27 +++++++++++++++++++ 8 files changed, 163 insertions(+), 15 deletions(-) create mode 100644 src/LintPhpcsCommand.php create mode 100644 src/LintPmdCommand.php create mode 100644 tests/LintPhpcsCommandTest.php create mode 100644 tests/LintPmdCommandTest.php diff --git a/README.md b/README.md index aa12fda..2d73421 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,9 @@ php artisan lint:publish php artisan lint:code php artisan lint:code --fix php artisan lint:code app/ tests/ -php artisan lint:code --standard=Squiz app/ +php artisan lint:code app/ tests/ --fix +php artisan lint:phpcs +php artisan lint:pmd php artisan lint:staged ``` diff --git a/composer.json b/composer.json index 89abb48..beb58c4 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "squizlabs/php_codesniffer": ">=3.5" }, "require-dev": { - "orchestra/testbench": ">=v7" + "orchestra/testbench": ">=v7", + "php-mock/php-mock": "^2.5" } } diff --git a/src/LintCodeCommand.php b/src/LintCodeCommand.php index d27727b..bd6d069 100644 --- a/src/LintCodeCommand.php +++ b/src/LintCodeCommand.php @@ -15,8 +15,7 @@ class LintCodeCommand extends Command */ protected $signature = 'lint:code {files?*} - {--fix : automatic fix} - {--standard=phpcs.xml : coding standards}'; + {--fix : automatic fix}'; /** * The console command description. @@ -32,17 +31,12 @@ class LintCodeCommand extends Command */ public function handle() { - $bin = $this->option('fix') ? 'phpcbf' : 'phpcs'; - $files = empty($this->argument('files')) ? ['.'] : $this->argument('files'); - $command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard="; - exec( - $command . $this->option('standard') . ' ' . implode(' ', $files), - $output, - $code - ); - foreach ($output as $line) { - $this->line($line); - } + $code = $this->call('lint:phpcs', [ + 'files' => $this->argument('files'), '--fix' => $this->option('fix') + ]); + $code += $this->call('lint:phpmd', [ + 'files' => $this->argument('files') + ]); return $code; } } diff --git a/src/LintPhpcsCommand.php b/src/LintPhpcsCommand.php new file mode 100644 index 0000000..0a4998e --- /dev/null +++ b/src/LintPhpcsCommand.php @@ -0,0 +1,48 @@ +option('fix') ? 'phpcbf' : 'phpcs'; + $files = empty($this->argument('files')) ? ['.'] : $this->argument('files'); + $command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard="; + exec( + $command . $this->option('standard') . ' ' . implode(' ', $files), + $output, + $code + ); + foreach ($output as $line) { + $this->line($line); + } + return $code; + } +} diff --git a/src/LintPmdCommand.php b/src/LintPmdCommand.php new file mode 100644 index 0000000..5cadf6a --- /dev/null +++ b/src/LintPmdCommand.php @@ -0,0 +1,47 @@ +argument('files')) ? ['.'] : $this->argument('files'); + $command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "phpmd "; + exec( + $command . implode(' ', $files) . ' ' . $this->option('format') . ' ' . $this->option('ruleset'), + $output, + $code + ); + foreach ($output as $line) { + $this->line($line); + } + return $code; + } +} diff --git a/src/LintServiceProvider.php b/src/LintServiceProvider.php index 608833a..ba3dad7 100644 --- a/src/LintServiceProvider.php +++ b/src/LintServiceProvider.php @@ -16,6 +16,8 @@ public function boot() if ($this->app->runningInConsole()) { $this->commands([ LintCodeCommand::class, + LintPmdCommand::class, + LintPhpcsCommand::class, LintPublishCommand::class, LintRouteCommand::class, LintStagedCommand::class, diff --git a/tests/LintPhpcsCommandTest.php b/tests/LintPhpcsCommandTest.php new file mode 100644 index 0000000..098d2bc --- /dev/null +++ b/tests/LintPhpcsCommandTest.php @@ -0,0 +1,27 @@ +setNamespace('\\LaravelFans\\Lint') + ->setName("exec") + ->setFunction( + function ($command, &$output, &$code) { + $this->assertEquals("vendor/bin/phpcs --standard=phpcs.xml .", $command); + $output = []; + $code = 0; + }); + $mock = $builder->build(); + $mock->enable(); + $this->artisan('lint:phpcs')->assertExitCode(0); + $mock->disable(); + } +} diff --git a/tests/LintPmdCommandTest.php b/tests/LintPmdCommandTest.php new file mode 100644 index 0000000..9bbcceb --- /dev/null +++ b/tests/LintPmdCommandTest.php @@ -0,0 +1,27 @@ +setNamespace('\\LaravelFans\\Lint') + ->setName("exec") + ->setFunction( + function ($command, &$output, &$code) { + $this->assertEquals("vendor/bin/phpmd . text phpmd.xml", $command); + $output = []; + $code = 1; + }); + $mock = $builder->build(); + $mock->enable(); + $this->artisan('lint:pmd')->assertExitCode(1); + $mock->disable(); + } +} From 5ca57e4cd793e1ba5151f4df97fdb0cffeb3c0d5 Mon Sep 17 00:00:00 2001 From: Sink Date: Mon, 18 Nov 2024 01:37:47 +0800 Subject: [PATCH 2/2] style: #22 fix style --- tests/LintPhpcsCommandTest.php | 9 +++++---- tests/LintPmdCommandTest.php | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/LintPhpcsCommandTest.php b/tests/LintPhpcsCommandTest.php index 098d2bc..85c91d4 100644 --- a/tests/LintPhpcsCommandTest.php +++ b/tests/LintPhpcsCommandTest.php @@ -15,10 +15,11 @@ public function testLintPhpcsWithoutArgs() ->setName("exec") ->setFunction( function ($command, &$output, &$code) { - $this->assertEquals("vendor/bin/phpcs --standard=phpcs.xml .", $command); - $output = []; - $code = 0; - }); + $this->assertEquals("vendor/bin/phpcs --standard=phpcs.xml .", $command); + $output = []; + $code = 0; + } + ); $mock = $builder->build(); $mock->enable(); $this->artisan('lint:phpcs')->assertExitCode(0); diff --git a/tests/LintPmdCommandTest.php b/tests/LintPmdCommandTest.php index 9bbcceb..de4df42 100644 --- a/tests/LintPmdCommandTest.php +++ b/tests/LintPmdCommandTest.php @@ -15,10 +15,11 @@ public function testLintPmdWithoutArgs() ->setName("exec") ->setFunction( function ($command, &$output, &$code) { - $this->assertEquals("vendor/bin/phpmd . text phpmd.xml", $command); - $output = []; - $code = 1; - }); + $this->assertEquals("vendor/bin/phpmd . text phpmd.xml", $command); + $output = []; + $code = 1; + } + ); $mock = $builder->build(); $mock->enable(); $this->artisan('lint:pmd')->assertExitCode(1);