From ae3e14019a3761e30c3b1f61763d6b7131f54b74 Mon Sep 17 00:00:00 2001 From: Laurent Muller Date: Tue, 21 Jan 2025 13:51:47 +0100 Subject: [PATCH] Removed the calculation query resolver. Updated exceptions. --- public/js/application/calculation_edit.js | 74 ++++----- src/Chart/MonthChart.php | 6 +- src/Controller/AbstractEntityController.php | 3 +- src/Controller/AjaxCalculationController.php | 29 ++-- .../CalculationArchiveController.php | 6 +- src/Controller/CalculationBelowController.php | 7 +- src/Controller/CalculationController.php | 15 +- .../CalculationDuplicateController.php | 7 +- src/Controller/CalculationEmptyController.php | 7 +- src/Controller/CalculationStateController.php | 7 +- .../CalculationUpdateController.php | 3 +- src/Controller/CategoryController.php | 9 +- src/Controller/ChartController.php | 3 +- src/Controller/GlobalMarginController.php | 5 +- src/Controller/GroupController.php | 7 +- src/Controller/TaskController.php | 9 +- src/Controller/UserController.php | 9 +- src/Enums/ImageExtension.php | 2 +- src/Generator/CalculationGenerator.php | 5 +- src/Model/CalculationGroupQuery.php | 30 ---- src/Model/CalculationQuery.php | 6 +- .../AbstractCategoryItemRepository.php | 3 +- src/Repository/AbstractRepository.php | 3 +- .../CalculationCategoryRepository.php | 3 +- src/Repository/CalculationGroupRepository.php | 3 +- src/Repository/GlobalMarginRepository.php | 3 +- src/Repository/GroupMarginRepository.php | 3 +- src/Repository/UserRepository.php | 3 +- src/Resolver/CalculationQueryResolver.php | 98 ------------ src/Service/CalculationArchiveService.php | 9 +- src/Service/CalculationService.php | 92 +++++------ src/Service/CalculationUpdateService.php | 5 +- src/Table/CalculationBelowTable.php | 5 +- src/Table/CalculationDuplicateTable.php | 5 +- src/Table/CalculationEmptyTable.php | 5 +- src/Traits/RightsTrait.php | 2 +- .../AjaxCalculationControllerTest.php | 6 +- .../Resolver/CalculationQueryResolverTest.php | 143 ------------------ tests/Service/CalculationServiceTest.php | 9 +- vendor-bin/phpstan/composer.lock | 12 +- 40 files changed, 209 insertions(+), 452 deletions(-) delete mode 100644 src/Model/CalculationGroupQuery.php delete mode 100644 src/Resolver/CalculationQueryResolver.php delete mode 100644 tests/Resolver/CalculationQueryResolverTest.php diff --git a/public/js/application/calculation_edit.js b/public/js/application/calculation_edit.js index 7663a314a..5df60fd71 100644 --- a/public/js/application/calculation_edit.js +++ b/public/js/application/calculation_edit.js @@ -291,11 +291,11 @@ const Application = { }); const userMargin = $('#calculation_userMargin').floatVal() / 100.0; - return { + return JSON.stringify({ adjust: adjust, userMargin: userMargin, groups: groups, - }; + }); }, /** @@ -343,10 +343,6 @@ const Application = { that.jqXHR = null; } - // parameters - const url = $form.data('update'); - const data = this.serializeForm(adjust); - /** * @param {Object} response * @param {boolean} response.result @@ -356,37 +352,43 @@ const Application = { * @param {number} response.user_margin * @param {boolean} response.overall_below */ - that.jqXHR = $.post(url, data, function (response) { - // error? - if (!response.result) { - return that.disable(response.message); - } - // update content - const $totalPanel = $('#totals-panel'); - if (response.view) { - const $body = $('#totals-table > tbody'); - $body.fadeOut(300, function () { - $body.html(response.view).fadeIn(300); - $totalPanel.fadeIn(); - }); - } else { - $totalPanel.fadeOut(); - } - if (response.adjust && !$.isUndefined(response.user_margin) && !isNaN(response.user_margin)) { - $('#calculation_userMargin').intVal(response.user_margin * 100).selectFocus(); - } - if (response.overall_below) { - $buttonAdjust.toggleDisabled(false).removeClass('cursor-default'); - } else { - $buttonAdjust.toggleDisabled(true).addClass('cursor-default'); - } - $('#calculation_customer').trigger('input'); - $('#data-table-edit').updateErrors(); - return that; + that.jqXHR = $.post({ + url: $form.data('update'), + data: this.serializeForm(adjust), + contentType: 'application/json;', + success: function (response) { + // error? + if (!response.result) { + return that.disable(response.message); + } - }).fail(function (_jqXHR, textStatus) { - if (textStatus !== 'abort') { - return that.disable(); + // update content + const $totalPanel = $('#totals-panel'); + if (response.view) { + const $body = $('#totals-table > tbody'); + $body.fadeOut(300, function () { + $body.html(response.view).fadeIn(300); + $totalPanel.fadeIn(); + }); + } else { + $totalPanel.fadeOut(); + } + if (response.adjust && !$.isUndefined(response.user_margin) && !isNaN(response.user_margin)) { + $('#calculation_userMargin').intVal(response.user_margin * 100).selectFocus(); + } + if (response.overall_below) { + $buttonAdjust.toggleDisabled(false).removeClass('cursor-default'); + } else { + $buttonAdjust.toggleDisabled(true).addClass('cursor-default'); + } + $('#calculation_customer').trigger('input'); + $('#data-table-edit').updateErrors(); + return that; + }, + fail: function (_jqXHR, textStatus) { + if (textStatus !== 'abort') { + return that.disable(); + } } }); diff --git a/src/Chart/MonthChart.php b/src/Chart/MonthChart.php index acc06cbad..bbd544a6b 100644 --- a/src/Chart/MonthChart.php +++ b/src/Chart/MonthChart.php @@ -18,6 +18,7 @@ use App\Service\ApplicationService; use App\Traits\ArrayTrait; use App\Utils\FormatUtils; +use Doctrine\ORM\Exception\ORMException; use HighchartsBundle\Highcharts\ChartExpression; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Twig\Environment; @@ -55,8 +56,7 @@ public function __construct( /** * Generate the chart data. * - * @throws \Doctrine\ORM\Exception\ORMException - * @throws \Exception + * @throws \Exception|ORMException */ public function generate(int $months): array { @@ -123,7 +123,7 @@ private function formatDate(\DateTimeInterface $date): string /** * @return int[] * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getAllowedMonths(): array { diff --git a/src/Controller/AbstractEntityController.php b/src/Controller/AbstractEntityController.php index 70bb13cec..b83de3dce 100644 --- a/src/Controller/AbstractEntityController.php +++ b/src/Controller/AbstractEntityController.php @@ -26,6 +26,7 @@ use App\Utils\StringUtils; use App\Word\WordDocument; use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\Exception\ORMException; use fpdf\PdfDocument; use Psr\Log\LoggerInterface; use Symfony\Component\Form\Extension\Core\Type\FormType; @@ -212,7 +213,7 @@ protected function getEditTemplate(): string * * @psalm-return TEntity[] * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ protected function getEntities( array|string $sortedFields = [], diff --git a/src/Controller/AjaxCalculationController.php b/src/Controller/AjaxCalculationController.php index e8031c59c..621965baf 100644 --- a/src/Controller/AjaxCalculationController.php +++ b/src/Controller/AjaxCalculationController.php @@ -16,8 +16,8 @@ use App\Attribute\Post; use App\Interfaces\RoleInterface; use App\Model\CalculationQuery; -use App\Resolver\CalculationQueryResolver; use App\Service\CalculationService; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -26,7 +26,7 @@ use Symfony\Component\Security\Http\Attribute\IsGranted; /** - * Controller to update calculation's total within XMLHttpRequest (Ajax) calls. + * Controller to update calculation's total or the user margin within XMLHttpRequest (Ajax) calls. */ #[AsController] #[Route(path: '/calculation', name: 'calculation_')] @@ -34,33 +34,26 @@ class AjaxCalculationController extends AbstractController { /** - * Update the calculation's total. - * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Post(path: '/update', name: 'update')] public function update( CalculationService $service, LoggerInterface $logger, - #[MapRequestPayload(resolver: CalculationQueryResolver::class)] + #[MapRequestPayload] CalculationQuery $query = new CalculationQuery() ): JsonResponse { try { $parameters = $service->createParameters($query); - if (!$parameters['result']) { - return $this->json($parameters); + if ($parameters['result']) { + $view = $this->renderView('calculation/calculation_ajax_totals.html.twig', $parameters); + $parameters = \array_merge($parameters, [ + 'adjust' => $query->adjust, + 'view' => $view, + ]); } - $view = $this->renderView('calculation/calculation_ajax_totals.html.twig', $parameters); - - return $this->jsonTrue([ - 'view' => $view, - 'adjust' => $query->adjust, - 'user_margin' => $parameters['user_margin'], - 'overall_margin' => $parameters['overall_margin'], - 'overall_total' => $parameters['overall_total'], - 'overall_below' => $parameters['overall_below'], - ]); + return $this->json($parameters); } catch (\Exception $e) { $message = $this->trans('calculation.edit.error.update_total'); $context = $this->getExceptionContext($e); diff --git a/src/Controller/CalculationArchiveController.php b/src/Controller/CalculationArchiveController.php index 2a03f4c8a..67b127df1 100644 --- a/src/Controller/CalculationArchiveController.php +++ b/src/Controller/CalculationArchiveController.php @@ -37,7 +37,8 @@ class CalculationArchiveController extends AbstractController { /** - * @throws ORMException|\DateException + * @throws \DateException + * @throws ORMException */ #[GetPost(path: '/archive', name: 'archive')] public function invoke(Request $request, CalculationArchiveService $service): Response @@ -74,7 +75,8 @@ public function invoke(Request $request, CalculationArchiveService $service): Re /** * @return FormInterface * - * @throws ORMException|\DateException + * @throws \DateException + * @throws ORMException */ private function createQueryForm(CalculationArchiveService $service, CalculationArchiveQuery $query): FormInterface { diff --git a/src/Controller/CalculationBelowController.php b/src/Controller/CalculationBelowController.php index 329b54232..76d0f47c7 100644 --- a/src/Controller/CalculationBelowController.php +++ b/src/Controller/CalculationBelowController.php @@ -25,6 +25,7 @@ use App\Table\DataQuery; use App\Traits\TableTrait; use App\Utils\FormatUtils; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -47,8 +48,8 @@ class CalculationBelowController extends AbstractController * Export the calculations to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the report cannot be rendered - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(CalculationRepository $repository): Response @@ -85,7 +86,7 @@ public function index( /** * Export calculations to a PDF document. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(CalculationRepository $repository): Response @@ -109,7 +110,7 @@ public function pdf(CalculationRepository $repository): Response /** * Returns a response if no calculation is below the given margin. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getEmptyResponse(CalculationRepository $repository, float $minMargin): ?RedirectResponse { diff --git a/src/Controller/CalculationController.php b/src/Controller/CalculationController.php index bf0e0e708..285ec5857 100644 --- a/src/Controller/CalculationController.php +++ b/src/Controller/CalculationController.php @@ -35,6 +35,7 @@ use App\Spreadsheet\CalculationsDocument; use App\Table\CalculationTable; use App\Table\DataQuery; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -65,7 +66,7 @@ public function __construct( /** * Add a new calculation. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetPost(path: '/add', name: 'add')] public function add(Request $request): Response @@ -88,7 +89,7 @@ public function add(Request $request): Response /** * Edit a copy (cloned) calculation. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetPost(path: '/clone/{id}', name: 'clone', requirements: self::ID_REQUIREMENT)] public function clone(Request $request, Calculation $item): Response @@ -117,7 +118,7 @@ public function delete(Request $request, Calculation $item, LoggerInterface $log /** * Edit a calculation. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetPost(path: '/edit/{id}', name: 'edit', requirements: self::ID_REQUIREMENT)] public function edit(Request $request, Calculation $item): Response @@ -129,8 +130,8 @@ public function edit(Request $request, Calculation $item): Response * Export the calculations to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation is found - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -174,7 +175,7 @@ public function index( * Export calculations to a PDF document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse @@ -245,7 +246,7 @@ public function state(Request $request, Calculation $item): Response /** * @param Calculation $item * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ protected function editEntity(Request $request, EntityInterface $item, array $parameters = []): Response { @@ -267,7 +268,7 @@ protected function editEntity(Request $request, EntityInterface $item, array $pa /** * @psalm-param Calculation $item * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ protected function saveToDatabase(EntityInterface $item): void { diff --git a/src/Controller/CalculationDuplicateController.php b/src/Controller/CalculationDuplicateController.php index c244f326d..1b105b851 100644 --- a/src/Controller/CalculationDuplicateController.php +++ b/src/Controller/CalculationDuplicateController.php @@ -23,6 +23,7 @@ use App\Table\CalculationDuplicateTable; use App\Table\DataQuery; use App\Traits\TableTrait; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -47,7 +48,7 @@ class CalculationDuplicateController extends AbstractController * Export the duplicate items to a Spreadsheet document. * * @throws \PhpOffice\PhpSpreadsheet\Exception - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(CalculationRepository $repository): Response @@ -78,7 +79,7 @@ public function index( /** * Exports the duplicate items in the calculations. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(CalculationRepository $repository): Response @@ -96,7 +97,7 @@ public function pdf(CalculationRepository $repository): Response /** * Returns a response if no item is duplicated. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getEmptyResponse(CalculationRepository $repository): ?RedirectResponse { diff --git a/src/Controller/CalculationEmptyController.php b/src/Controller/CalculationEmptyController.php index 29f86408e..e168a7d04 100644 --- a/src/Controller/CalculationEmptyController.php +++ b/src/Controller/CalculationEmptyController.php @@ -23,6 +23,7 @@ use App\Table\CalculationEmptyTable; use App\Table\DataQuery; use App\Traits\TableTrait; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -47,7 +48,7 @@ class CalculationEmptyController extends AbstractController * Export the empty items to a Spreadsheet document. * * @throws \PhpOffice\PhpSpreadsheet\Exception - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(CalculationRepository $repository): Response @@ -78,7 +79,7 @@ public function index( /** * Export the calculations where items have the price or the quantity is equal to 0. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(CalculationRepository $repository): Response @@ -96,7 +97,7 @@ public function pdf(CalculationRepository $repository): Response /** * Returns a response if no calculation's item is empty. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getEmptyResponse(CalculationRepository $repository): ?RedirectResponse { diff --git a/src/Controller/CalculationStateController.php b/src/Controller/CalculationStateController.php index ae7e2bbf9..4ffa3616a 100644 --- a/src/Controller/CalculationStateController.php +++ b/src/Controller/CalculationStateController.php @@ -28,6 +28,7 @@ use App\Spreadsheet\CalculationStatesDocument; use App\Table\CalculationStateTable; use App\Table\DataQuery; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -78,7 +79,7 @@ public function clone(Request $request, CalculationState $item): Response /** * Delete a calculation state. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetDelete(path: '/delete/{id}', name: 'delete', requirements: self::ID_REQUIREMENT)] public function delete( @@ -122,8 +123,8 @@ public function edit(Request $request, CalculationState $item): Response * Export the calculation states to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation state is found - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -154,7 +155,7 @@ public function index( * Export the calculation states to a PDF document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation state is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse diff --git a/src/Controller/CalculationUpdateController.php b/src/Controller/CalculationUpdateController.php index e3e7a0877..186fbc058 100644 --- a/src/Controller/CalculationUpdateController.php +++ b/src/Controller/CalculationUpdateController.php @@ -38,7 +38,8 @@ class CalculationUpdateController extends AbstractController { /** - * @throws ORMException|\Exception + * @throws \Exception + * @throws ORMException */ #[GetPost(path: '/update', name: 'update')] public function update(Request $request, CalculationUpdateService $service): Response diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 75251fdfa..29df7ac76 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -30,6 +30,7 @@ use App\Spreadsheet\CategoriesDocument; use App\Table\CategoryTable; use App\Table\DataQuery; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -82,7 +83,7 @@ public function clone(Request $request, Category $item): Response /** * Delete a category. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetDelete(path: '/delete/{id}', name: 'delete', requirements: self::ID_REQUIREMENT)] public function delete( @@ -137,8 +138,8 @@ public function edit(Request $request, Category $item): Response * Export the categories to a Spreadsheet document. * * @throws NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -168,8 +169,8 @@ public function index( /** * Export the categories to a PDF document. * - * @throws NotFoundHttpException if no category is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws NotFoundHttpException if no category is found + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse diff --git a/src/Controller/ChartController.php b/src/Controller/ChartController.php index bf9a6836f..567e42488 100644 --- a/src/Controller/ChartController.php +++ b/src/Controller/ChartController.php @@ -25,6 +25,7 @@ use App\Repository\CalculationStateRepository; use App\Response\PdfResponse; use App\Traits\MathTrait; +use Doctrine\ORM\Exception\ORMException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -48,8 +49,8 @@ class ChartController extends AbstractController private const KEY_MONTHS = 'chart_month'; /** - * @throws \Doctrine\ORM\Exception\ORMException * @throws BadRequestHttpException + * @throws ORMException */ #[Get(path: '/month', name: 'month')] public function month(Request $request, MonthChart $chart): Response diff --git a/src/Controller/GlobalMarginController.php b/src/Controller/GlobalMarginController.php index 8e55b3183..30d149c81 100644 --- a/src/Controller/GlobalMarginController.php +++ b/src/Controller/GlobalMarginController.php @@ -28,6 +28,7 @@ use App\Spreadsheet\GlobalMarginsDocument; use App\Table\DataQuery; use App\Table\GlobalMarginTable; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -83,8 +84,8 @@ public function edit(Request $request): Response * Export the global margins to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no global margin is found - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -115,7 +116,7 @@ public function index( * Export the global margins to a PDF document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no global margin is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse diff --git a/src/Controller/GroupController.php b/src/Controller/GroupController.php index 0e2bbbdf6..60a77ae42 100644 --- a/src/Controller/GroupController.php +++ b/src/Controller/GroupController.php @@ -27,6 +27,7 @@ use App\Spreadsheet\GroupsDocument; use App\Table\DataQuery; use App\Table\GroupTable; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -79,7 +80,7 @@ public function clone(Request $request, Group $item): Response /** * Delete a group. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[GetDelete(path: '/delete/{id}', name: 'delete', requirements: self::ID_REQUIREMENT)] public function delete( @@ -130,8 +131,8 @@ public function edit(Request $request, Group $item): Response * Export the groups to a Spreadsheet document. * * @throws NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -162,7 +163,7 @@ public function index( * Export the groups to a PDF document. * * @throws NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 5aa6919ff..80a90d981 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -31,6 +31,7 @@ use App\Spreadsheet\TasksDocument; use App\Table\DataQuery; use App\Table\TaskTable; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -133,9 +134,9 @@ public function edit(Request $request, Task $item): Response /** * Export tasks to a Spreadsheet document. * - * @throws NotFoundHttpException if no category is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws NotFoundHttpException if no category is found * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(): SpreadsheetResponse @@ -165,8 +166,8 @@ public function index( /** * Export tasks to a PDF document. * - * @throws NotFoundHttpException if no category is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws NotFoundHttpException if no category is found + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(): PdfResponse diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 6b34ad5d3..b338cb959 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -42,6 +42,7 @@ use App\Table\DataQuery; use App\Table\UserTable; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Form\Extension\Core\Type\FormType; @@ -110,8 +111,8 @@ public function edit(Request $request, User $item): Response * Export the customers to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/excel', name: 'excel')] public function excel(StorageInterface $storage): SpreadsheetResponse @@ -199,7 +200,7 @@ public function password(Request $request, User $item, PasswordTooltipService $s * Export the users to a PDF document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no user is found - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/pdf', name: 'pdf')] public function pdf(StorageInterface $storage): PdfResponse @@ -323,8 +324,8 @@ public function rights( * Export the user access rights to a Spreadsheet document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException * @throws \PhpOffice\PhpSpreadsheet\Exception + * @throws ORMException */ #[Get(path: '/rights/excel', name: 'rights_excel')] public function rightsExcel(RoleBuilderService $builder): SpreadsheetResponse @@ -342,7 +343,7 @@ public function rightsExcel(RoleBuilderService $builder): SpreadsheetResponse * Export user access rights to a PDF document. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ #[Get(path: '/rights/pdf', name: 'rights_pdf')] public function rightsPdf(RoleBuilderService $builder): PdfResponse diff --git a/src/Enums/ImageExtension.php b/src/Enums/ImageExtension.php index 18b265966..b508da3dc 100644 --- a/src/Enums/ImageExtension.php +++ b/src/Enums/ImageExtension.php @@ -165,7 +165,7 @@ public function getImageType(): int * * @return bool true on success or false on failure * - * @throw \InvalidArgumentException if one of the given options is invalid + * @throws \InvalidArgumentException if one of the given options is invalid * * @psalm-param SaveOptionsType $options */ diff --git a/src/Generator/CalculationGenerator.php b/src/Generator/CalculationGenerator.php index 9198df091..c8a236c89 100644 --- a/src/Generator/CalculationGenerator.php +++ b/src/Generator/CalculationGenerator.php @@ -23,6 +23,7 @@ use App\Service\FakerService; use App\Utils\FormatUtils; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Exception\ORMException; /** * Class to generate calculations. @@ -40,7 +41,7 @@ public function __construct( } /** - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ protected function createEntities(int $count, bool $simulate, Generator $generator): array { @@ -72,7 +73,7 @@ protected function mapEntity(EntityInterface $entity): array } /** - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function createEntity(int $min, int $max, Generator $generator): Calculation { diff --git a/src/Model/CalculationGroupQuery.php b/src/Model/CalculationGroupQuery.php deleted file mode 100644 index df72f0962..000000000 --- a/src/Model/CalculationGroupQuery.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace App\Model; - -/** - * Contains a single query group values. - */ -readonly class CalculationGroupQuery -{ - /** - * @param int $id the group identifier - * @param float $total the group's total - */ - public function __construct( - public int $id = 0, - public float $total = 0.0 - ) { - } -} diff --git a/src/Model/CalculationQuery.php b/src/Model/CalculationQuery.php index affc68c1e..575e06f7e 100644 --- a/src/Model/CalculationQuery.php +++ b/src/Model/CalculationQuery.php @@ -21,9 +21,9 @@ readonly class CalculationQuery { /** - * @param bool $adjust adjust the user margin state - * @param float $userMargin the user margin - * @param CalculationGroupQuery[] $groups the groups + * @param bool $adjust true to adjust the user's margin to reach the minimum margin + * @param float $userMargin the user margin + * @param QueryGroupType[] $groups the groups containing each the identifier and the total */ public function __construct( public bool $adjust = false, diff --git a/src/Repository/AbstractCategoryItemRepository.php b/src/Repository/AbstractCategoryItemRepository.php index bc0bfed7f..9607b2e0c 100644 --- a/src/Repository/AbstractCategoryItemRepository.php +++ b/src/Repository/AbstractCategoryItemRepository.php @@ -15,6 +15,7 @@ use App\Entity\Category; use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\QueryBuilder; /** @@ -43,7 +44,7 @@ abstract class AbstractCategoryItemRepository extends AbstractRepository * * @return int the number of entities * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function countCategoryReferences(Category $category): int { diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php index 4401f24ff..fdf6fc23b 100644 --- a/src/Repository/AbstractRepository.php +++ b/src/Repository/AbstractRepository.php @@ -19,6 +19,7 @@ use App\Utils\StringUtils; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; @@ -126,7 +127,7 @@ public function getSearchFields(string $field, string $alias = self::DEFAULT_ALI * @param array $criteria the filter criteria (the where clause) * @param literal-string $alias the entity alias * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException * * @see AbstractRepository::createDefaultQueryBuilder() * diff --git a/src/Repository/CalculationCategoryRepository.php b/src/Repository/CalculationCategoryRepository.php index 07e2f9168..04cf4d4a2 100644 --- a/src/Repository/CalculationCategoryRepository.php +++ b/src/Repository/CalculationCategoryRepository.php @@ -16,6 +16,7 @@ use App\Entity\CalculationCategory; use App\Entity\Category; use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Exception\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -37,7 +38,7 @@ public function __construct(ManagerRegistry $registry) * * @return int the number of calculations * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function countCategoryReferences(Category $category): int { diff --git a/src/Repository/CalculationGroupRepository.php b/src/Repository/CalculationGroupRepository.php index 0ac0db49e..1145d0e28 100644 --- a/src/Repository/CalculationGroupRepository.php +++ b/src/Repository/CalculationGroupRepository.php @@ -16,6 +16,7 @@ use App\Entity\CalculationGroup; use App\Entity\Group; use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Exception\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -37,7 +38,7 @@ public function __construct(ManagerRegistry $registry) * * @return int the number of calculations * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function countGroupReferences(Group $group): int { diff --git a/src/Repository/GlobalMarginRepository.php b/src/Repository/GlobalMarginRepository.php index 633c2e993..d3ab17e37 100644 --- a/src/Repository/GlobalMarginRepository.php +++ b/src/Repository/GlobalMarginRepository.php @@ -16,6 +16,7 @@ use App\Entity\GlobalMargin; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\AbstractQuery; +use Doctrine\ORM\Exception\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -47,7 +48,7 @@ public function findByMinimum(): array * * @return float the margin in percent, if found; 0 otherwise * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function getMargin(float $amount): float { diff --git a/src/Repository/GroupMarginRepository.php b/src/Repository/GroupMarginRepository.php index 72c83b1a5..d09198929 100644 --- a/src/Repository/GroupMarginRepository.php +++ b/src/Repository/GroupMarginRepository.php @@ -17,6 +17,7 @@ use App\Entity\GroupMargin; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\AbstractQuery; +use Doctrine\ORM\Exception\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -39,7 +40,7 @@ public function __construct(ManagerRegistry $registry) * * @return float the margin, in percent, if found; 0 otherwise * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function getMargin(Group $group, float $amount): float { diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index af3ac7a07..e8c5a4dd2 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -17,6 +17,7 @@ use App\Interfaces\RoleInterface; use App\Utils\DateUtils; use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; @@ -170,7 +171,7 @@ public function getTableQueryBuilder(string $alias = self::DEFAULT_ALIAS): Query /** * Returns if one or more users have the reset password requested. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function isResettableUsers(): bool { diff --git a/src/Resolver/CalculationQueryResolver.php b/src/Resolver/CalculationQueryResolver.php deleted file mode 100644 index 650e353aa..000000000 --- a/src/Resolver/CalculationQueryResolver.php +++ /dev/null @@ -1,98 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace App\Resolver; - -use App\Model\CalculationGroupQuery; -use App\Model\CalculationQuery; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; -use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; - -/** - * @psalm-type GroupType = array{id: string, total: string} - * @psalm-type SourceType = array{adjust: string, userMargin: string, groups: GroupType[]} - */ -class CalculationQueryResolver implements ValueResolverInterface -{ - public function resolve(Request $request, ArgumentMetadata $argument): iterable - { - if (CalculationQuery::class !== $argument->getType()) { - return []; - } - - $source = $request->getPayload()->all(); - if ([] === $source) { - return [$this->createQuery($argument)]; - } - - /** @psalm-var SourceType $source */ - $query = new CalculationQuery( - $this->isAdjust($source), - $this->getUserMargin($source), - $this->getGroups($source) - ); - - return [$query]; - } - - /** - * @psalm-param GroupType $group - */ - private function createGroup(array $group): CalculationGroupQuery - { - return new CalculationGroupQuery( - (int) $group['id'], - (float) $group['total'] - ); - } - - private function createQuery(ArgumentMetadata $argument): CalculationQuery - { - if ($argument->hasDefaultValue()) { - /** @psalm-var CalculationQuery */ - return $argument->getDefaultValue(); - } - - return new CalculationQuery(); - } - - /** - * @psalm-param SourceType $source - * - * @return CalculationGroupQuery[] - */ - private function getGroups(array $source): array - { - return \array_map( - fn (array $group): CalculationGroupQuery => $this->createGroup($group), - $source['groups'] - ); - } - - /** - * @psalm-param SourceType $source - */ - private function getUserMargin(array $source): float - { - return (float) $source['userMargin']; - } - - /** - * @psalm-param SourceType $source - */ - private function isAdjust(array $source): bool - { - return \filter_var($source['adjust'], \FILTER_VALIDATE_BOOLEAN); - } -} diff --git a/src/Service/CalculationArchiveService.php b/src/Service/CalculationArchiveService.php index 39f6d0c7a..4d62c8f52 100644 --- a/src/Service/CalculationArchiveService.php +++ b/src/Service/CalculationArchiveService.php @@ -53,7 +53,8 @@ public function __construct( /** * Create the archive query. * - * @throws ORMException|\DateException + * @throws \DateException + * @throws ORMException */ public function createQuery(): CalculationArchiveQuery { @@ -68,7 +69,8 @@ public function createQuery(): CalculationArchiveQuery /** * Gets the maximum allowed date or null if none. * - * @throws ORMException|\DateException + * @throws \DateException + * @throws ORMException */ public function getDateMaxConstraint(): ?string { @@ -193,7 +195,8 @@ private function getCalculations(\DateTimeImmutable $date, array $sources): arra } /** - * @throws ORMException|\DateException + * @throws \DateException + * @throws ORMException */ private function getDate(): \DateTimeInterface { diff --git a/src/Service/CalculationService.php b/src/Service/CalculationService.php index 391071e2a..3dc1a35b6 100644 --- a/src/Service/CalculationService.php +++ b/src/Service/CalculationService.php @@ -18,12 +18,12 @@ use App\Entity\Group; use App\Interfaces\ConstantsInterface; use App\Interfaces\EntityInterface; -use App\Model\CalculationGroupQuery; use App\Model\CalculationQuery; use App\Repository\GlobalMarginRepository; use App\Repository\GroupMarginRepository; use App\Repository\GroupRepository; use App\Traits\MathTrait; +use Doctrine\ORM\Exception\ORMException; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -46,6 +46,8 @@ * min_margin: float, * user_margin: float, * groups: GroupType[]} + * + * @psalm-import-type QueryGroupType from CalculationQuery */ class CalculationService implements ConstantsInterface { @@ -116,7 +118,7 @@ public static function constants(): array /** * Creates groups, used to render the total view, from the given calculation. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException * * @psalm-return non-empty-array */ @@ -129,7 +131,7 @@ public function createGroups(Calculation $calculation): array $groups = \array_map( fn (CalculationGroup $group): array => $this->createGroup( id: self::ROW_GROUP, - description: $group, + entityOrId: $group, amount: $group->getAmount(), margin_percent: $group->getMargin(), margin_amount: $group->getMarginAmount(), @@ -148,7 +150,7 @@ public function createGroups(Calculation $calculation): array /** * Creates parameters, used to render the total view, for the given query. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException * * @psalm-return ParametersType */ @@ -172,8 +174,7 @@ public function createParameters(CalculationQuery $query): array if ($query->adjust && $overall_below) { $groups = $this->adjustUserMargin($groups); - $user_group = $groups[self::ROW_USER_MARGIN]; - $user_margin = $user_group['margin_percent']; + $user_margin = $groups[self::ROW_USER_MARGIN]['margin_percent']; $overall_below = false; } @@ -197,21 +198,20 @@ public function createParameters(CalculationQuery $query): array */ private function adjustUserMargin(array $groups): array { - $total_group = &$groups[self::ROW_TOTAL_GROUP]; - $total_net_group = &$groups[self::ROW_TOTAL_NET]; - $user_margin_group = &$groups[self::ROW_USER_MARGIN]; - $overall_total_group = &$groups[self::ROW_OVERALL_TOTAL]; + $total_net = $groups[self::ROW_TOTAL_NET]['total']; + $total_group = $groups[self::ROW_TOTAL_GROUP]['amount']; - $total_amount = $total_group['amount']; - $net_total = $total_net_group['total']; - $user_margin = (($total_amount * $this->getMinMargin()) - $net_total) / $net_total; - $user_margin = $this->ceil($user_margin); + $user_margin = $this->ceil((($total_group * $this->getMinMargin()) - $total_net) / $total_net); + $user_total = $this->round($total_net * $user_margin); + $user_margin_group = &$groups[self::ROW_USER_MARGIN]; $user_margin_group['margin_percent'] = $user_margin; - $user_margin_group['total'] = $net_total * $user_margin; + $user_margin_group['total'] = $user_total; - $overall_total_group['total'] = $net_total + $user_margin_group['total']; - $overall_total_group['margin_percent'] = $this->floor($overall_total_group['total'] / $total_amount); - $overall_total_group['margin_amount'] = $overall_total_group['total'] - $total_amount; + $overall_total = $total_net + $user_total; + $overall_total_group = &$groups[self::ROW_OVERALL_TOTAL]; + $overall_total_group['margin_percent'] = $this->floor($overall_total / $total_group); + $overall_total_group['margin_amount'] = $overall_total - $total_group; + $overall_total_group['total'] = $overall_total; return $groups; } @@ -225,7 +225,7 @@ private function adjustUserMargin(array $groups): array * * @psalm-return non-empty-array * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function computeGroups( array $groups, @@ -237,7 +237,7 @@ private function computeGroups( $total_net = $groups_amount + $groups_margin; $groups[self::ROW_TOTAL_GROUP] = $this->createGroup( id: self::ROW_TOTAL_GROUP, - description: 'calculation.fields.marginTotal', + entityOrId: 'calculation.fields.marginTotal', amount: $groups_amount, margin_percent: 1.0 + $this->round($this->safeDivide($groups_margin, $groups_amount)), margin_amount: $groups_margin, @@ -249,20 +249,20 @@ private function computeGroups( $total_net += $global_amount; $groups[self::ROW_GLOBAL_MARGIN] = $this->createGroup( id: self::ROW_GLOBAL_MARGIN, - description: 'calculation.fields.globalMargin', + entityOrId: 'calculation.fields.globalMargin', margin_percent: $global_margin, total: $global_amount ); $groups[self::ROW_TOTAL_NET] = $this->createGroup( id: self::ROW_TOTAL_NET, - description: 'calculation.fields.totalNet', + entityOrId: 'calculation.fields.totalNet', total: $total_net ); $user_amount = $this->round($total_net * $user_margin); $groups[self::ROW_USER_MARGIN] = $this->createGroup( id: self::ROW_USER_MARGIN, - description: 'calculation.fields.userMargin', + entityOrId: 'calculation.fields.userMargin', margin_percent: $user_margin, total: $user_amount ); @@ -273,7 +273,7 @@ private function computeGroups( $overall_margin = $this->floor(1.0 + $overall_margin); $groups[self::ROW_OVERALL_TOTAL] = $this->createGroup( id: self::ROW_OVERALL_TOTAL, - description: 'calculation.fields.overallTotal', + entityOrId: 'calculation.fields.overallTotal', amount: $groups_amount, margin_percent: $overall_margin, margin_amount: $overall_amount, @@ -284,38 +284,42 @@ private function computeGroups( } /** - * @param CalculationGroupQuery[] $queryGroups + * @param QueryGroupType[] $queryGroups * * @return GroupType[] * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function convertQueryGroups(array $queryGroups): array { $groups = []; - foreach ($queryGroups as $group) { - if ($this->isFloatZero($group->total)) { + foreach ($queryGroups as $queryGroup) { + if ($this->isFloatZero($queryGroup['total'])) { continue; } - $id = $group->id; - $targetGroup = $this->findGroup($id); - if (!$targetGroup instanceof Group) { + $id = $queryGroup['id']; + $group = $this->findGroup($id); + if (!$group instanceof Group) { continue; } if (!\array_key_exists($id, $groups)) { - $groups[$id] = $this->createGroup(self::ROW_GROUP, $targetGroup); + $groups[$id] = $this->createGroup(self::ROW_GROUP, $group); } $current = &$groups[$id]; - $amount = $current['amount'] + $group->total; - $margin_percent = $this->getGroupMargin($targetGroup, $amount); + $amount = $current['amount'] + $queryGroup['total']; + $margin_percent = $this->getGroupMargin($group, $amount); $total = $this->round($margin_percent * $amount); - $current['amount'] = $amount; - $current['margin_percent'] = $margin_percent; - $current['margin_amount'] = $total - $amount; - $current['total'] = $total; + $margin_amount = $total - $amount; + + $current = \array_merge($current, [ + 'margin_percent' => $margin_percent, + 'margin_amount' => $margin_amount, + 'amount' => $amount, + 'total' => $total, + ]); } return $groups; @@ -352,16 +356,16 @@ private function createEmptyParameters(): array */ private function createGroup( int $id, - EntityInterface|string $description, + EntityInterface|string $entityOrId, float $amount = 0.0, float $margin_percent = 0.0, float $margin_amount = 0.0, float $total = 0.0 ): array { - if ($description instanceof EntityInterface) { - $description = (string) $description; + if ($entityOrId instanceof EntityInterface) { + $description = $entityOrId->getDisplay(); } else { - $description = $this->translator->trans($description); + $description = $this->translator->trans($entityOrId); } return [ @@ -385,7 +389,7 @@ private function findGroup(int $id): ?Group /** * Gets the global margin, in percent, for the given amount. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getGlobalMargin(float $amount): float { @@ -395,7 +399,7 @@ private function getGlobalMargin(float $amount): float /** * Gets the margin, in percent, for the given group and amount. * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ private function getGroupMargin(Group $group, float $amount): float { diff --git a/src/Service/CalculationUpdateService.php b/src/Service/CalculationUpdateService.php index 2b4fb3123..f743d9b7d 100644 --- a/src/Service/CalculationUpdateService.php +++ b/src/Service/CalculationUpdateService.php @@ -76,7 +76,8 @@ public function saveQuery(CalculationUpdateQuery $query): void } /** - * @throws ORMException|\Exception + * @throws \Exception + * @throws ORMException */ public function update(CalculationUpdateQuery $query): CalculationUpdateResult { @@ -109,7 +110,7 @@ public function update(CalculationUpdateQuery $query): CalculationUpdateResult } /** - * Update the calculation's total. + * Update the given calculation's total. * * @return bool true if updated; false otherwise * diff --git a/src/Table/CalculationBelowTable.php b/src/Table/CalculationBelowTable.php index b45c7a2fa..97310533f 100644 --- a/src/Table/CalculationBelowTable.php +++ b/src/Table/CalculationBelowTable.php @@ -17,6 +17,7 @@ use App\Repository\CalculationRepository; use App\Repository\CalculationStateRepository; use App\Service\ApplicationService; +use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\QueryBuilder; use Twig\Environment; @@ -37,7 +38,7 @@ public function __construct( /** * @return int<0, max> * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function count(): int { @@ -45,7 +46,7 @@ public function count(): int } /** - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function getEmptyMessage(): ?string { diff --git a/src/Table/CalculationDuplicateTable.php b/src/Table/CalculationDuplicateTable.php index 98542a8a0..13276cf48 100644 --- a/src/Table/CalculationDuplicateTable.php +++ b/src/Table/CalculationDuplicateTable.php @@ -14,6 +14,7 @@ namespace App\Table; use App\Traits\DuplicateItemsTrait; +use Doctrine\ORM\Exception\ORMException; /** * Calculation table for duplicate items. @@ -27,7 +28,7 @@ class CalculationDuplicateTable extends AbstractCalculationItemsTable /** * @return int<0, max> * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function count(): int { @@ -35,7 +36,7 @@ public function count(): int } /** - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function getEmptyMessage(): ?string { diff --git a/src/Table/CalculationEmptyTable.php b/src/Table/CalculationEmptyTable.php index a94379645..7a7ee056d 100644 --- a/src/Table/CalculationEmptyTable.php +++ b/src/Table/CalculationEmptyTable.php @@ -15,6 +15,7 @@ use App\Repository\CalculationRepository; use App\Traits\EmptyItemsTrait; +use Doctrine\ORM\Exception\ORMException; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -46,7 +47,7 @@ public function __construct(CalculationRepository $repository, TranslatorInterfa /** * @return int<0, max> * - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function count(): int { @@ -54,7 +55,7 @@ public function count(): int } /** - * @throws \Doctrine\ORM\Exception\ORMException + * @throws ORMException */ public function getEmptyMessage(): ?string { diff --git a/src/Traits/RightsTrait.php b/src/Traits/RightsTrait.php index d605fd99d..8b675278d 100644 --- a/src/Traits/RightsTrait.php +++ b/src/Traits/RightsTrait.php @@ -52,7 +52,7 @@ trait RightsTrait /** * @return ?FlagBag */ - public function __get(string $name): mixed + public function __get(string $name): ?FlagBag { $entity = EntityName::tryFromField($name); diff --git a/tests/Controller/AjaxCalculationControllerTest.php b/tests/Controller/AjaxCalculationControllerTest.php index 9c247dad6..a60a7c5c1 100644 --- a/tests/Controller/AjaxCalculationControllerTest.php +++ b/tests/Controller/AjaxCalculationControllerTest.php @@ -44,16 +44,16 @@ public static function getRoutes(): \Iterator public function testUpdate(): void { - $parameters = [ + $parameter = \json_encode([ 'adjust' => true, 'userMargin' => -0.16, 'groups' => [ ['id' => 10, 'total' => 83.5], ], - ]; + ]); $this->loginUsername('ROLE_USER'); - $this->client->request(Request::METHOD_POST, self::UPDATE_ROUTE_NAME, $parameters); + $this->client->request(Request::METHOD_POST, self::UPDATE_ROUTE_NAME, [$parameter]); $response = $this->client->getResponse(); self::assertTrue($response->isOk()); self::assertInstanceOf(JsonResponse::class, $response); diff --git a/tests/Resolver/CalculationQueryResolverTest.php b/tests/Resolver/CalculationQueryResolverTest.php deleted file mode 100644 index 78173487e..000000000 --- a/tests/Resolver/CalculationQueryResolverTest.php +++ /dev/null @@ -1,143 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace App\Tests\Resolver; - -use App\Model\CalculationQuery; -use App\Resolver\CalculationQueryResolver; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; - -class CalculationQueryResolverTest extends TestCase -{ - /** - * @throws Exception - */ - public function testDefault(): void - { - $resolver = $this->createResolver(); - $request = $this->createRequest(); - $argument = $this->createArgumentMetadata(); - - $actual = $resolver->resolve($request, $argument); - self::assertIsArray($actual); - self::assertCount(1, $actual); - - $query = $actual[0]; - self::assertInstanceOf(CalculationQuery::class, $query); - self::assertFalse($query->adjust); - self::assertSame(0.0, $query->userMargin); - self::assertSame([], $query->groups); - } - - /** - * @throws Exception - */ - public function testInvalidType(): void - { - $resolver = $this->createResolver(); - $request = $this->createRequest(); - $argument = $this->createArgumentMetadata(Request::class); - - $actual = $resolver->resolve($request, $argument); - self::assertIsArray($actual); - self::assertCount(0, $actual); - } - - /** - * @throws Exception - */ - public function testWithDefaultParams(): void - { - $parameters = [ - 'adjust' => true, - 'userMargin' => 0.1, - 'groups' => [ - [ - 'id' => 10, - 'total' => 50, - ], - ], - ]; - - $resolver = $this->createResolver(); - $argument = $this->createArgumentMetadata(); - $request = $this->createRequest($parameters); - - $actual = $resolver->resolve($request, $argument); - self::assertIsArray($actual); - self::assertCount(1, $actual); - - $query = $actual[0]; - self::assertInstanceOf(CalculationQuery::class, $query); - self::assertTrue($query->adjust); - self::assertSame(0.1, $query->userMargin); - - self::assertCount(1, $query->groups); - $group = $query->groups[0]; - self::assertSame(10, $group->id); - self::assertSame(50.0, $group->total); - } - - /** - * @throws Exception - */ - public function testWithDefaultValue(): void - { - $resolver = $this->createResolver(); - $request = $this->createRequest(); - $argument = $this->createArgumentMetadata(); - $argument->expects(self::once()) - ->method('hasDefaultValue') - ->willReturn(true); - $argument->expects(self::once()) - ->method('getDefaultValue') - ->willReturn(new CalculationQuery()); - - $actual = $resolver->resolve($request, $argument); - self::assertIsArray($actual); - self::assertCount(1, $actual); - $query = $actual[0]; - self::assertInstanceOf(CalculationQuery::class, $query); - } - - /** - * @throws Exception - */ - private function createArgumentMetadata(string $type = CalculationQuery::class): MockObject&ArgumentMetadata - { - $argument = $this->createMock(ArgumentMetadata::class); - $argument->expects(self::once()) - ->method('getType') - ->willReturn($type); - - return $argument; - } - - private function createRequest(array $parameters = []): Request - { - return Request::create( - uri: '/', - method: Request::METHOD_POST, - parameters: $parameters - ); - } - - private function createResolver(): CalculationQueryResolver - { - return new CalculationQueryResolver(); - } -} diff --git a/tests/Service/CalculationServiceTest.php b/tests/Service/CalculationServiceTest.php index 1c9bd2cb7..c96306ca8 100644 --- a/tests/Service/CalculationServiceTest.php +++ b/tests/Service/CalculationServiceTest.php @@ -20,7 +20,6 @@ use App\Entity\GroupMargin; use App\Entity\Product; use App\Interfaces\EntityInterface; -use App\Model\CalculationGroupQuery; use App\Model\CalculationQuery; use App\Repository\GlobalMarginRepository; use App\Repository\GroupMarginRepository; @@ -57,7 +56,7 @@ public function testAdjustUserMargin(): void adjust: true, userMargin: -0.99, groups: [ - new CalculationGroupQuery(1, 100.0), + ['id' => 1, 'total' => 100.0], ] ); @@ -126,7 +125,7 @@ public function testCreateGroupsFromQuery(): void adjust: false, userMargin: 0.05, groups: [ - new CalculationGroupQuery(1, 2.5), + ['id' => 1, 'total' => 2.5], ] ); @@ -164,7 +163,7 @@ public function testCreateGroupsFromQueryGroupNotFound(): void adjust: false, userMargin: 0.05, groups: [ - new CalculationGroupQuery(10, 10.0), + ['id' => 10, 'total' => 10.0], ] ); @@ -183,7 +182,7 @@ public function testCreateGroupsFromQueryGroupTotalZero(): void adjust: false, userMargin: 0.05, groups: [ - new CalculationGroupQuery(1, 0.0), + ['id' => 1, 'total' => 0.0], ] ); diff --git a/vendor-bin/phpstan/composer.lock b/vendor-bin/phpstan/composer.lock index 262c66a01..c1411c43c 100644 --- a/vendor-bin/phpstan/composer.lock +++ b/vendor-bin/phpstan/composer.lock @@ -283,16 +283,16 @@ }, { "name": "phpstan/phpstan-strict-rules", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-strict-rules.git", - "reference": "02277ce4b0dd03d74f15282064f8f027d1dec9cc" + "reference": "8b88b5f818bfa301e0c99154ab622dace071c3ba" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/02277ce4b0dd03d74f15282064f8f027d1dec9cc", - "reference": "02277ce4b0dd03d74f15282064f8f027d1dec9cc", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/8b88b5f818bfa301e0c99154ab622dace071c3ba", + "reference": "8b88b5f818bfa301e0c99154ab622dace071c3ba", "shasum": "" }, "require": { @@ -325,9 +325,9 @@ "description": "Extra strict and opinionated rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", - "source": "https://github.com/phpstan/phpstan-strict-rules/tree/2.0.2" + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/2.0.3" }, - "time": "2025-01-19T13:03:11+00:00" + "time": "2025-01-21T10:52:14+00:00" } ], "packages-dev": [],