From 4bbe163aee9c2920d91e9c1497b380a61793b5c4 Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 8 Aug 2024 15:06:58 +0200 Subject: [PATCH] PHPSTAN level 4 --- .github/workflows/analyse.yml | 23 +++++++++++++++++++ composer.json | 10 ++++++++ phpstan.neon | 9 ++++++++ src/Actions/StatamicEntryAction.php | 5 +++- src/Commands/ImportBrands.php | 1 + src/Commands/ImportCategories.php | 1 + src/Commands/ImportProducts.php | 1 + src/Commands/InstallCommand.php | 6 ++--- src/Extend/SitesLinkedToMagentoStores.php | 16 +++++++++---- src/Http/Controllers/ImportsController.php | 7 +++--- .../Controllers/StatamicRewriteController.php | 2 +- .../StatamicGlobalDataComposer.php | 6 ++++- src/RapidezStatamicServiceProvider.php | 17 +++++++++----- src/Tags/Alternates.php | 6 ++++- 14 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/analyse.yml create mode 100644 phpstan.neon diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml new file mode 100644 index 0000000..bf70a1a --- /dev/null +++ b/.github/workflows/analyse.yml @@ -0,0 +1,23 @@ +name: analyse + +on: ['push', 'pull_request'] + +jobs: + analyse: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Install dependencies + run: | + composer update --prefer-stable --prefer-dist --no-interaction + - name: Analyse + run: composer analyse \ No newline at end of file diff --git a/composer.json b/composer.json index 9c9b117..99407ea 100644 --- a/composer.json +++ b/composer.json @@ -31,11 +31,16 @@ "prefer-stable": true, "require": { "php": "^8.1|^8.2", + "laravel/pint": "^1.7", + "larastan/larastan": "^2.5", + "phpstan/phpstan-mockery": "^1.1", "justbetter/statamic-eloquent-driver-globalset-migration-generator": "^0.1.0", "rapidez/blade-directives": "^0.6", "spatie/statamic-responsive-images": "^5.0", "statamic-rad-pack/runway": "^7.6", + "orchestra/testbench": "^8.0|^9.0", "statamic/cms": "^5.0", + "rapidez/core": "^2.0", "statamic/eloquent-driver": "^4.9", "tormjens/eventy": "^0.8" }, @@ -56,5 +61,10 @@ "Rapidez\\Statamic\\RapidezStatamicServiceProvider" ] } + }, + "scripts": { + "analyse": "phpstan", + "fix-style": "pint", + "style": "pint --test" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..182066c --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +includes: + - ./vendor/larastan/larastan/extension.neon + - ./vendor/phpstan/phpstan-mockery/extension.neon + +parameters: + paths: + - src + level: 4 + checkMissingIterableValueType: false \ No newline at end of file diff --git a/src/Actions/StatamicEntryAction.php b/src/Actions/StatamicEntryAction.php index e4ac214..5ff3295 100644 --- a/src/Actions/StatamicEntryAction.php +++ b/src/Actions/StatamicEntryAction.php @@ -2,6 +2,7 @@ namespace Rapidez\Statamic\Actions; +use Statamic\Eloquent\Entries\EntryQueryBuilder; use Statamic\Facades\Entry; use ReflectionClass; @@ -9,7 +10,9 @@ class StatamicEntryAction { public static function createEntry(array $attributes, array $values = []): void { - if (Entry::query()->where($attributes)->count()) { + /** @var EntryQueryBuilder $entry */ + $entry = Entry::query(); + if ($entry->where($attributes)->count()) { // Entry was already created. return; } diff --git a/src/Commands/ImportBrands.php b/src/Commands/ImportBrands.php index 3b5d52c..2050dd9 100644 --- a/src/Commands/ImportBrands.php +++ b/src/Commands/ImportBrands.php @@ -59,6 +59,7 @@ public function handle(StatamicEntryAction $statamicEntryAction): int 'locale' => $site->handle(), 'site' => $site->handle(), ...$extraData, + // @phpstan-ignore staticMethod.void ], ...[Eventy::filter('rapidez.statamic.brand.entry.data', $brand)]) ); } diff --git a/src/Commands/ImportCategories.php b/src/Commands/ImportCategories.php index 2921646..fc72f49 100644 --- a/src/Commands/ImportCategories.php +++ b/src/Commands/ImportCategories.php @@ -68,6 +68,7 @@ public function handle(): int array_merge([ 'locale' => $site->handle(), 'site' => $site->handle(), + // @phpstan-ignore staticMethod.void ], ...[Eventy::filter('rapidez.statamic.category.entry.data', $category)]) ); } diff --git a/src/Commands/ImportProducts.php b/src/Commands/ImportProducts.php index 5b76ec6..59a89c2 100644 --- a/src/Commands/ImportProducts.php +++ b/src/Commands/ImportProducts.php @@ -53,6 +53,7 @@ public function handle() array_merge([ 'locale' => $site->handle(), 'site' => $site->handle(), + // @phpstan-ignore staticMethod.void ], ...[Eventy::filter('rapidez.statamic.product.entry.data', $product)]) ); } diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index f66718d..4b9ae4a 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -21,7 +21,7 @@ public function handle(): void '--tag' => 'statamic-eloquent-config', ]); - $this->confirm('Did you set the table_prefix in config/statamic/eloquent-driver.php to "statamic_"?', 1); + $this->confirm('Did you set the table_prefix in config/statamic/eloquent-driver.php to "statamic_"?', true); $this->call('statamic:install:eloquent-driver'); @@ -31,7 +31,7 @@ public function handle(): void $this->call('statamic:multisite'); } - if ($this->confirm('Would you like to publish Collections, Blueprints, Fieldsets & Views?', 1)) { + if ($this->confirm('Would you like to publish Collections, Blueprints, Fieldsets & Views?', true)) { $this->call('vendor:publish', [ '--provider' => 'Rapidez\Statamic\RapidezStatamicServiceProvider', '--tag' => 'rapidez-statamic-content', @@ -43,7 +43,7 @@ public function handle(): void ]); } - if ($this->confirm('Would you like to make a user?', 1)) { + if ($this->confirm('Would you like to make a user?', true)) { $this->call('statamic:make:user'); } diff --git a/src/Extend/SitesLinkedToMagentoStores.php b/src/Extend/SitesLinkedToMagentoStores.php index cef143c..22e153d 100644 --- a/src/Extend/SitesLinkedToMagentoStores.php +++ b/src/Extend/SitesLinkedToMagentoStores.php @@ -2,13 +2,19 @@ namespace Rapidez\Statamic\Extend; +use Statamic\Sites\Site; use Statamic\Sites\Sites; +use Illuminate\Support\Collection; class SitesLinkedToMagentoStores extends Sites { - public function findByUrl($url) + /** + * @param string $url + * @return Site + */ + public function findByUrl($url): Site { - if ($site = $this->findByMageRunCode(request()->server('MAGE_RUN_CODE'))) { + if ($site = $this->findByMageRunCode((string) request()->server('MAGE_RUN_CODE'))) { return $site; } @@ -16,8 +22,10 @@ public function findByUrl($url) } - public function findByMageRunCode($code) + public function findByMageRunCode(string $code): Site|null { - return collect($this->sites)->get($code); + $sites = collect($this->sites); + + return $sites->get($code); } } diff --git a/src/Http/Controllers/ImportsController.php b/src/Http/Controllers/ImportsController.php index f7d21a0..7545bfb 100644 --- a/src/Http/Controllers/ImportsController.php +++ b/src/Http/Controllers/ImportsController.php @@ -2,6 +2,7 @@ namespace Rapidez\Statamic\Http\Controllers; +use Illuminate\Routing\Redirector; use Statamic\Facades\CP\Toast; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; @@ -15,7 +16,7 @@ public function __invoke() : View return view('rapidez-statamic::utilities.import_utility.imports'); } - public function importCategories() : RedirectResponse + public function importCategories() : RedirectResponse|Redirector { Artisan::queue('rapidez:statamic:import:categories --all') ->onQueue('imports'); @@ -25,7 +26,7 @@ public function importCategories() : RedirectResponse return redirect(cp_route('utilities.imports')); } - public function importProducts() : RedirectResponse + public function importProducts() : RedirectResponse|Redirector { Artisan::queue('rapidez:statamic:import:products') ->onQueue('imports'); @@ -35,7 +36,7 @@ public function importProducts() : RedirectResponse return redirect(cp_route('utilities.imports')); } - public function importBrands() : RedirectResponse + public function importBrands() : RedirectResponse|Redirector { Artisan::queue('rapidez:statamic:import:brands') ->onQueue('imports'); diff --git a/src/Http/Controllers/StatamicRewriteController.php b/src/Http/Controllers/StatamicRewriteController.php index 43e4e09..1daf971 100644 --- a/src/Http/Controllers/StatamicRewriteController.php +++ b/src/Http/Controllers/StatamicRewriteController.php @@ -10,7 +10,7 @@ class StatamicRewriteController { - public function __invoke(Request $request) + public function __invoke(Request $request): string { return (new FrontendController)->index($request); } diff --git a/src/Http/ViewComposers/StatamicGlobalDataComposer.php b/src/Http/ViewComposers/StatamicGlobalDataComposer.php index e49a402..5a3313b 100644 --- a/src/Http/ViewComposers/StatamicGlobalDataComposer.php +++ b/src/Http/ViewComposers/StatamicGlobalDataComposer.php @@ -4,12 +4,16 @@ use Illuminate\Support\Facades\Cache; use Illuminate\View\View; +use Statamic\Eloquent\Globals\Variables; use Statamic\Facades\GlobalSet; use Statamic\Facades\Site; class StatamicGlobalDataComposer { - public function getGlobals() + /** + * @return array + */ + public function getGlobals(): array { return Cache::rememberForever('statamic-globals-'.Site::current()->handle(), function() { $data = []; diff --git a/src/RapidezStatamicServiceProvider.php b/src/RapidezStatamicServiceProvider.php index be77fcb..de7d498 100644 --- a/src/RapidezStatamicServiceProvider.php +++ b/src/RapidezStatamicServiceProvider.php @@ -2,6 +2,7 @@ namespace Rapidez\Statamic; +use Statamic\Eloquent\Entries\EntryQueryBuilder; use Statamic\Statamic; use Statamic\Sites\Sites; use Statamic\Facades\Site; @@ -30,14 +31,14 @@ class RapidezStatamicServiceProvider extends ServiceProvider { - public function register() + public function register(): void { - $this->app->extend(Sites::class, function () { - return new SitesLinkedToMagentoStores(config('statamic.sites')); + $this->app->extend(Sites::class, function (): SitesLinkedToMagentoStores { + return new SitesLinkedToMagentoStores(); }); } - public function boot() + public function boot(): void { $this ->bootCommands() @@ -133,7 +134,9 @@ public function bootComposers() : self { if (config('rapidez.statamic.fetch.product') && $this->currentSiteIsEnabled()) { View::composer('rapidez::product.overview', function (RenderedView $view) { - $entry = Entry::query() + /** @var EntryQueryBuilder $entry */ + $entry = Entry::query(); + $entry = $entry ->where('collection', 'products') ->where('site', $this->getSiteHandleByStoreId()) ->where('linked_product', config('frontend.product.sku')) @@ -145,7 +148,9 @@ public function bootComposers() : self if (config('rapidez.statamic.fetch.category') && $this->currentSiteIsEnabled()) { View::composer('rapidez::category.overview', function (RenderedView $view) { - $entry = Entry::query() + /** @var EntryQueryBuilder $entry */ + $entry = Entry::query(); + $entry = $entry ->where('collection', 'categories') ->where('site', $this->getSiteHandleByStoreId()) ->where('linked_category', config('frontend.category.entity_id')) diff --git a/src/Tags/Alternates.php b/src/Tags/Alternates.php index aae0875..2c82233 100644 --- a/src/Tags/Alternates.php +++ b/src/Tags/Alternates.php @@ -2,12 +2,16 @@ namespace Rapidez\Statamic\Tags; +use Illuminate\Support\Collection; use Statamic\Facades\Config; use Statamic\Tags\Tags; class Alternates extends Tags { - public function index() + /** + * @return Collection + */ + public function index(): Collection { $page = $this->params->get('page'); $isHome = request()->path() == '/';