diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..225a807 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -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 diff --git a/composer.json b/composer.json index 32f7cc5..ccd3298 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.0", - "orchestra/testbench": "^6.0" + "orchestra/testbench": ">=6.0", + "nunomaduro/larastan": "^2.0" }, "license": "MIT", "authors": [ diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..99fcd65 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,10 @@ +includes: + - ./vendor/nunomaduro/larastan/extension.neon + +parameters: + + paths: + - src/ + + # The level 9 is the highest level + level: 5 diff --git a/src/AuthServiceProvider.php b/src/AuthServiceProvider.php index 1747e5d..3080166 100644 --- a/src/AuthServiceProvider.php +++ b/src/AuthServiceProvider.php @@ -14,7 +14,7 @@ class AuthServiceProvider extends ServiceProvider Tag::class => TagPolicy::class, ]; - public function boot() + public function boot(): void { $this->registerPolicies(); diff --git a/src/Dto/TagDto.php b/src/Dto/TagDto.php index 9ef06f2..267d16b 100644 --- a/src/Dto/TagDto.php +++ b/src/Dto/TagDto.php @@ -38,12 +38,12 @@ public function getModelName() :? string return $this->modelName; } - public function getTags() + public function getTags(): array { return $this->tags; } - public function getModelId() + public function getModelId(): int { return $this->modelId; } diff --git a/src/EscolaLmsTagsServiceProvider.php b/src/EscolaLmsTagsServiceProvider.php index 1d5b7be..3055356 100644 --- a/src/EscolaLmsTagsServiceProvider.php +++ b/src/EscolaLmsTagsServiceProvider.php @@ -16,12 +16,15 @@ */ class EscolaLmsTagsServiceProvider extends ServiceProvider { - public $singletons = [ + /** + * @var array + */ + public array $singletons = [ TagRepositoryContract::class => TagRepository::class, TagServiceContract::class => TagService::class ]; - public function register() + public function register(): void { $this->mergeConfigFrom(__DIR__ . '/config.php', 'escolalms_tags'); if (!app()->bound(EscolaLmsSettingsServiceProvider::class)) { @@ -29,10 +32,11 @@ public function register() } } - public function boot() + public function boot(): void { $this->loadRoutesFrom(__DIR__ . '/routes.php'); $this->loadMigrations(); + // @phpstan-ignore-next-line $this->app['router']->aliasMiddleware('role', RoleMiddleware::class); if ($this->app->runningInConsole()) { $this->bootForConsole(); @@ -42,7 +46,7 @@ public function boot() $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'tags'); } - protected function bootForConsole() + protected function bootForConsole(): void { $this->publishes([ __DIR__ . '/config.php' => config_path('escolalms_tags.php'), diff --git a/src/Http/Controllers/TagsAPIController.php b/src/Http/Controllers/TagsAPIController.php index cf7cb25..db25b9b 100644 --- a/src/Http/Controllers/TagsAPIController.php +++ b/src/Http/Controllers/TagsAPIController.php @@ -69,14 +69,11 @@ public function index(Request $request): JsonResponse * Display the specified Tag. * GET|HEAD /tags/{tag} * - * @param $id * @return JsonResponse */ public function show(Tag $tag, Request $request): JsonResponse { - return empty($tag) ? - $this->sendError('Tag not found', 404) : - $this->sendResponse($tag, 'Tag fetched successfully'); + return $this->sendResponse($tag, 'Tag fetched successfully'); } /** @@ -101,16 +98,12 @@ public function destroy(TagRemoveRequest $tagRemoveRequest): JsonResponse public function unique(Request $request): JsonResponse { $tags = $this->tagRepository->unique(); - return $tags ? - $this->sendResponse($tags, 'Tags unique fetched successfully') : - $this->sendError('Tags not found', 404) ; + return $this->sendResponse($tags, 'Tags unique fetched successfully'); } public function uniqueAdmin(Request $request): JsonResponse { $tags = $this->tagRepository->unique(); - return $tags ? - $this->sendResponse($tags, 'Tags unique fetched successfully') : - $this->sendError('Tags not found', 404) ; + return $this->sendResponse($tags, 'Tags unique fetched successfully'); } } diff --git a/src/Http/Request/TagInsertRequest.php b/src/Http/Request/TagInsertRequest.php index 707ea40..71bcb89 100644 --- a/src/Http/Request/TagInsertRequest.php +++ b/src/Http/Request/TagInsertRequest.php @@ -12,7 +12,7 @@ class TagInsertRequest extends FormRequest * * @return bool */ - public function authorize() + public function authorize(): bool { return auth()->user()->can('create', Tag::class); } @@ -20,9 +20,9 @@ public function authorize() /** * Get the validation rules that apply to the request. * - * @return array + * @return array */ - public function rules() + public function rules(): array { return [ 'model_type' => 'required|string', diff --git a/src/Http/Request/TagRemoveRequest.php b/src/Http/Request/TagRemoveRequest.php index 177f952..236b765 100644 --- a/src/Http/Request/TagRemoveRequest.php +++ b/src/Http/Request/TagRemoveRequest.php @@ -12,7 +12,7 @@ class TagRemoveRequest extends FormRequest * * @return bool */ - public function authorize() + public function authorize(): bool { return auth()->user()->can('create', Tag::class); } @@ -20,9 +20,9 @@ public function authorize() /** * Get the validation rules that apply to the request. * - * @return array + * @return array */ - public function rules() + public function rules(): array { return [ 'tags' => 'required|array', diff --git a/src/Models/Tag.php b/src/Models/Tag.php index 6ae984d..579987e 100644 --- a/src/Models/Tag.php +++ b/src/Models/Tag.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; /** * Class Tag @@ -31,7 +32,7 @@ class Tag extends Model /** * The attributes that should be casted to native types. * - * @var array + * @var array */ protected $casts = [ 'id' => 'integer', @@ -43,7 +44,7 @@ class Tag extends Model /** * Validation rules * - * @var array + * @var array */ public static $rules = [ 'title' => 'nullable|string|max:255', @@ -57,7 +58,7 @@ class Tag extends Model /** * Get the owning commentable model. */ - public function morphable() + public function morphable(): MorphTo { return $this->morphTo(); } diff --git a/src/Policies/TagPolicy.php b/src/Policies/TagPolicy.php index f4d097f..76ccc93 100644 --- a/src/Policies/TagPolicy.php +++ b/src/Policies/TagPolicy.php @@ -22,7 +22,6 @@ public function create(User $user): bool /** * @param User $user - * @param Tag $tag * @return bool */ public function delete(User $user): bool diff --git a/src/Repository/TagRepository.php b/src/Repository/TagRepository.php index 92fd302..ef64ae0 100644 --- a/src/Repository/TagRepository.php +++ b/src/Repository/TagRepository.php @@ -47,7 +47,9 @@ public function model() : string */ public function insert(array $tagData) : Tag { - return $this->model->create($tagData); + /** @var Tag $model */ + $model = $this->model->create($tagData); + return $model; } /** @@ -73,7 +75,9 @@ public function unique(): Collection public function uniqueTagsFromActiveCourses(): ?Collection { return $this->model->select('title') + // @phpstan-ignore-next-line ->whereHasMorph('morphable', Course::class, function(Builder $query) { + // @phpstan-ignore-next-line $query->where('status', '=', CourseStatusEnum::PUBLISHED); }) ->distinct('title')