Skip to content

Commit

Permalink
Added phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL authored Jul 23, 2024
1 parent 84e3fa1 commit f40caed
Show file tree
Hide file tree
Showing 38 changed files with 309 additions and 59 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests PHPStan in environments

on: [pull_request]

jobs:
php82-laravel-latest-phpstan-postgres:
runs-on: ubuntu-latest
container:
image: escolalms/php:8.2

services:
postgres:
image: postgres:12
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
TZ: Europe/Warsaw
ports:
- 5432:5432

steps:
- name: Instantiate package
uses: actions/checkout@v2

- name: Setup environment
run: cp env/postgres/* .

- name: Update composer
run: COMPOSER_ROOT_VERSION=0.9.9 composer update

- name: Clear config
run: vendor/bin/testbench config:clear

- name: Publish things
run: vendor/bin/testbench migrate:fresh

- name: Run tests
run: vendor/bin/phpstan analyse
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.12",
"escolalms/settings": "^0.1.91",
"nunomaduro/collision": "^5.11",
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^9.0"
"nunomaduro/collision": ">=5.11",
"orchestra/testbench": ">=5.0",
"phpunit/phpunit": "^9.0",
"nunomaduro/larastan": "^2.0"
},
"suggest": {},
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src/

# The level 9 is the highest level
level: 6
11 changes: 7 additions & 4 deletions src/EscolaLmsVouchersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class EscolaLmsVouchersServiceProvider extends ServiceProvider
{
const CONFIG_KEY = 'escolalms_vouchers';

public $singletons = [
/**
* @var array<class-string, class-string>
*/
public array $singletons = [
CouponServiceContract::class => CouponService::class,
OrderServiceContract::class => OrderService::class,
ShopServiceContract::class => ShopService::class,
];

public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/config.php', self::CONFIG_KEY);

Expand All @@ -40,7 +43,7 @@ public function register()
$this->app->register(AuthServiceProvider::class);
}

public function boot()
public function boot(): void
{
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'coupon');
Expand All @@ -53,7 +56,7 @@ public function boot()
}
}

public function bootForConsole()
public function bootForConsole(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/VouchersAdminApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index(ListCouponsRequest $request): JsonResponse
$orderDto = OrderDto::instantiateFromRequest($request);
$searchCouponsDto = $request->toDto();
$paginatedResults = $this->couponsService->searchAndPaginateCoupons($searchCouponsDto, $orderDto);
return $this->sendResponseForResource(CouponResource::collection($paginatedResults, __('Coupons search results')));
return $this->sendResponseForResource(CouponResource::collection($paginatedResults), ('Coupons search results'));
}

