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 #324

Merged
merged 6 commits into from
Jul 10, 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
2 changes: 1 addition & 1 deletion .github/workflows/stan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
run: vendor/bin/testbench migrate:fresh

- name: Run tests
run: vendor/bin/phpstan analyse --level max src
run: vendor/bin/phpstan analyse
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
},
"require-dev": {
"orchestra/testbench": ">=5.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.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: 2
3 changes: 2 additions & 1 deletion src/Http/Controllers/CourseAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

return $this->sendResponseForResource(CourseListResource::collection(
$this->courseRepository->getAuthoredCourses(
$user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))

Check warning on line 76 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "DecrementInteger": @@ @@ public function authoredCourses(ListAuthoredCourseAPIRequest $request): JsonResponse { $user = $request->user(); - return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))), __('Courses retrieved successfully')); + return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 19))), __('Courses retrieved successfully')); } public function store(CreateCourseAPIRequest $request): JsonResponse {

Check warning on line 76 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "IncrementInteger": @@ @@ public function authoredCourses(ListAuthoredCourseAPIRequest $request): JsonResponse { $user = $request->user(); - return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))), __('Courses retrieved successfully')); + return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 21))), __('Courses retrieved successfully')); } public function store(CreateCourseAPIRequest $request): JsonResponse {
), __('Courses retrieved successfully'));
}

Expand Down Expand Up @@ -105,6 +105,7 @@

