From 6fc9847be3b5a414af050ec41cb4efa41898d57b Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Fri, 23 Aug 2024 09:45:19 +0200 Subject: [PATCH] phpstan fixes --- .../Controllers/Crud/ActivitiesController.php | 8 +++---- .../Crud/SubscriptionsController.php | 14 ++++++------- lib/GaletteActivities/Entity/Activity.php | 13 +++++------- lib/GaletteActivities/Entity/Subscription.php | 21 ++++++++----------- .../Filters/SubscriptionsList.php | 2 ++ .../Repository/Activities.php | 2 +- .../Repository/Subscriptions.php | 7 ++----- phpstan.neon | 18 ++++++++++++++++ .../Entity/tests/units/Activity.php | 8 +++---- .../Entity/tests/units/Subscription.php | 10 ++++----- 10 files changed, 57 insertions(+), 46 deletions(-) create mode 100644 phpstan.neon diff --git a/lib/GaletteActivities/Controllers/Crud/ActivitiesController.php b/lib/GaletteActivities/Controllers/Crud/ActivitiesController.php index 12bf14b..eb2c10b 100644 --- a/lib/GaletteActivities/Controllers/Crud/ActivitiesController.php +++ b/lib/GaletteActivities/Controllers/Crud/ActivitiesController.php @@ -188,7 +188,7 @@ public function edit(Request $request, Response $response, int $id = null, strin $activity = $this->session->activity; $this->session->activity = null; } else { - $activity = new Activity($this->zdb, $this->login); + $activity = new Activity($this->zdb); } if ($id !== null && $activity->getId() != $id) { @@ -236,7 +236,7 @@ public function edit(Request $request, Response $response, int $id = null, strin public function doEdit(Request $request, Response $response, int $id = null, string $action = 'edit'): Response { $post = $request->getParsedBody(); - $activity = new Activity($this->zdb, $this->login); + $activity = new Activity($this->zdb); if (isset($post['id']) && !empty($post['id'])) { $activity->load((int)$post['id']); } @@ -350,7 +350,7 @@ public function formUri(array $args): string */ public function confirmRemoveTitle(array $args): string { - $activity = new Activity($this->zdb, $this->login, (int)$args['id']); + $activity = new Activity($this->zdb, (int)$args['id']); return sprintf( //TRANS %1$s is activity name _T('Remove activity %1$s', 'activities'), @@ -368,7 +368,7 @@ public function confirmRemoveTitle(array $args): string */ protected function doDelete(array $args, array $post): bool { - $activity = new Activity($this->zdb, $this->login, (int)$args['id']); + $activity = new Activity($this->zdb, (int)$args['id']); return $activity->remove(); } diff --git a/lib/GaletteActivities/Controllers/Crud/SubscriptionsController.php b/lib/GaletteActivities/Controllers/Crud/SubscriptionsController.php index a643a3a..f4b69eb 100644 --- a/lib/GaletteActivities/Controllers/Crud/SubscriptionsController.php +++ b/lib/GaletteActivities/Controllers/Crud/SubscriptionsController.php @@ -112,14 +112,14 @@ public function list(Request $request, Response $response, string $option = null $activity = null; if ($filters->activity_filter) { - $activity = new Activity($this->zdb, $this->login, (int)$filters->activity_filter); + $activity = new Activity($this->zdb, (int)$filters->activity_filter); } //Groups $groups = new Groups($this->zdb, $this->login); $groups_list = $groups->getList(); - $subscriptions = new Subscriptions($this->zdb, $this->login, $filters); + $subscriptions = new Subscriptions($this->zdb, $filters); $activities = new Activities($this->zdb, $this->login, $this->preferences); $list = $subscriptions->getList(); @@ -258,7 +258,7 @@ public function edit(Request $request, Response $response, int $id = null, strin $subscription = $this->session->subscription; $this->session->subscription = null; } else { - $subscription = new Subscription($this->zdb, $this->login); + $subscription = new Subscription($this->zdb); } if ($id !== null && $subscription->getId() != $id) { @@ -309,7 +309,7 @@ public function edit(Request $request, Response $response, int $id = null, strin 'autocomplete' => true, 'page_title' => $title, 'subscription' => $subscription, - 'activities' => $activities->getList(true), + 'activities' => $activities->getList(), 'require_dialog' => true, 'require_calendar' => true, // pseudo random int @@ -333,7 +333,7 @@ public function edit(Request $request, Response $response, int $id = null, strin public function doEdit(Request $request, Response $response, int $id = null, string $action = 'edit'): Response { $post = $request->getParsedBody(); - $subscription = new Subscription($this->zdb, $this->login); + $subscription = new Subscription($this->zdb); if (isset($post['id']) && !empty($post['id'])) { $subscription->load((int)$post['id']); } @@ -483,7 +483,7 @@ public function formUri(array $args): string */ public function confirmRemoveTitle(array $args): string { - $subscription = new Subscription($this->zdb, $this->login, (int)$args['id']); + $subscription = new Subscription($this->zdb, (int)$args['id']); $member = $subscription->getMember(); $activity = $subscription->getActivity(); return sprintf( @@ -504,7 +504,7 @@ public function confirmRemoveTitle(array $args): string */ protected function doDelete(array $args, array $post): bool { - $subscription = new Subscription($this->zdb, $this->login, (int)$post['id']); + $subscription = new Subscription($this->zdb, (int)$post['id']); return $subscription->remove(); } diff --git a/lib/GaletteActivities/Entity/Activity.php b/lib/GaletteActivities/Entity/Activity.php index b70eaae..8b58f2b 100644 --- a/lib/GaletteActivities/Entity/Activity.php +++ b/lib/GaletteActivities/Entity/Activity.php @@ -44,7 +44,6 @@ class Activity public const PK = 'id_activity'; private Db $zdb; - private Login $login; /** @var array */ private array $errors = []; @@ -60,16 +59,14 @@ class Activity /** * Default constructor * - * @param Db $zdb Database instance - * @param Login $login Login instance - * @param null|int|ArrayObject $args Either a ResultSet row or its id for to load - * a specific activity, or null to just - * instanciate object + * @param Db $zdb Database instance + * @param null|int|ArrayObject $args Either a ResultSet row or its id for to load + * a specific activity, or null to just + * instanciate object */ - public function __construct(Db $zdb, Login $login, int|ArrayObject $args = null) + public function __construct(Db $zdb, int|ArrayObject $args = null) { $this->zdb = $zdb; - $this->login = $login; $this->setFields(); if (is_int($args) && $args > 0) { diff --git a/lib/GaletteActivities/Entity/Subscription.php b/lib/GaletteActivities/Entity/Subscription.php index 000b840..08fadb4 100644 --- a/lib/GaletteActivities/Entity/Subscription.php +++ b/lib/GaletteActivities/Entity/Subscription.php @@ -45,7 +45,6 @@ class Subscription public const PK = 'id_subscription'; private Db $zdb; - private Login $login; /** @var array */ private array $errors; @@ -65,16 +64,14 @@ class Subscription /** * Default constructor * - * @param Db $zdb Database instance - * @param Login $login Login instance - * @param null|int|ArrayObject $args Either a ResultSet row or its id for to load - * a specific subscription, or null to just - * instanciate object + * @param Db $zdb Database instance + * @param null|int|ArrayObject $args Either a ResultSet row or its id for to load + * a specific subscription, or null to just + * instanciate object */ - public function __construct(Db $zdb, Login $login, int|ArrayObject|null $args = null) + public function __construct(Db $zdb, int|ArrayObject|null $args = null) { $this->zdb = $zdb; - $this->login = $login; $this->setFields(); if (is_int($args)) { @@ -179,9 +176,9 @@ public function remove(): bool * @param array $values All values to check, basically the $_POST array * after sending the form * - * @return true|array + * @return boolean */ - public function check(array $values): array|bool + public function check(array $values): bool { $this->errors = array(); @@ -356,7 +353,7 @@ public function getActivityId(): ?int public function getActivity(): ?Activity { if (isset($this->id_activity)) { - $this->activity = new Activity($this->zdb, $this->login, $this->id_activity); + $this->activity = new Activity($this->zdb, $this->id_activity); } return $this->activity; } @@ -471,7 +468,7 @@ public function getEndDate(bool $formatted = true): string public function setActivity(int $activity): self { $this->id_activity = $activity; - $this->activity = new Activity($this->zdb, $this->login, $this->id_activity); + $this->activity = new Activity($this->zdb, $this->id_activity); return $this; } diff --git a/lib/GaletteActivities/Filters/SubscriptionsList.php b/lib/GaletteActivities/Filters/SubscriptionsList.php index a0d42e1..941a985 100644 --- a/lib/GaletteActivities/Filters/SubscriptionsList.php +++ b/lib/GaletteActivities/Filters/SubscriptionsList.php @@ -42,6 +42,8 @@ * @property int $paid_filter * @property int $payment_type_filter * @property int $date_field + * @property array $selected + * @property string $query */ class SubscriptionsList extends Pagination { diff --git a/lib/GaletteActivities/Repository/Activities.php b/lib/GaletteActivities/Repository/Activities.php index f8f3634..19fc532 100644 --- a/lib/GaletteActivities/Repository/Activities.php +++ b/lib/GaletteActivities/Repository/Activities.php @@ -86,7 +86,7 @@ public function getList(): array|ResultSet $activities = []; foreach ($results as $row) { - $activity = new Activity($this->zdb, $this->login, $row); + $activity = new Activity($this->zdb, $row); $activities[] = $activity; } diff --git a/lib/GaletteActivities/Repository/Subscriptions.php b/lib/GaletteActivities/Repository/Subscriptions.php index b4e5eee..3663492 100644 --- a/lib/GaletteActivities/Repository/Subscriptions.php +++ b/lib/GaletteActivities/Repository/Subscriptions.php @@ -45,7 +45,6 @@ class Subscriptions { private Db $zdb; - private Login $login; private SubscriptionsList $filters; private int $count; private float $sum; @@ -64,13 +63,11 @@ class Subscriptions * Constructor * * @param Db $zdb Database instance - * @param Login $login Login instance * @param ?SubscriptionsList $filters Filtering */ - public function __construct(Db $zdb, Login $login, SubscriptionsList $filters = null) + public function __construct(Db $zdb, SubscriptionsList $filters = null) { $this->zdb = $zdb; - $this->login = $login; if ($filters === null) { $this->filters = new SubscriptionsList(); @@ -102,7 +99,7 @@ public function getList(bool $full = false): array $subscriptions = []; foreach ($results as $row) { - $subscription = new Subscription($this->zdb, $this->login, $row); + $subscription = new Subscription($this->zdb, $row); $subscriptions[] = $subscription; } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..9817277 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,18 @@ +parameters: + parallel: + maximumNumberOfProcesses: 2 + level: 5 + paths: + - lib/ + scanFiles: + - _config.inc.php + - ../../includes/sys_config/paths.inc.php + - ../../config/config.inc.php.dist + - ../../includes/sys_config/galette_tcpdf_config.php + scanDirectories: + - ../../includes/ + - ../../lib/ + universalObjectCratesClasses: + - RKA\Session + ignoreErrors: + - '/Access to an undefined property ArrayObject.*/' diff --git a/tests/GaletteActivities/Entity/tests/units/Activity.php b/tests/GaletteActivities/Entity/tests/units/Activity.php index e8a2f34..c12486e 100644 --- a/tests/GaletteActivities/Entity/tests/units/Activity.php +++ b/tests/GaletteActivities/Entity/tests/units/Activity.php @@ -57,7 +57,7 @@ public function tearDown(): void */ public function testEmpty(): void { - $activity = new \GaletteActivities\Entity\Activity($this->zdb, $this->login); + $activity = new \GaletteActivities\Entity\Activity($this->zdb); $this->assertNull($activity->getId()); $this->assertSame('',$activity->getName()); @@ -73,7 +73,7 @@ public function testEmpty(): void */ public function testCrud(): void { - $activity = new \GaletteActivities\Entity\Activity($this->zdb, $this->login); + $activity = new \GaletteActivities\Entity\Activity($this->zdb); $activities = new \GaletteActivities\Repository\Activities($this->zdb, $this->login, $this->preferences); //ensure the table is empty @@ -140,7 +140,7 @@ public function testCrud(): void $data['id_group'] = $group->getId(); $this->assertTrue($activity->check($data)); $this->assertTrue($activity->store()); - $activity = new \GaletteActivities\Entity\Activity($this->zdb, $this->login, $first_id); + $activity = new \GaletteActivities\Entity\Activity($this->zdb, $first_id); $this->assertInstanceOf(\Galette\Entity\Group::class, $activity->getGroup()); $this->assertSame($group->getId(), $activity->getGroup()->getId()); @@ -157,7 +157,7 @@ public function testCrud(): void */ public function testLoadError(): void { - $activity = new \GaletteActivities\Entity\Activity($this->zdb, $this->login); + $activity = new \GaletteActivities\Entity\Activity($this->zdb); $this->assertFalse($activity->load(999)); } } diff --git a/tests/GaletteActivities/Entity/tests/units/Subscription.php b/tests/GaletteActivities/Entity/tests/units/Subscription.php index 8aeea83..428b160 100644 --- a/tests/GaletteActivities/Entity/tests/units/Subscription.php +++ b/tests/GaletteActivities/Entity/tests/units/Subscription.php @@ -60,7 +60,7 @@ public function tearDown(): void */ public function testEmpty(): void { - $subscription = new \GaletteActivities\Entity\Subscription($this->zdb, $this->login); + $subscription = new \GaletteActivities\Entity\Subscription($this->zdb); $this->assertNull($subscription->getId()); $this->assertNull($subscription->getActivityId()); @@ -85,14 +85,14 @@ public function testEmpty(): void */ public function testCrud(): void { - $subscription = new \GaletteActivities\Entity\Subscription($this->zdb, $this->login); + $subscription = new \GaletteActivities\Entity\Subscription($this->zdb); $subscriptions = new \GaletteActivities\Repository\Activities($this->zdb, $this->login, $this->preferences); //ensure the table is empty $this->assertCount(0, $subscriptions->getList()); //bootstrap data - $activity = new \GaletteActivities\Entity\Activity($this->zdb, $this->login); + $activity = new \GaletteActivities\Entity\Activity($this->zdb); $data = [ 'name' => 'Activity for subscriptions', 'comment' => 'Comment ' . $this->seed, @@ -186,7 +186,7 @@ public function testCrud(): void $this->assertSame($member_one->id, $subscription->getMember()->id); //reload - $subscription = new \GaletteActivities\Entity\Subscription($this->zdb, $this->login, $subscription_id); + $subscription = new \GaletteActivities\Entity\Subscription($this->zdb, $subscription_id); $data += [ 'paid' => 1, 'payment_amount' => 21.0, @@ -213,7 +213,7 @@ public function testCrud(): void */ public function testLoadError(): void { - $subscription = new \GaletteActivities\Entity\Subscription($this->zdb, $this->login); + $subscription = new \GaletteActivities\Entity\Subscription($this->zdb); $this->assertFalse($subscription->load(999)); } }