From 462db8d652071a50aa01e051409725f4eba8ed39 Mon Sep 17 00:00:00 2001 From: Sink Date: Tue, 19 Nov 2024 13:11:21 +0800 Subject: [PATCH] feat: #32 pint --- .github/workflows/laravel-11.yml | 1 + README.md | 7 +++-- composer.json | 1 + src/LintCodeCommand.php | 2 -- src/LintCommand.php | 10 ++++-- src/LintPhpcsCommand.php | 2 -- src/LintPintCommand.php | 53 ++++++++++++++++++++++++++++++++ src/LintPmdCommand.php | 2 -- src/LintPublishCommand.php | 3 ++ src/LintServiceProvider.php | 1 + src/LintStagedCommand.php | 2 -- src/stubs/git-pre-commit | 2 ++ src/stubs/pint.json | 3 ++ tests/LintPhpcsCommandTest.php | 2 -- tests/LintPintCommandTest.php | 26 ++++++++++++++++ tests/LintPmdCommandTest.php | 2 -- tests/LintPublishCommandTest.php | 1 + 17 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 src/LintPintCommand.php create mode 100644 src/stubs/pint.json create mode 100644 tests/LintPintCommandTest.php diff --git a/.github/workflows/laravel-11.yml b/.github/workflows/laravel-11.yml index 128e7e6..5b0e64b 100644 --- a/.github/workflows/laravel-11.yml +++ b/.github/workflows/laravel-11.yml @@ -30,6 +30,7 @@ jobs: run: | ./vendor/bin/phpcs --standard=PSR12 src/ tests/ ./vendor/bin/phpmd . text src/stubs/phpmd.xml + ./vendor/bin/pint --test --config src/stubs/pint.json - name: Upload coverage to Codecov uses: codecov/codecov-action@v4 diff --git a/README.md b/README.md index d683623..477bc03 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ composer require --dev laravel-fans/lint php artisan lint:publish ``` -You will find `phpcs.xml` and `phpmd.xml` in your project, feel free to change it. +You will find `pint.json`, `phpcs.xml` and `phpmd.xml` in your project, feel free to change it. ## usage @@ -35,15 +35,16 @@ php artisan lint:code php artisan lint:code --fix php artisan lint:code app/ tests/ php artisan lint:code app/ tests/ --fix +php artisan lint:pint php artisan lint:phpcs php artisan lint:pmd php artisan lint:staged ``` -### lint route URI +### lint route ```shell php artisan lint:route ``` -Slug(kebab-case) standard: lowercase ASCII letters, digits, and hyphens (a-z, 0–9, -) +Slug(kebab-case) URI standard: lowercase ASCII letters, digits, and hyphens (a-z, 0–9, -) diff --git a/composer.json b/composer.json index beb58c4..35d8ec1 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "require": { "ext-json": "*", "illuminate/support": ">=v9", + "laravel/pint": ">=v1", "phpmd/phpmd": ">=2.10", "squizlabs/php_codesniffer": ">=3.5" }, diff --git a/src/LintCodeCommand.php b/src/LintCodeCommand.php index 7c98539..6cb0c72 100644 --- a/src/LintCodeCommand.php +++ b/src/LintCodeCommand.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint; -use FilesystemIterator; use Illuminate\Console\Command; -use Illuminate\Support\Facades\File; class LintCodeCommand extends Command { diff --git a/src/LintCommand.php b/src/LintCommand.php index 8adf42d..5d1516b 100644 --- a/src/LintCommand.php +++ b/src/LintCommand.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint; -use FilesystemIterator; use Illuminate\Console\Command; -use Illuminate\Support\Facades\File; class LintCommand extends Command { @@ -34,7 +32,15 @@ public function handle() $code = $this->call('lint:phpcs', [ 'files' => $this->argument('files'), '--fix' => $this->option('fix') ]); + if ($this->option('fix')) { + $code = $this->call('lint:pint', [ + 'files' => $this->argument('files'), '--repair' => true + ]); + } if (!$this->option('fix')) { + $code = $this->call('lint:pint', [ + 'files' => $this->argument('files'), '--test' => true + ]); $code += $this->call('lint:pmd', [ 'files' => $this->argument('files') ]); diff --git a/src/LintPhpcsCommand.php b/src/LintPhpcsCommand.php index 0a4998e..e5a4ed1 100644 --- a/src/LintPhpcsCommand.php +++ b/src/LintPhpcsCommand.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint; -use FilesystemIterator; use Illuminate\Console\Command; -use Illuminate\Support\Facades\File; class LintPhpcsCommand extends Command { diff --git a/src/LintPintCommand.php b/src/LintPintCommand.php new file mode 100644 index 0000000..f1a3452 --- /dev/null +++ b/src/LintPintCommand.php @@ -0,0 +1,53 @@ +argument('files')) ? ['.'] : $this->argument('files'); + $command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "pint"; + $command .= " --config=" . $this->option('config'); + if ($this->option('test')) { + $command .= ' --test'; + } + if ($this->option('repair')) { + $command .= ' --repair'; + } + exec( + $command . ' ' . implode(' ', $files), + $output, + $code + ); + foreach ($output as $line) { + $this->line($line); + } + return $code; + } +} diff --git a/src/LintPmdCommand.php b/src/LintPmdCommand.php index 5cadf6a..a03b865 100644 --- a/src/LintPmdCommand.php +++ b/src/LintPmdCommand.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint; -use FilesystemIterator; use Illuminate\Console\Command; -use Illuminate\Support\Facades\File; class LintPmdCommand extends Command { diff --git a/src/LintPublishCommand.php b/src/LintPublishCommand.php index b08ff74..c39bb96 100644 --- a/src/LintPublishCommand.php +++ b/src/LintPublishCommand.php @@ -30,6 +30,9 @@ public function handle() { $basePath = $this->laravel->basePath(); + if (!File::exists($basePath . '/pint.json')) { + File::copy(__DIR__ . '/stubs/pint.json', $basePath . '/pint.json'); + } if (!File::exists($basePath . '/phpcs.xml')) { File::copy(__DIR__ . '/stubs/phpcs.xml', $basePath . '/phpcs.xml'); } diff --git a/src/LintServiceProvider.php b/src/LintServiceProvider.php index 274bd9f..ea5be73 100644 --- a/src/LintServiceProvider.php +++ b/src/LintServiceProvider.php @@ -17,6 +17,7 @@ public function boot() $this->commands([ LintCommand::class, LintCodeCommand::class, + LintPintCommand::class, LintPmdCommand::class, LintPhpcsCommand::class, LintPublishCommand::class, diff --git a/src/LintStagedCommand.php b/src/LintStagedCommand.php index ae53150..5a84180 100644 --- a/src/LintStagedCommand.php +++ b/src/LintStagedCommand.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint; -use FilesystemIterator; use Illuminate\Console\Command; -use Illuminate\Support\Facades\File; class LintStagedCommand extends Command { diff --git a/src/stubs/git-pre-commit b/src/stubs/git-pre-commit index 6a065e4..938104d 100755 --- a/src/stubs/git-pre-commit +++ b/src/stubs/git-pre-commit @@ -2,6 +2,8 @@ FILES=$(git diff --diff-filter=d --name-only HEAD | { grep '.php$' || true; }) for file in $FILES; do + ./vendor/bin/phpcs --config=pint.json --repair "$file" + ./vendor/bin/phpcbf --extensions=php --standard=phpcs.xml "$file" ./vendor/bin/phpcs --extensions=php --standard=phpcs.xml "$file" ./vendor/bin/phpmd "$file" text phpmd.xml done diff --git a/src/stubs/pint.json b/src/stubs/pint.json new file mode 100644 index 0000000..ea5e72c --- /dev/null +++ b/src/stubs/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "psr12" +} diff --git a/tests/LintPhpcsCommandTest.php b/tests/LintPhpcsCommandTest.php index 85c91d4..97a7d8e 100644 --- a/tests/LintPhpcsCommandTest.php +++ b/tests/LintPhpcsCommandTest.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint\Tests; -use Illuminate\Support\Facades\File; use phpmock\MockBuilder; -use phpmock\functions\FixedValueFunction; class LintPhpcsCommandTest extends TestCase { diff --git a/tests/LintPintCommandTest.php b/tests/LintPintCommandTest.php new file mode 100644 index 0000000..fc04134 --- /dev/null +++ b/tests/LintPintCommandTest.php @@ -0,0 +1,26 @@ +setNamespace('\\LaravelFans\\Lint') + ->setName("exec") + ->setFunction( + function ($command, &$output, &$code) { + $this->assertEquals("vendor/bin/pint --config=pint.json .", $command); + $output = []; + $code = 0; + } + ); + $mock = $builder->build(); + $mock->enable(); + $this->artisan('lint:pint')->assertExitCode(0); + $mock->disable(); + } +} diff --git a/tests/LintPmdCommandTest.php b/tests/LintPmdCommandTest.php index de4df42..6bc3176 100644 --- a/tests/LintPmdCommandTest.php +++ b/tests/LintPmdCommandTest.php @@ -2,9 +2,7 @@ namespace LaravelFans\Lint\Tests; -use Illuminate\Support\Facades\File; use phpmock\MockBuilder; -use phpmock\functions\FixedValueFunction; class LintPmdCommandTest extends TestCase { diff --git a/tests/LintPublishCommandTest.php b/tests/LintPublishCommandTest.php index 643919c..e73efef 100644 --- a/tests/LintPublishCommandTest.php +++ b/tests/LintPublishCommandTest.php @@ -21,6 +21,7 @@ public function testGitExists() $laravelPath = __DIR__ . '/../vendor/orchestra/testbench-core/laravel'; File::makeDirectory($laravelPath . '/.git/hooks/', 0755, true); $this->artisan('lint:publish')->run(); + $this->assertFileExists($laravelPath . '/pint.json'); $this->assertFileExists($laravelPath . '/phpcs.xml'); $this->assertFileExists($laravelPath . '/phpmd.xml'); $this->assertFileExists($laravelPath . '/.git/hooks/pre-commit');