Skip to content

Commit

Permalink
PHPSTAN level 4
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 committed Aug 8, 2024
1 parent 1670d5e commit 4bbe163
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 20 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -56,5 +61,10 @@
"Rapidez\\Statamic\\RapidezStatamicServiceProvider"
]
}
},
"scripts": {
"analyse": "phpstan",
"fix-style": "pint",
"style": "pint --test"
}
}
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:
paths:
- src
level: 4
checkMissingIterableValueType: false
5 changes: 4 additions & 1 deletion src/Actions/StatamicEntryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Rapidez\Statamic\Actions;

use Statamic\Eloquent\Entries\EntryQueryBuilder;
use Statamic\Facades\Entry;
use ReflectionClass;

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;
}
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportBrands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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',
Expand All @@ -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');
}

Expand Down
16 changes: 12 additions & 4 deletions src/Extend/SitesLinkedToMagentoStores.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

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;
}

return parent::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);
}
}
7 changes: 4 additions & 3 deletions src/Http/Controllers/ImportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/StatamicRewriteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class StatamicRewriteController
{
public function __invoke(Request $request)
public function __invoke(Request $request): string
{
return (new FrontendController)->index($request);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Http/ViewComposers/StatamicGlobalDataComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Variables>
*/
public function getGlobals(): array
{
return Cache::rememberForever('statamic-globals-'.Site::current()->handle(), function() {
$data = [];
Expand Down
17 changes: 11 additions & 6 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rapidez\Statamic;

use Statamic\Eloquent\Entries\EntryQueryBuilder;
use Statamic\Statamic;
use Statamic\Sites\Sites;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'))
Expand All @@ -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'))
Expand Down
6 changes: 5 additions & 1 deletion src/Tags/Alternates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
public function index(): Collection
{
$page = $this->params->get('page');
$isHome = request()->path() == '/';
Expand Down

0 comments on commit 4bbe163

Please sign in to comment.