public function program($id, GetCourseCurriculumAPIRequest $request): JsonResponse
{
/** @var Course $course */
$course = $this->courseRepository->findWith(
$id,
['*'],
Expand Down Expand Up @@ -135,7 +136,7 @@
{
$input = $request->all();

/** @var Course $course */
/** @var Course|null $course */
$course = $this->courseRepository->find($id);

if (empty($course)) {
Expand Down Expand Up @@ -165,7 +166,7 @@
{
$this->courseServiceContract->sort($request->get('class'), $request->get('orders'));

return $this->sendResponse([], __($request->get('class') . ' sorted successfully'));

Check warning on line 169 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "Concat": @@ @@ public function sort(SortAPIRequest $request): JsonResponse { $this->courseServiceContract->sort($request->get('class'), $request->get('orders')); - return $this->sendResponse([], __($request->get('class') . ' sorted successfully')); + return $this->sendResponse([], __(' sorted successfully' . $request->get('class'))); } public function uniqueTags(): JsonResponse {

Check warning on line 169 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ public function sort(SortAPIRequest $request): JsonResponse { $this->courseServiceContract->sort($request->get('class'), $request->get('orders')); - return $this->sendResponse([], __($request->get('class') . ' sorted successfully')); + return $this->sendResponse([], __(' sorted successfully')); } public function uniqueTags(): JsonResponse {

Check warning on line 169 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ public function sort(SortAPIRequest $request): JsonResponse { $this->courseServiceContract->sort($request->get('class'), $request->get('orders')); - return $this->sendResponse([], __($request->get('class') . ' sorted successfully')); + return $this->sendResponse([], __($request->get('class'))); } public function uniqueTags(): JsonResponse {
}

public function uniqueTags(): JsonResponse
Expand All @@ -173,6 +174,6 @@
$tags = $this->tagRepositoryContract->uniqueTagsFromActiveCourses();
return $tags ?
$this->sendResponse($tags, 'Tags unique fetched successfully') :
$this->sendError('Tags not found', 404);

Check warning on line 177 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "DecrementInteger": @@ @@ public function uniqueTags(): JsonResponse { $tags = $this->tagRepositoryContract->uniqueTagsFromActiveCourses(); - return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 404); + return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 403); } }

Check warning on line 177 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "IncrementInteger": @@ @@ public function uniqueTags(): JsonResponse { $tags = $this->tagRepositoryContract->uniqueTagsFromActiveCourses(); - return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 404); + return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 405); } }
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/LessonAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function update($id, UpdateLessonAPIRequest $request)
{
$input = $request->all();

/** @var Lesson $lesson */
/** @var Lesson|null $lesson */
$lesson = $this->lessonRepository->find($id);

if (empty($lesson)) {
Expand Down
16 changes: 7 additions & 9 deletions src/Http/Controllers/Swagger/TopicAPISwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EscolaLms\Courses\Http\Requests\DeleteTopicAPIRequest;
use EscolaLms\Courses\Http\Requests\GetTopicAPIRequest;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

interface TopicAPISwagger
Expand Down Expand Up @@ -51,7 +52,7 @@ public function index(Request $request);

/**
* @param CreateTopicAPIRequest $request
* @return Response
* @return JsonResponse
*
* @OA\Post(
* path="/api/admin/topics",
Expand Down Expand Up @@ -94,7 +95,7 @@ public function index(Request $request);
* )
*/

public function store(CreateTopicAPIRequest $request);
public function store(CreateTopicAPIRequest $request): JsonResponse;

/**
* @OA\Get(
Expand Down Expand Up @@ -240,8 +241,7 @@ public function update($id, UpdateTopicAPIRequest $request);
public function destroy($id, DeleteTopicAPIRequest $request);

/**
* @param Request $request
* @return Response
* @return JsonResponse
*
* @OA\Get(
* path="/api/admin/topics/types",
Expand Down Expand Up @@ -276,12 +276,11 @@ public function destroy($id, DeleteTopicAPIRequest $request);
* )
* )
*/

public function classes();
public function classes(): JsonResponse;

/**
* @param CloneTopicAPIRequest $request
* @return Response
* @return JsonResponse
*
* @OA\Post(
* path="/api/admin/topics/{id}/clone",
Expand Down Expand Up @@ -324,6 +323,5 @@ public function classes();
* )
* )
*/

public function clone(CloneTopicAPIRequest $request);
public function clone(CloneTopicAPIRequest $request): JsonResponse;
}
10 changes: 5 additions & 5 deletions src/Http/Controllers/TopicAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use EscolaLms\Courses\Http\Resources\TopicResource;
use EscolaLms\Courses\Repositories\Contracts\TopicRepositoryContract;
use EscolaLms\Courses\Services\Contracts\TopicServiceContract;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

Expand All @@ -22,8 +23,7 @@
*/
class TopicAPIController extends AppBaseController implements TopicAPISwagger
{
/** @var TopicRepository */
private $topicRepository;
private TopicRepositoryContract $topicRepository;

private TopicServiceContract $topicService;

Expand All @@ -44,9 +44,9 @@
return $this->sendResponseForResource(TopicResource::collection($topics), __('Topics retrieved successfully'));
}

public function store(CreateTopicAPIRequest $request)
public function store(CreateTopicAPIRequest $request): JsonResponse
{
try {

Check warning on line 49 in src/Http/Controllers/TopicAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "CatchBlockRemoval": @@ @@ { try { $topic = $this->topicRepository->createFromRequest($request); - } catch (AccessDeniedHttpException $error) { - return $this->sendError($error->getMessage(), 403); } catch (TopicException $error) { return $this->sendDataError($error->getMessage(), $error->getData()); } catch (Error $error) {
$topic = $this->topicRepository->createFromRequest($request);
} catch (AccessDeniedHttpException $error) {
return $this->sendError($error->getMessage(), 403);
Expand Down Expand Up @@ -110,14 +110,14 @@
return $this->sendSuccess(__('Topic deleted successfully'));
}

public function classes()
public function classes(): JsonResponse
{
$classes = $this->topicRepository->availableContentClasses();

return $this->sendResponse($classes, __('Topic content available list'));
}

public function clone(CloneTopicAPIRequest $request)
public function clone(CloneTopicAPIRequest $request): JsonResponse
{
$topic = $request->getTopic();

Expand Down
2 changes: 2 additions & 0 deletions src/Http/Requests/SortAPIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EscolaLms\Courses\Models\Topic;
use EscolaLms\Courses\Models\Lesson;
use EscolaLms\Courses\Models\Course;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Http\FormRequest;

class SortAPIRequest extends FormRequest
Expand All @@ -26,6 +27,7 @@ public function authorize(): bool
&& $lessons->pluck('course_id')->unique()->count() === 1
&& $lessons->pluck('parent_lesson_id')->unique()->count() === 1;
case 'Topic':
/** @var Collection<int, Topic> $topics */
$topics = Topic::whereIn('id', $ids)->get();

if ($topics->count() !== count($ids) || $topics->pluck('lesson_id')->unique()->count() != 1) {
Expand Down
16 changes: 8 additions & 8 deletions src/Http/Resources/Admin/LessonWithTopicsAdminResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public function toArray($request): array
$lesson = $this->getResource();

return [
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
'duration' => $this->duration,
'active' => $this->active,
'id' => $this->resource->id,
'title' => $this->resource->title,
'summary' => $this->resource->summary,
'duration' => $this->resource->duration,
'active' => $this->resource->active,
'topics' => TopicAdminResource::collection($lesson->topics->sortBy('order')),
'topics_count' => $lesson->topics->count(),
'order' => $this->order,
'order' => $this->resource->order,
'lessons' => LessonWithTopicsAdminResource::collection($lesson->lessons->sortBy('order')),
'active_from' => $this->active_from,
'active_to' => $this->active_to,
'active_from' => $this->resource->active_from,
'active_to' => $this->resource->active_to,
...ModelFields::getExtraAttributesValues($this->resource, MetaFieldVisibilityEnum::PUBLIC)
];
}
Expand Down
38 changes: 19 additions & 19 deletions src/Http/Resources/Admin/TopicAdminResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ class TopicAdminResource extends JsonResource
*/
public function toArray($request)
{
$topicable = $this->topicable;
$topicable = $this->resource->topicable;

if (Topic::getResourceClass($this->topicable_type, 'admin')) {
$resourceClass = Topic::getResourceClass($this->topicable_type, 'admin');
$resource = new $resourceClass($this->topicable);
if (Topic::getResourceClass($this->resource->topicable_type, 'admin')) {
$resourceClass = Topic::getResourceClass($this->resource->topicable_type, 'admin');
$resource = new $resourceClass($this->resource->topicable);
$topicable = $resource->toArray($request);
}

return [
'id' => $this->id,
'title' => $this->title,
'lesson_id' => $this->lesson_id,
'active' => $this->active,
'preview' => $this->preview,
'topicable_id' => $this->topicable_id,
'topicable_type' => $this->topicable_type,
'id' => $this->resource->id,
'title' => $this->resource->title,
'lesson_id' => $this->resource->lesson_id,
'active' => $this->resource->active,
'preview' => $this->resource->preview,
'topicable_id' => $this->resource->topicable_id,
'topicable_type' => $this->resource->topicable_type,
'topicable' => $topicable,
'summary' => $this->summary,
'introduction' => $this->introduction,
'description' => $this->description,
'resources' => TopicResourceResource::collection($this->resources),
'order' => $this->order,
'json' => $this->json,
'can_skip' => $this->can_skip,
'duration' => $this->duration,
'summary' => $this->resource->summary,
'introduction' => $this->resource->introduction,
'description' => $this->resource->description,
'resources' => TopicResourceResource::collection($this->resource->resources),
'order' => $this->resource->order,
'json' => $this->resource->json,
'can_skip' => $this->resource->can_skip,
'duration' => $this->resource->duration,
];
}
}
64 changes: 32 additions & 32 deletions src/Http/Resources/CourseListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,38 @@ class CourseListResource extends JsonResource
public function toArray($request)
{
$fields = [
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'title' => $this->title,
'summary' => $this->summary,
'image_path' => $this->image_path,
'video_path' => $this->video_path,
'duration' => $this->duration,
'author_id' => $this->author_id,
'author' => $this->author ? TutorResource::make($this->author) : null,
'authors' => $this->authors ? TutorResource::collection($this->authors) : [],
'status' => $this->status,
'subtitle' => $this->subtitle,
'language' => $this->language,
'description' => $this->description,
'categories' => $this->categories,
'tags' => $this->tags,
'level' => $this->level,
'poster_path' => $this->poster_path,
'active_from' => $this->active_from,
'active_to' => $this->active_to,
'hours_to_complete' => $this->hours_to_complete,
'findable' => $this->findable,
'scorm_sco_id' => $this->scorm_sco_id,
'target_group' => $this->target_group,
'users_count' => $this->users_count,
'image_url' => $this->image_url,
'video_url' => $this->video_url,
'poster_url' => $this->poster_url,
'teaser_url' => $this->teaser_url,
'public' => $this->public ?? false,
'fields' => $this->fields,
'id' => $this->resource->id,
'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at,
'title' => $this->resource->title,
'summary' => $this->resource->summary,
'image_path' => $this->resource->image_path,
'video_path' => $this->resource->video_path,
'duration' => $this->resource->duration,
'author_id' => $this->resource->author_id,
'author' => $this->resource->author ? TutorResource::make($this->resource->author) : null,
'authors' => $this->resource->authors ? TutorResource::collection($this->resource->authors) : [],
'status' => $this->resource->status,
'subtitle' => $this->resource->subtitle,
'language' => $this->resource->language,
'description' => $this->resource->description,
'categories' => $this->resource->categories,
'tags' => $this->resource->tags,
'level' => $this->resource->level,
'poster_path' => $this->resource->poster_path,
'active_from' => $this->resource->active_from,
'active_to' => $this->resource->active_to,
'hours_to_complete' => $this->resource->hours_to_complete,
'findable' => $this->resource->findable,
'scorm_sco_id' => $this->resource->scorm_sco_id,
'target_group' => $this->resource->target_group,
'users_count' => $this->resource->users_count,
'image_url' => $this->resource->image_url,
'video_url' => $this->resource->video_url,
'poster_url' => $this->resource->poster_url,
'teaser_url' => $this->resource->teaser_url,
'public' => $this->resource->public ?? false,
'fields' => $this->resource->fields,
];

return self::apply($fields, $this);
Expand Down
66 changes: 33 additions & 33 deletions src/Http/Resources/CourseSimpleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ class CourseSimpleResource extends JsonResource
public function toArray($request)
{
$fields = [
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'title' => $this->title,
'summary' => $this->summary,
'image_path' => $this->image_path,
'video_path' => $this->video_path,
'duration' => $this->duration,
'author_id' => $this->author_id,
'author' => $this->author ? TutorResource::make($this->author) : null,
'authors' => $this->authors ? TutorResource::collection($this->authors) : [],
'status' => $this->status,
'subtitle' => $this->subtitle,
'language' => $this->language,
'description' => $this->description,
'categories' => $this->categories,
'tags' => $this->tags,
'level' => $this->level,
'lessons' => LessonSimpleResource::collection($this->lessons()->main()->active()->orderBy('order')->get()),
'poster_path' => $this->poster_path,
'active_from' => $this->active_from,
'active_to' => $this->active_to,
'hours_to_complete' => $this->hours_to_complete,
'findable' => $this->findable,
'scorm_sco_id' => $this->scorm_sco_id,
'target_group' => $this->target_group,
'users_count' => $this->users_count,
'image_url' => $this->image_url,
'video_url' => $this->video_url,
'poster_url' => $this->poster_url,
'teaser_url' => $this->teaser_url,
'public' => $this->public ?? false,
'fields' => $this->fields,
'id' => $this->resource->id,
'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at,
'title' => $this->resource->title,
'summary' => $this->resource->summary,
'image_path' => $this->resource->image_path,
'video_path' => $this->resource->video_path,
'duration' => $this->resource->duration,
'author_id' => $this->resource->author_id,
'author' => $this->resource->author ? TutorResource::make($this->resource->author) : null,
'authors' => $this->resource->authors ? TutorResource::collection($this->resource->authors) : [],
'status' => $this->resource->status,
'subtitle' => $this->resource->subtitle,
'language' => $this->resource->language,
'description' => $this->resource->description,
'categories' => $this->resource->categories,
'tags' => $this->resource->tags,
'level' => $this->resource->level,
'lessons' => LessonSimpleResource::collection($this->resource->lessons()->main()->active()->orderBy('order')->get()),
'poster_path' => $this->resource->poster_path,
'active_from' => $this->resource->active_from,
'active_to' => $this->resource->active_to,
'hours_to_complete' => $this->resource->hours_to_complete,
'findable' => $this->resource->findable,
'scorm_sco_id' => $this->resource->scorm_sco_id,
'target_group' => $this->resource->target_group,
'users_count' => $this->resource->users_count,
'image_url' => $this->resource->image_url,
'video_url' => $this->resource->video_url,
'poster_url' => $this->resource->poster_url,
'teaser_url' => $this->resource->teaser_url,
'public' => $this->resource->public ?? false,
'fields' => $this->resource->fields,
];
return self::apply($fields, $this);
}
Expand Down
Loading
Loading