public function create(CreateCouponRequest $request): JsonResponse
Expand Down
9 changes: 7 additions & 2 deletions src/Http/Controllers/VouchersApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Vouchers\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use EscolaLms\Vouchers\Exceptions\CouponInactiveException;
use EscolaLms\Vouchers\Exceptions\CouponNotApplicableException;
use EscolaLms\Vouchers\Http\Controllers\Swagger\VouchersApiControllerSwagger;
Expand All @@ -23,7 +24,9 @@ public function __construct(ShopServiceContract $shopService)
public function apply(ApplyCouponRequest $request): JsonResponse
{
try {
$cart = $this->shopService->cartForUser($request->user());
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
$cartManager = $cart->cart_manager;
$cartManager->setCoupon($request->getCoupon());
} catch (CouponInactiveException $ex) {
Expand All @@ -36,7 +39,9 @@ public function apply(ApplyCouponRequest $request): JsonResponse

public function unapply(UnapplyCouponRequest $request): JsonResponse
{
$cart = $this->shopService->cartForUser($request->user());
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
$cartManager = $cart->cart_manager;
$cartManager->removeCoupon();
return $this->sendSuccess(__("Coupon removed from cart"));
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/ApplyCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function authorize(): bool
return Gate::allows('apply', $this->getCoupon());
}

/**
* @return array<string, string>
*/
public function rules(): array
{
return [
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Requests/CreateCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function authorize(): bool
return Gate::allows('create', Coupon::class);
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
Expand Down Expand Up @@ -47,7 +50,7 @@ public function rules(): array
];
}

public function withValidator(Validator $validator)
public function withValidator(Validator $validator): void
{
$validator->sometimes('amount', 'max:100', function ($input) {
return $input->type === CouponTypeEnum::CART_PERCENT || $input->type === CouponTypeEnum::PRODUCT_PERCENT;
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/DeleteCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function authorize(): bool
return Gate::allows('delete', $this->getCoupon());
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [];
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/ListCouponsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function authorize(): bool
return Gate::allows('viewAny', Coupon::class);
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/ReadCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function authorize(): bool
return Gate::allows('view', $this->getCoupon());
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [];
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/UnapplyCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function authorize(): bool
return Gate::allows('unapply', Coupon::class);
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [];
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Requests/UpdateCouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function authorize(): bool
return Gate::allows('update', $this->getCoupon());
}

/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
Expand Down Expand Up @@ -47,7 +50,7 @@ public function rules(): array
];
}

public function withValidator(Validator $validator)
public function withValidator(Validator $validator): void
{
$validator->sometimes('amount', 'max:100', function ($input) {
return $input->type === CouponTypeEnum::CART_PERCENT || $input->type === CouponTypeEnum::PRODUCT_PERCENT;
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Resources/CartItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ protected function getCartItem(): CartItem
return $this->resource;
}

/**
* @param $request
* @return array<string, mixed>
*/
public function toArray($request): array
{
return array_merge(parent::toArray($request), [
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Resources/CartResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ protected function getCartItemsResourceCollection(): ResourceCollection
return CartItemResource::collection($this->getCart()->items);
}

/**
* @param $request
* @return array<string, mixed>
*/
public function toArray($request): array
{
return array_merge(parent::toArray($request), [
Expand Down
12 changes: 8 additions & 4 deletions src/Http/Resources/CouponProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public function __construct(CouponProduct $couponProduct)
parent::__construct($couponProduct);
}

public function toArray($request)
/**
* @param $request
* @return array<string, mixed>
*/
public function toArray($request): array
{
return [
'id' => $this->id,
'coupon_id' => $this->coupon_id,
'product_id' => $this->product_id,
'id' => $this->resource->id,
'coupon_id' => $this->resource->coupon_id,
'product_id' => $this->resource->product_id,
];
}
}
42 changes: 23 additions & 19 deletions src/Http/Resources/CouponResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,32 @@ public function __construct(Coupon $coupon)
parent::__construct($coupon);
}

public function toArray($request)
/**
* @param $request
* @return array<string, mixed>
*/
public function toArray($request): array
{
$couponService = app(CouponServiceContract::class);
return [
'id' => $this->id,
'name' => $this->name,
'code' => $this->code,
'type' => $this->type,
'active' => $this->active,
'active_from' => $this->active_from,
'active_to' => $this->active_to,
'limit_usage' => $this->limit_usage,
'limit_per_user' => $this->limit_per_user,
'min_cart_price' => $this->min_cart_price,
'max_cart_price' => $this->max_cart_price,
'amount' => $this->amount,
'included_products' => ProductResource::collection($this->includedProducts),
'excluded_products' => ProductResource::collection($this->excludedProducts),
'users' => $this->users->map(fn (User $user) => $user->getKey())->toArray(),
'included_categories' => CategoryResource::collection($this->includedCategories),
'excluded_categories' => CategoryResource::collection($this->excludedCategories),
'exclude_promotions' => $this->exclude_promotions,
'id' => $this->resource->id,
'name' => $this->resource->name,
'code' => $this->resource->code,
'type' => $this->resource->type,
'active' => $this->resource->active,
'active_from' => $this->resource->active_from,
'active_to' => $this->resource->active_to,
'limit_usage' => $this->resource->limit_usage,
'limit_per_user' => $this->resource->limit_per_user,
'min_cart_price' => $this->resource->min_cart_price,
'max_cart_price' => $this->resource->max_cart_price,
'amount' => $this->resource->amount,
'included_products' => ProductResource::collection($this->resource->includedProducts),
'excluded_products' => ProductResource::collection($this->resource->excludedProducts),
'users' => $this->resource->users->map(fn (User $user) => $user->getKey())->toArray(),
'included_categories' => CategoryResource::collection($this->resource->includedCategories),
'excluded_categories' => CategoryResource::collection($this->resource->excludedCategories),
'exclude_promotions' => $this->resource->exclude_promotions,
'usages' => $couponService->couponTimesUsed($this->resource),
];
}
Expand Down
7 changes: 7 additions & 0 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@
*/
class Cart extends BaseCart
{
/** @var array<int, string> */
protected $guarded = ['id', 'cartManager'];

/**
* @return BelongsTo<Coupon, self>
*/
public function coupon(): BelongsTo
{
return $this->belongsTo(Coupon::class);
}

/**
* @return HasMany<CartItem>
*/
public function items(): HasMany
{
return $this->hasMany(CartItem::class);
Expand Down
9 changes: 6 additions & 3 deletions src/Models/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@
*/
class CartItem extends BaseCartItem
{
/**
* @return BelongsTo<Cart, self>
*/
public function cart(): BelongsTo
{
return $this->belongsTo(Cart::class);
}

public function getSubtotalAttribute()
public function getSubtotalAttribute(): float
{
return parent::getSubtotalAttribute() - $this->discountSubtotal;
}

public function getPriceAttribute()
public function getPriceAttribute(): float|null
{
return parent::getPriceAttribute() - $this->discount;
}

public function getBasePriceAttribute()
public function getBasePriceAttribute(): float|null
{
return parent::getPriceAttribute();
}
Expand Down
5 changes: 4 additions & 1 deletion src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ class Category extends BaseCategory
{
use HasFactory;

/**
* @return BelongsToMany<Coupon>
*/
public function coupons(): BelongsToMany
{
return $this->belongsToMany(Coupon::class, 'coupons_categories', 'category_id', 'coupon_id')->using(CouponCategory::class);
}

protected static function newFactory()
protected static function newFactory(): CategoryFactory
{
return new CategoryFactory();
}
Expand Down
Loading

0 comments on commit f40caed

Please sign in to comment.