From 0688f165d1d58aebe37015afda109dab93f23e7a Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 11 Jul 2023 09:50:31 -0400 Subject: [PATCH] Use Laravel Pint for code formatting (#141) Co-authored-by: jesseleite --- .github/workflows/pint-fix.yml | 29 ++++++++++++++++++++++ .github/workflows/pint-lint.yml | 20 +++++++++++++++ .gitignore | 1 + pint.json | 11 +++++++++ src/Commands/StaticSiteClear.php | 2 +- src/Commands/StaticSiteGenerate.php | 3 ++- src/Commands/StaticSiteLinks.php | 2 +- src/GeneratedPage.php | 2 +- src/Generator.php | 38 ++++++++++++++++------------- src/NotGeneratedException.php | 6 ++--- src/Page.php | 6 ++--- src/ServiceProvider.php | 3 +-- tests/ConsecutiveTasksTest.php | 2 ++ tests/PageTest.php | 1 - tests/RequestTest.php | 5 ++-- tests/TestCase.php | 2 +- 16 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 .github/workflows/pint-fix.yml create mode 100644 .github/workflows/pint-lint.yml create mode 100644 pint.json diff --git a/.github/workflows/pint-fix.yml b/.github/workflows/pint-fix.yml new file mode 100644 index 0000000..ae003a1 --- /dev/null +++ b/.github/workflows/pint-fix.yml @@ -0,0 +1,29 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + fix-php-code-styling: + runs-on: ubuntu-latest + if: github.repository_owner == 'statamic' + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + token: ${{ secrets.PINT }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@1.0.0 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/pint-lint.yml b/.github/workflows/pint-lint.yml new file mode 100644 index 0000000..87a91b0 --- /dev/null +++ b/.github/workflows/pint-lint.yml @@ -0,0 +1,20 @@ +name: Lint PHP code style issues + +on: + pull_request: + paths: + - '**.php' + +jobs: + lint-php-code-styling: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check PHP code style issues + uses: aglipanci/laravel-pint-action@1.0.0 + with: + testMode: true + verboseMode: true diff --git a/.gitignore b/.gitignore index de4a392..52c8085 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.DS_Store /vendor /composer.lock diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..ce1bb7f --- /dev/null +++ b/pint.json @@ -0,0 +1,11 @@ +{ + "preset": "laravel", + "rules": { + "class_attributes_separation": { + "elements": { + "method": "one" + } + }, + "psr_autoloading": true + } +} diff --git a/src/Commands/StaticSiteClear.php b/src/Commands/StaticSiteClear.php index b8f38d2..6120a0e 100644 --- a/src/Commands/StaticSiteClear.php +++ b/src/Commands/StaticSiteClear.php @@ -3,8 +3,8 @@ namespace Statamic\StaticSite\Commands; use Illuminate\Console\Command; -use Statamic\StaticSite\Generator; use Statamic\Console\RunsInPlease; +use Statamic\StaticSite\Generator; use Wilderborn\Partyline\Facade as Partyline; class StaticSiteClear extends Command diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index a944a8d..91c7b5a 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -3,9 +3,9 @@ namespace Statamic\StaticSite\Commands; use Illuminate\Console\Command; -use Statamic\StaticSite\Generator; use Statamic\Console\RunsInPlease; use Statamic\StaticSite\GenerationFailedException; +use Statamic\StaticSite\Generator; use Wilderborn\Partyline\Facade as Partyline; class StaticSiteGenerate extends Command @@ -63,6 +63,7 @@ public function handle() } catch (GenerationFailedException $e) { $this->line($e->getConsoleMessage()); $this->error('Static site generation failed.'); + return 1; } diff --git a/src/Commands/StaticSiteLinks.php b/src/Commands/StaticSiteLinks.php index b68e178..6bbb796 100644 --- a/src/Commands/StaticSiteLinks.php +++ b/src/Commands/StaticSiteLinks.php @@ -3,8 +3,8 @@ namespace Statamic\StaticSite\Commands; use Illuminate\Console\Command; -use Statamic\StaticSite\Generator; use Statamic\Console\RunsInPlease; +use Statamic\StaticSite\Generator; use Wilderborn\Partyline\Facade as Partyline; class StaticSiteLinks extends Command diff --git a/src/GeneratedPage.php b/src/GeneratedPage.php index fb5c969..6cc4ea4 100644 --- a/src/GeneratedPage.php +++ b/src/GeneratedPage.php @@ -38,7 +38,7 @@ public function consoleMessage() $message = vsprintf('%s%s %s', [ "\x1B[1A\x1B[2K", $this->hasWarning() ? '[!]' : '[✔]', - $this->url() + $this->url(), ]); if ($this->isRedirect()) { diff --git a/src/Generator.php b/src/Generator.php index 3584c4f..195dae4 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -3,27 +3,26 @@ namespace Statamic\StaticSite; use Carbon\Carbon; -use Spatie\Fork\Fork; -use Statamic\Statamic; use Facades\Statamic\View\Cascade; -use Statamic\Facades\URL; -use Statamic\Support\Str; -use Statamic\Facades\Site; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Filesystem\Filesystem; use Illuminate\Routing\Router; use Illuminate\Support\Arr; +use League\Flysystem\Filesystem as Flysystem; +use Spatie\Fork\Fork; +use Statamic\Contracts\Imaging\UrlBuilder; use Statamic\Facades\Collection; use Statamic\Facades\Entry; use Statamic\Facades\Glide; +use Statamic\Facades\Site; use Statamic\Facades\Term; -use League\Flysystem\Adapter\Local; +use Statamic\Facades\URL; +use Statamic\Http\Controllers\FrontendController; use Statamic\Imaging\ImageGenerator; -use Illuminate\Filesystem\Filesystem; use Statamic\Imaging\StaticUrlBuilder; -use Statamic\Contracts\Imaging\UrlBuilder; -use League\Flysystem\Filesystem as Flysystem; +use Statamic\Statamic; +use Statamic\Support\Str; use Wilderborn\Partyline\Facade as Partyline; -use Illuminate\Contracts\Foundation\Application; -use Statamic\Http\Controllers\FrontendController; class Generator { @@ -115,12 +114,12 @@ public function bindGlide() : '\League\Flysystem\Local\LocalFilesystemAdapter'; $this->app['League\Glide\Server']->setCache( - new Flysystem(new $localAdapter($this->config['destination'] . '/' . $directory)) + new Flysystem(new $localAdapter($this->config['destination'].'/'.$directory)) ); $this->app->bind(UrlBuilder::class, function () use ($directory) { return new StaticUrlBuilder($this->app[ImageGenerator::class], [ - 'route' => URL::tidy($this->config['base_url'] . '/' . $directory) + 'route' => URL::tidy($this->config['base_url'].'/'.$directory), ]); }); @@ -137,7 +136,7 @@ public function clearDirectory() public function createSymlinks() { foreach ($this->config['symlinks'] as $source => $dest) { - $dest = $this->config['destination'] . '/' . $dest; + $dest = $this->config['destination'].'/'.$dest; if ($this->files->exists($dest)) { Partyline::line("Symlink not created. $dest already exists."); @@ -153,7 +152,7 @@ public function createSymlinks() public function copyFiles() { foreach ($this->config['copy'] ?? [] as $source => $dest) { - $dest = $this->config['destination'] . '/' . $dest; + $dest = $this->config['destination'].'/'.$dest; if (is_file($source)) { $this->files->copy($source, $dest); @@ -234,7 +233,9 @@ protected function pages() ->reject(function ($page) { foreach ($this->config['exclude'] as $url) { if (Str::endsWith($url, '*')) { - if (Str::is($url, $page->url())) return true; + if (Str::is($url, $page->url())) { + return true; + } } } @@ -274,6 +275,7 @@ protected function makeContentGenerationClosures($pages, $request) } $errors[] = $e->consoleMessage(); + continue; } finally { Carbon::setToStringFormat($oldCarbonFormat); @@ -305,7 +307,7 @@ protected function outputTasksResults() protected function outputSummary() { Partyline::info(''); - Partyline::info('Static site generated into ' . $this->config['destination']); + Partyline::info('Static site generated into '.$this->config['destination']); $total = $this->taskResults['count']; @@ -378,6 +380,7 @@ protected function urls() return collect($this->config['urls'] ?? [])->merge($extra)->map(function ($url) { $url = URL::tidy(Str::start($url, $this->config['base_url'].'/')); + return $this->createPage(new Route($url)); }); } @@ -389,6 +392,7 @@ protected function routes() && ! Str::contains($route->uri(), '{'); })->map(function ($route) { $url = URL::tidy(Str::start($route->uri(), $this->config['base_url'].'/')); + return $this->createPage(new StatamicRoute($url)); }); } diff --git a/src/NotGeneratedException.php b/src/NotGeneratedException.php index 04c0cad..6e630c1 100644 --- a/src/NotGeneratedException.php +++ b/src/NotGeneratedException.php @@ -3,8 +3,8 @@ namespace Statamic\StaticSite; use Facade\Ignition\Exceptions\ViewException; -use Symfony\Component\HttpKernel\Exception\HttpException; use Statamic\Exceptions\NotFoundHttpException as StatamicNotFoundHttpException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as SymfonyNotFoundHttpException; class NotGeneratedException extends \Exception @@ -37,12 +37,12 @@ public function consoleMessage() $message = 'Resulted in 404'; break; case HttpException::class: - $message = 'Resulted in ' . $exception->getStatusCode(); + $message = 'Resulted in '.$exception->getStatusCode(); break; default: $message = $this->getMessage(); } - return sprintf('%s %s (%s)', "[✘]", $this->getPage()->url(), $message); + return sprintf('%s %s (%s)', '[✘]', $this->getPage()->url(), $message); } } diff --git a/src/Page.php b/src/Page.php index b0f5ba9..6c3ff1d 100644 --- a/src/Page.php +++ b/src/Page.php @@ -4,8 +4,8 @@ use Exception; use Illuminate\Filesystem\Filesystem; -use Illuminate\Http\RedirectResponse; use Illuminate\Http\Exceptions\HttpResponseException; +use Illuminate\Http\RedirectResponse; class Page { @@ -67,14 +67,14 @@ public function directory() public function path() { if ($this->is404()) { - return $this->config['destination'] . '/404.html'; + return $this->config['destination'].'/404.html'; } $url = $this->url(); $ext = pathinfo($url, PATHINFO_EXTENSION) ?: 'html'; - $url = $this->config['destination'] . $url; + $url = $this->config['destination'].$url; if ($ext === 'html') { $url .= '/index.html'; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index fd155e2..48d7e12 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,9 +2,8 @@ namespace Statamic\StaticSite; -use Spatie\Fork\Fork; -use Statamic\StaticSite\Generator; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; +use Spatie\Fork\Fork; class ServiceProvider extends LaravelServiceProvider { diff --git a/tests/ConsecutiveTasksTest.php b/tests/ConsecutiveTasksTest.php index b2313ed..126eec7 100644 --- a/tests/ConsecutiveTasksTest.php +++ b/tests/ConsecutiveTasksTest.php @@ -13,11 +13,13 @@ public function it_runs_callbacks() $one = function () use (&$callbacksRan) { $callbacksRan++; + return 'one'; }; $two = function () use (&$callbacksRan) { $callbacksRan++; + return 'two'; }; diff --git a/tests/PageTest.php b/tests/PageTest.php index 2b4ad8d..2da17b6 100644 --- a/tests/PageTest.php +++ b/tests/PageTest.php @@ -3,7 +3,6 @@ namespace Tests; use Illuminate\Filesystem\Filesystem; -use Mockery; use Statamic\Contracts\Entries\Entry; use Statamic\StaticSite\Page; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 2603166..858eb98 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -2,11 +2,10 @@ namespace Tests; -use Mockery; -use Statamic\Facades\Site; +use Statamic\Entries\Collection; use Statamic\Entries\Entry; +use Statamic\Facades\Site; use Statamic\StaticSite\Page; -use Statamic\Entries\Collection; use Statamic\StaticSite\Request; class RequestTest extends TestCase diff --git a/tests/TestCase.php b/tests/TestCase.php index 4423c01..93552a4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,8 +2,8 @@ namespace Tests; -use Statamic\Providers\StatamicServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; +use Statamic\Providers\StatamicServiceProvider; use Statamic\Statamic; class TestCase extends OrchestraTestCase