From ee042b479bd5c5ccded31f3ea5b043ddadcf502f Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Tue, 12 Nov 2024 13:18:06 +0000 Subject: [PATCH] PHPStan level 1 (#620) --- .github/workflows/analyse.yml | 42 ++++++++++++++++++++++++++ composer.json | 3 ++ config/rapidez/models.php | 1 + phpstan.neon | 12 ++++++++ src/Actions/DecodeJwt.php | 3 +- src/Auth/MagentoCartTokenGuard.php | 2 +- src/Auth/MagentoCustomerTokenGuard.php | 2 +- src/Casts/QuoteItems.php | 0 src/ContentVariables/Widget.php | 4 ++- src/Facades/Rapidez.php | 1 + src/Models/Customer.php | 2 +- src/Models/Product.php | 2 +- src/Models/ProductReviewSummary.php | 2 +- src/Rapidez.php | 1 + tests/DuskTestCaseSetup.php | 3 +- 15 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/analyse.yml create mode 100644 phpstan.neon delete mode 100644 src/Casts/QuoteItems.php diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml new file mode 100644 index 000000000..34298a876 --- /dev/null +++ b/.github/workflows/analyse.yml @@ -0,0 +1,42 @@ +name: analyse + +on: + push: + branches: + - master + - '*.x' + pull_request: + +jobs: + analyse: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: [8.3] + stability: [prefer-stable] + include: + - laravel: 10.* + testbench: 8.* + - laravel: 11.* + testbench: 9.* + + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + token: ${{ secrets.RAPIDEZ_ACTIONS_ACCOUNT_PAT }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + 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 require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + - name: Analyse + run: composer analyse diff --git a/composer.json b/composer.json index b8e7d5e0b..be1cca24b 100644 --- a/composer.json +++ b/composer.json @@ -29,9 +29,11 @@ "tormjens/eventy": "^0.8" }, "require-dev": { + "larastan/larastan": "^2.9", "laravel/dusk": "^8.2", "orchestra/testbench": "^9.4", "orchestra/testbench-dusk": "^9.7", + "phpstan/phpstan": "^1.12", "phpunit/phpunit": "^10.5.34|^11.3.5" }, "autoload": { @@ -61,6 +63,7 @@ } }, "scripts": { + "analyse": "phpstan --memory-limit=256M", "dusk:prepare": [ "./vendor/bin/dusk-updater detect --auto-update", "@php -r \"file_exists('phpunit.dusk.xml') || copy('phpunit.dusk.xml.dist', 'phpunit.dusk.xml'); \"" diff --git a/config/rapidez/models.php b/config/rapidez/models.php index 4782a6300..864efc62c 100644 --- a/config/rapidez/models.php +++ b/config/rapidez/models.php @@ -6,6 +6,7 @@ 'attribute' => Rapidez\Core\Models\Attribute::class, 'product' => Rapidez\Core\Models\Product::class, 'category' => Rapidez\Core\Models\Category::class, + 'customer_group' => Rapidez\Core\Models\CustomerGroup::class, 'category_product' => Rapidez\Core\Models\CategoryProduct::class, 'customer' => Rapidez\Core\Models\Customer::class, 'config' => Rapidez\Core\Models\Config::class, diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 000000000..1b0abc9c6 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,12 @@ +includes: + - ./vendor/larastan/larastan/extension.neon + +parameters: + paths: + - src/ + - tests/ + - routes/ + ignoreErrors: + - '#^Result of static method TorMorten\\Eventy\\Events::#' + - "#Access to an undefined property#" + level: 1 diff --git a/src/Actions/DecodeJwt.php b/src/Actions/DecodeJwt.php index 24037cb4c..28a9d1e02 100644 --- a/src/Actions/DecodeJwt.php +++ b/src/Actions/DecodeJwt.php @@ -15,6 +15,7 @@ use Lcobucci\JWT\Validation\Constraint\SignedWith; use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Rapidez\Core\Exceptions\DecryptionException; class DecodeJwt { @@ -39,7 +40,7 @@ public static function decode(string $jwt): UnencryptedToken } } - throw $exception; + throw $exception ?? new DecryptionException('No crypt keys defined.'); } /** diff --git a/src/Auth/MagentoCartTokenGuard.php b/src/Auth/MagentoCartTokenGuard.php index a866cd67f..db7facb9e 100644 --- a/src/Auth/MagentoCartTokenGuard.php +++ b/src/Auth/MagentoCartTokenGuard.php @@ -16,7 +16,7 @@ public static function register() }); auth()->extend('magento-cart', function (Application $app, string $name, array $config) { - return new static(auth()->createUserProvider($config['provider']), request(), 'mask', 'mask'); + return new self(auth()->createUserProvider($config['provider']), request(), 'mask', 'mask'); }); config([ diff --git a/src/Auth/MagentoCustomerTokenGuard.php b/src/Auth/MagentoCustomerTokenGuard.php index cb6fc47f7..d1afe3ca5 100644 --- a/src/Auth/MagentoCustomerTokenGuard.php +++ b/src/Auth/MagentoCustomerTokenGuard.php @@ -17,7 +17,7 @@ public static function register() }); auth()->extend('magento-customer', function (Application $app, string $name, array $config) { - return new static(auth()->createUserProvider($config['provider']), request(), 'token', 'token'); + return new self(auth()->createUserProvider($config['provider']), request(), 'token', 'token'); }); config([ diff --git a/src/Casts/QuoteItems.php b/src/Casts/QuoteItems.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/ContentVariables/Widget.php b/src/ContentVariables/Widget.php index 7df511ddf..a9f76c25c 100644 --- a/src/ContentVariables/Widget.php +++ b/src/ContentVariables/Widget.php @@ -9,12 +9,14 @@ public function __invoke($content) return preg_replace_callback('/{{widget type="(.*?)" (.*?)}}/ms', function ($matches) { [$full, $type, $parameters] = $matches; preg_match_all('/(.*?)="(.*?)"/ms', $parameters, $parameters, PREG_SET_ORDER); + + $options = []; foreach ($parameters as $parameter) { [$full, $parameter, $value] = $parameter; $options[trim($parameter)] = trim($value); } - if (! isset($type)) { + if (! $type) { return ''; } diff --git a/src/Facades/Rapidez.php b/src/Facades/Rapidez.php index 274472877..a605ff5e9 100644 --- a/src/Facades/Rapidez.php +++ b/src/Facades/Rapidez.php @@ -14,6 +14,7 @@ * @method static array getStores($storeId = null) * @method static array getStore($storeId) * @method static void setStore($store) + * @method static mixed withStore(\Rapidez\Core\Models\Store|array|callable|int|string $store, callable $callback, mixed ...$args) * * @see \Rapidez\Core\Rapidez */ diff --git a/src/Models/Customer.php b/src/Models/Customer.php index 418302db8..e1210f607 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -29,7 +29,7 @@ public function oauthTokens() public function group() { - return $this->belongsTo(CustomerGroup::class, 'group_id'); + return $this->belongsTo(config('rapidez.models.customer_group'), 'group_id'); } public function getRememberTokenName() diff --git a/src/Models/Product.php b/src/Models/Product.php index a5b3971fb..d37d64a3c 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -122,7 +122,7 @@ public function categoryProducts(): HasMany public function reviewSummary(): HasOne { return $this->hasOne( - config('rapidez.models.product_review_summary', Rapidez\Core\Models\ProductReviewSummary::class), + config('rapidez.models.product_review_summary', \Rapidez\Core\Models\ProductReviewSummary::class), 'entity_pk_value' ); } diff --git a/src/Models/ProductReviewSummary.php b/src/Models/ProductReviewSummary.php index 88ef9dc69..e1a0491dd 100644 --- a/src/Models/ProductReviewSummary.php +++ b/src/Models/ProductReviewSummary.php @@ -27,7 +27,7 @@ protected static function booting(): void public function product(): BelongsTo { return $this->belongsTo( - config('rapidez.models.product', Rapidez\Core\Models\Product::class), + config('rapidez.models.product', \Rapidez\Core\Models\Product::class), 'entity_pk_value' ); } diff --git a/src/Rapidez.php b/src/Rapidez.php index 911e8f6b7..dc1024ecc 100644 --- a/src/Rapidez.php +++ b/src/Rapidez.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; +use Rapidez\Core\Exceptions\StoreNotFoundException; use Rapidez\Core\Models\Store; class Rapidez diff --git a/tests/DuskTestCaseSetup.php b/tests/DuskTestCaseSetup.php index 39e59f011..1556d074e 100644 --- a/tests/DuskTestCaseSetup.php +++ b/tests/DuskTestCaseSetup.php @@ -39,7 +39,7 @@ protected function setUp(): void Browser::macro('waitUntilIdle', function ($timeout = 120) { /** @var Browser $this */ - $this->waitUntilTrueForDuration('window.app?.$data?.loading !== true && await new Promise((resolve, reject) => window.requestIdleCallback((deadline) => resolve(!deadline.didTimeout), {timeout: 5}))', $timeout); + $this->waitUntilTrueForDuration('window.app?.$data?.loading !== true && await new Promise((resolve, reject) => window.requestIdleCallback((deadline) => resolve(!deadline.didTimeout), {timeout: 5}))', $timeout); // @phpstan-ignore-line return $this; }); @@ -63,6 +63,7 @@ protected function setUp(): void ->visit($productUrl); } + // @phpstan-ignore-next-line $this ->waitUntilIdle() ->pressAndWaitFor('@add-to-cart', 60)