Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added phpstan #34

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
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: 5
2 changes: 1 addition & 1 deletion src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AuthServiceProvider extends ServiceProvider
Tag::class => TagPolicy::class,
];

public function boot()
public function boot(): void
{
$this->registerPolicies();

Expand Down
4 changes: 2 additions & 2 deletions src/Dto/TagDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions src/EscolaLmsTagsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
*/
class EscolaLmsTagsServiceProvider extends ServiceProvider
{
public $singletons = [
/**
* @var array<class-string, class-string>
*/
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)) {
$this->app->register(EscolaLmsSettingsServiceProvider::class);
}
}

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();
Expand All @@ -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'),
Expand Down
13 changes: 3 additions & 10 deletions src/Http/Controllers/TagsAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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');
}
}
6 changes: 3 additions & 3 deletions src/Http/Request/TagInsertRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class TagInsertRequest extends FormRequest
*
* @return bool
*/
public function authorize()
public function authorize(): bool
{
return auth()->user()->can('create', Tag::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array
* @return array<string, string>
*/
public function rules()
public function rules(): array
{
return [
'model_type' => 'required|string',
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/TagRemoveRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class TagRemoveRequest extends FormRequest
*
* @return bool
*/
public function authorize()
public function authorize(): bool
{
return auth()->user()->can('create', Tag::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array
* @return array<string, string>
*/
public function rules()
public function rules(): array
{
return [
'tags' => 'required|array',
Expand Down
7 changes: 4 additions & 3 deletions src/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* Class Tag
Expand Down Expand Up @@ -31,7 +32,7 @@ class Tag extends Model
/**
* The attributes that should be casted to native types.
*
* @var array
* @var array<string, string>
*/
protected $casts = [
'id' => 'integer',
Expand All @@ -43,7 +44,7 @@ class Tag extends Model
/**
* Validation rules
*
* @var array
* @var array<string, string>
*/
public static $rules = [
'title' => 'nullable|string|max:255',
Expand All @@ -57,7 +58,7 @@ class Tag extends Model
/**
* Get the owning commentable model.
*/
public function morphable()
public function morphable(): MorphTo
{
return $this->morphTo();
}
Expand Down
1 change: 0 additions & 1 deletion src/Policies/TagPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function create(User $user): bool

/**
* @param User $user
* @param Tag $tag
* @return bool
*/
public function delete(User $user): bool
Expand Down
6 changes: 5 additions & 1 deletion src/Repository/TagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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')
Expand Down
Loading