From 2c51bae75a6a620df1292663ccd51fa267999eb6 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 25 Jan 2024 08:46:20 +0100 Subject: [PATCH] Typehint --- .../Controllers/Crud/ObjectsController.php | 11 +- .../Controllers/ImagesController.php | 2 +- .../Controllers/MainController.php | 2 +- .../Entity/LendCategory.php | 78 ++++--- lib/GaletteObjectsLend/Entity/LendObject.php | 191 +++++++++--------- lib/GaletteObjectsLend/Entity/LendPDF.php | 4 +- lib/GaletteObjectsLend/Entity/LendRent.php | 80 ++++---- lib/GaletteObjectsLend/Entity/LendStatus.php | 58 +++--- lib/GaletteObjectsLend/Entity/Picture.php | 21 +- lib/GaletteObjectsLend/Entity/Preferences.php | 29 +-- .../Filters/CategoriesList.php | 10 +- .../Filters/ObjectsList.php | 18 +- lib/GaletteObjectsLend/Filters/StatusList.php | 8 +- lib/GaletteObjectsLend/IO/PdfObject.php | 14 +- lib/GaletteObjectsLend/IO/PdfObjects.php | 13 +- .../Repository/Categories.php | 80 ++++---- lib/GaletteObjectsLend/Repository/Objects.php | 95 +++++---- lib/GaletteObjectsLend/Repository/Status.php | 76 +++---- 18 files changed, 396 insertions(+), 394 deletions(-) diff --git a/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php b/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php index cb61eea..45ffc23 100644 --- a/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php +++ b/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php @@ -147,7 +147,7 @@ public function list(Request $request, Response $response, string $option = null 'objects' => $list, 'nb_objects' => count($list), 'filters' => $filters, - 'lendsprefs' => $lendsprefs->getpreferences(), + 'lendsprefs' => $lendsprefs->getPreferences(), 'olendsprefs' => $lendsprefs, 'time' => time(), 'module_id' => $this->getModuleId(), @@ -279,10 +279,7 @@ public function handleBatch(Request $request, Response $response): Response ->withHeader('Location', $this->routeparser->urlFor('objectslend_objects_print')); } - $this->flash->addMessage( - 'error_detected', - _T("No action was found. Please contact plugin developpers.") - ); + throw new \RuntimeException('Does not know what to batch :('); } else { $this->flash->addMessage( 'error_detected', @@ -338,7 +335,7 @@ public function edit(Request $request, Response $response, int $id = null, strin 'object' => $object, 'time' => time(), 'action' => $action, - 'lendsprefs' => $lendsprefs->getpreferences(), + 'lendsprefs' => $lendsprefs->getPreferences(), 'olendsprefs' => $lendsprefs, 'categories' => $categories_list, 'statuses' => $slist @@ -590,7 +587,7 @@ public function lend(Request $request, Response $response, string $action, int $ 'statuses' => ($action == 'take' ? LendStatus::getActiveTakeAwayStatuses($this->zdb) : LendStatus::getActiveStockStatuses($this->zdb)), - 'lendsprefs' => $lendsprefs->getpreferences(), + 'lendsprefs' => $lendsprefs->getPreferences(), 'olendsprefs' => $lendsprefs, 'ajax' => $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest', 'takeorgive' => $action, diff --git a/lib/GaletteObjectsLend/Controllers/ImagesController.php b/lib/GaletteObjectsLend/Controllers/ImagesController.php index 08b74c0..a72937d 100644 --- a/lib/GaletteObjectsLend/Controllers/ImagesController.php +++ b/lib/GaletteObjectsLend/Controllers/ImagesController.php @@ -43,7 +43,7 @@ class ImagesController extends GImagesController * @param Response $response PSR Response * @param string $type Requested type (category or object) * @param string $mode Either thumbnail or photo - * @param int $id Object id + * @param ?int $id Object id * * @return Response */ diff --git a/lib/GaletteObjectsLend/Controllers/MainController.php b/lib/GaletteObjectsLend/Controllers/MainController.php index 5d4ece5..2cef51c 100644 --- a/lib/GaletteObjectsLend/Controllers/MainController.php +++ b/lib/GaletteObjectsLend/Controllers/MainController.php @@ -64,7 +64,7 @@ public function preferences(Request $request, Response $response): Response $params = [ 'page_title' => _T('ObjectsLend preferences', 'objectslend'), 'ctypes' => $ctypes->getList(), - 'lendsprefs' => $lendsprefs->getpreferences() + 'lendsprefs' => $lendsprefs->getPreferences() ]; // display page diff --git a/lib/GaletteObjectsLend/Entity/LendCategory.php b/lib/GaletteObjectsLend/Entity/LendCategory.php index 5b03d3e..e7a234f 100644 --- a/lib/GaletteObjectsLend/Entity/LendCategory.php +++ b/lib/GaletteObjectsLend/Entity/LendCategory.php @@ -43,50 +43,47 @@ class LendCategory public const TABLE = 'category'; public const PK = 'category_id'; - private $fields = array( + /** @var array */ + private array $fields = array( 'category_id' => 'integer', 'name' => 'varchar(100)', 'is_active' => 'boolean' ); - private $category_id; - private $name = ''; - private $is_active = true; - private $objects_nb = 0; - private $objects_price_sum = 0; + private int $category_id; + private string $name = ''; + private bool $is_active = true; + private int $objects_nb = 0; + private float $objects_price_sum = 0.0; // Used to have an url for the image - private $categ_image_url = ''; - private $picture; + private string $categ_image_url = ''; + private CategoryPicture $picture; - private $deps = [ + /** @var array */ + private array $deps = [ 'picture' => true ]; - private $zdb; - private $plugins; + private Db $zdb; + private Plugins $plugins; /** * Default constructor * - * @param Db $zdb Database instance - * @param Plugins $plugins Pluginsugins instance - * @param int|object $args Maybe null, an RS object or an id from database - * @param array $deps Dependencies configuration, see LendCategory::$deps + * @param Db $zdb Database instance + * @param Plugins $plugins Plugins instance + * @param int|ArrayObject|null $args Maybe null, an RS object or an id from database + * @param array $deps Dependencies configuration, see LendCategory::$deps */ - public function __construct(Db $zdb, Plugins $plugins, $args = null, $deps = null) + public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject $args = null, array $deps = null) { $this->zdb = $zdb; $this->plugins = $plugins; - if ($deps !== null && is_array($deps)) { + if ($deps !== null) { $this->deps = array_merge( $this->deps, $deps ); - } elseif ($deps !== null) { - Analog::log( - '$deps should be an array, ' . gettype($deps) . ' given!', - Analog::WARNING - ); } if ($this->deps['picture'] === true) { @@ -116,11 +113,11 @@ public function __construct(Db $zdb, Plugins $plugins, $args = null, $deps = nul /** * Populate object from a resultset row * - * @param ArrayObject $r the resultset row + * @param ArrayObject $r the resultset row * * @return void */ - private function loadFromRS($r) + private function loadFromRS(ArrayObject $r): void { $this->category_id = $r->category_id; $this->name = $r->name; @@ -141,11 +138,11 @@ private function loadFromRS($r) } /** - * Enregistre l'élément en cours que ce soit en insert ou update + * Store category * - * @return bool False si l'enregistrement a échoué, true si aucune erreur + * @return bool */ - public function store() + public function store(): bool { try { $values = array(); @@ -166,6 +163,7 @@ public function store() $result = $this->zdb->execute($insert); if ($result->count() > 0) { if ($this->zdb->isPostgres()) { + /** @phpstan-ignore-next-line */ $this->category_id = $this->zdb->driver->getLastGeneratedValue( PREFIX_DB . 'lend_category_id_seq' ); @@ -173,7 +171,7 @@ public function store() $this->category_id = $this->zdb->driver->getLastGeneratedValue(); } } else { - throw new \RuntimeException('Unable to add catagory!'); + throw new \RuntimeException('Unable to add category!'); } } else { $update = $this->zdb->update(LEND_PREFIX . self::TABLE) @@ -193,11 +191,11 @@ public function store() } /** - * Drop a category. All objects for removed catagory will be assigned to none. + * Drop a category. All objects for removed category will be assigned to none. * * @return boolean */ - public function delete() + public function delete(): bool { try { $this->zdb->connection->beginTransaction(); @@ -235,7 +233,7 @@ public function delete() * * @return string */ - public function getName($count = true) + public function getName(bool $count = true): string { $name = $this->name !== null ? $this->name : _T("No category", "objectslend"); @@ -253,14 +251,14 @@ public function getName($count = true) * * @return mixed the called property */ - public function __get($name) + public function __get(string $name) { switch ($name) { case 'objects_price_sum': return number_format($this->$name, 2, ',', ''); case 'is_active': default: - return $this->$name; + return $this->$name ?? null; } } @@ -272,7 +270,7 @@ public function __get($name) * * @return void */ - public function __set($name, $value) + public function __set(string $name, $value): void { $this->$name = $value; } @@ -280,11 +278,11 @@ public function __set($name, $value) /** * Get object ID * - * @return int + * @return ?int */ - public function getId(): int + public function getId(): ?int { - return (int)$this->category_id; + return $this->category_id ?? null; } /** @@ -294,7 +292,7 @@ public function getId(): int */ public function isActive(): bool { - return (bool)$this->is_active; + return $this->is_active; } /** @@ -330,11 +328,11 @@ public function getObjectsNb(): int /** * Generic isset function * - * @param $name Property name + * @param string $name Property name * * @return bool */ - public function __isset($name) + public function __isset(string $name): bool { return property_exists($this, $name); } diff --git a/lib/GaletteObjectsLend/Entity/LendObject.php b/lib/GaletteObjectsLend/Entity/LendObject.php index a741429..cb1ef31 100644 --- a/lib/GaletteObjectsLend/Entity/LendObject.php +++ b/lib/GaletteObjectsLend/Entity/LendObject.php @@ -54,13 +54,18 @@ * @property string $date_forecast * @property array $rents * @property int $category_id + * @property string $nom_adh + * @property string $prenom_adh + * @property string $currency + * @property bool $in_stock */ class LendObject { public const TABLE = 'objects'; public const PK = 'object_id'; - private $fields = array( + /** @var array */ + private array $fields = array( 'object_id' => 'integer', 'name' => 'varchar(100)', 'description' => 'varchar(500)', @@ -74,39 +79,40 @@ class LendObject 'category_id' => 'int', 'nb_available' => 'int', ); - private $object_id; - private $name = ''; - private $description = ''; - private $serial_number = ''; - private $price = 0.0; - private $rent_price = 0.0; - private $price_per_day = false; - private $dimension = ''; - private $weight = 0.0; - private $is_active = true; - private $category_id; - private $nb_available = 1; - // Requête sur le dernier statut de l'objet - private $date_begin; - private $date_forecast; - private $date_end; - private $status_text; - private $status_id; - private $in_stock = true; - // Requête sur l'adhérent associé au statut - private $nom_adh = ''; - private $prenom_adh = ''; - private $email_adh = ''; - private $id_adh; - private $rent_id; - private $comments; - private $currency = '€'; - private $picture; - private $cat_active = true; - private $cat_name; - private $member; - - private $deps = [ + private ?int $object_id; + private string $name = ''; + private string $description = ''; + private string $serial_number = ''; + private float $price = 0.0; + private float $rent_price = 0.0; + private bool $price_per_day = false; + private string $dimension = ''; + private float $weight = 0.0; + private bool $is_active = true; + private ?int $category_id; + private int $nb_available = 1; + + private string $date_begin; + private ?string $date_forecast; + private ?string $date_end; + private string $status_text = ''; + private int $status_id; + private bool $in_stock = true; + + private string $nom_adh = ''; + private string $prenom_adh = ''; + private string $email_adh = ''; + private ?int $id_adh; + private ?int $rent_id; + private string $comments = ''; + private string $currency = '€'; + private ObjectPicture $picture; + private bool $cat_active = true; + private ?string $cat_name = null; + private Adherent $member; + + /** @var array */ + private array $deps = [ 'picture' => true, 'rents' => false, 'last_rent' => false, @@ -115,38 +121,33 @@ class LendObject 'category' => false ]; - private $zdb; - private $plugins; + private Db $zdb; + private Plugins $plugins; /** * @var LendRent[] * Rents list for the object */ - private $rents; + private array $rents; /** * Default constructor * - * @param Db $zdb Database instance - * @param Plugins $plugins Plugins instance - * @param int|object $args Maybe null, an RS object or an id from database - * @param array $deps Dependencies configuration, see LendOb::$deps + * @param Db $zdb Database instance + * @param Plugins $plugins Plugins instance + * @param int|ArrayObject|null $args Maybe null, an RS object or an id from database + * @param array $deps Dependencies configuration, see LendOb::$deps */ - public function __construct(Db $zdb, Plugins $plugins, $args = null, $deps = null) + public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject $args = null, array $deps = null) { $this->zdb = $zdb; $this->plugins = $plugins; - if ($deps !== null && is_array($deps)) { + if ($deps !== null) { $this->deps = array_merge( $this->deps, $deps ); - } elseif ($deps !== null) { - Analog::log( - '$deps should be an array, ' . gettype($deps) . ' given!', - Analog::WARNING - ); } if ($this->deps['picture'] === true) { @@ -218,11 +219,11 @@ public function __construct(Db $zdb, Plugins $plugins, $args = null, $deps = nul /** * Populate object from a resultset row * - * @param ArrayObject $r the resultset row + * @param ArrayObject $r the resultset row * * @return void */ - private function loadFromRS($r) + private function loadFromRS(ArrayObject $r): void { $this->object_id = $r->object_id; $this->name = $r->name; @@ -305,11 +306,11 @@ private function loadFromRS($r) } /** - * Enregistre l'élément en cours que ce soit en insert ou update + * Store object * - * @return bool False si l'enregistrement a échoué, true si aucune erreur + * @return bool */ - public function store() + public function store(): bool { try { $values = array(); @@ -322,7 +323,7 @@ public function store() //Handle booleans for postgres ; bugs #18899 and #19354 $values[$k] = $this->zdb->isPostgres() ? 'false' : 0; } else { - $values[$k] = $this->$k; + $values[$k] = $this->$k ?? null; } } @@ -333,6 +334,7 @@ public function store() $result = $this->zdb->execute($insert); if ($result->count() > 0) { if ($this->zdb->isPostgres()) { + /** @phpstan-ignore-next-line */ $this->object_id = $this->zdb->driver->getLastGeneratedValue( PREFIX_DB . 'lend_objects_id_seq' ); @@ -364,13 +366,13 @@ public function store() } /** - * Get object rent status and rent user informations. + * Get object rent status and rent user information. * - * @param LendObject $object L'objet dont on cherche le statut. Est automatiquement modifié. + * @param LendObject $object Object instance to be modified * * @return void */ - public static function getStatusForObject($object) + public static function getStatusForObject(LendObject $object): void { global $zdb; @@ -411,13 +413,13 @@ public static function getStatusForObject($object) } /** - * Renvoit tous les objects correspondant aux IDs donnés. + * Get requested objects * - * @param array $ids Tableau des IDs pour lequels on souhaite avoir les objects + * @param array $ids Objects to retrieve IDs * - * @return LendObject[] Tableau des objets correspondant aux IDs + * @return LendObject[] */ - public static function getMoreObjectsByIds($ids) + public static function getMoreObjectsByIds(array $ids): array { global $zdb, $plugins; @@ -461,7 +463,7 @@ public static function getMoreObjectsByIds($ids) * * @return mixed the called property */ - public function __get($name) + public function __get(string $name) { switch ($name) { case 'date_begin': @@ -477,7 +479,7 @@ public function __get($name) case 'weight': return number_format($this->weight, 3, ',', ' '); default: - return $this->$name; + return $this->$name ?? null; } } @@ -485,11 +487,11 @@ public function __get($name) * Global setter method * * @param string $name name of the property we want to assign a value to - * @param object $value a relevant value for the property + * @param mixed $value a relevant value for the property * * @return void */ - public function __set($name, $value) + public function __set(string $name, $value): void { $forbidden = ['currency']; if (!in_array($name, $forbidden)) { @@ -511,7 +513,7 @@ public function __set($name, $value) * * @return string */ - public function getCurrency() + public function getCurrency(): string { return $this->currency; } @@ -519,13 +521,14 @@ public function getCurrency() /** * Get current rent * - * @return LendRent|void + * @return LendRent|null */ - public function getCurrentRent() + public function getCurrentRent(): ?LendRent { if (is_array($this->rents) && count($this->rents) > 0) { return $this->rents[0]; } + return null; } /** @@ -535,7 +538,7 @@ public function getCurrentRent() * * @return boolean */ - public function isActive() + public function isActive(): bool { return $this->is_active && $this->cat_active; } @@ -548,7 +551,7 @@ public function isActive() * * @return string */ - private function getHighlighted(ObjectsList $filters, $field) + private function getHighlighted(ObjectsList $filters, string $field): string { //check if search concerns field $process = false; @@ -596,7 +599,7 @@ private function getHighlighted(ObjectsList $filters, $field) * * @return string */ - public function displayName(ObjectsList $filters) + public function displayName(ObjectsList $filters): string { return $this->getHighlighted($filters, 'name'); } @@ -608,7 +611,7 @@ public function displayName(ObjectsList $filters) * * @return string */ - public function displayDescription(ObjectsList $filters) + public function displayDescription(ObjectsList $filters): string { return $this->getHighlighted($filters, 'description'); } @@ -620,7 +623,7 @@ public function displayDescription(ObjectsList $filters) * * @return string */ - public function displaySerial(ObjectsList $filters) + public function displaySerial(ObjectsList $filters): string { return $this->getHighlighted($filters, 'serial_number'); } @@ -632,7 +635,7 @@ public function displaySerial(ObjectsList $filters) * * @return string */ - public function displayDimension(ObjectsList $filters) + public function displayDimension(ObjectsList $filters): string { return $this->getHighlighted($filters, 'dimension'); } @@ -642,7 +645,7 @@ public function displayDimension(ObjectsList $filters) * * @return boolean */ - public function delete() + public function delete(): bool { try { $this->zdb->connection->beginTransaction(); @@ -670,10 +673,10 @@ public function delete() * * @return boolean */ - public function clone() + public function clone(): bool { //unset id so this is considered as new object - $this->object_id = null; + unset($this->object_id); //unset image $this->picture = new ObjectPicture($this->plugins); return $this->store(); @@ -682,11 +685,11 @@ public function clone() /** * Get ID * - * @return int + * @return ?int */ - public function getId(): int + public function getId(): ?int { - return (int)$this->object_id; + return $this->object_id ?? null; } /** @@ -752,9 +755,9 @@ public function getWeight(): float /** * Get textual status * - * @return mixed + * @return string */ - public function getStatusText() + public function getStatusText(): string { return $this->status_text; } @@ -794,19 +797,19 @@ public function getDateForecast(): string /** * Get member ID * - * @return mixed + * @return ?int */ - public function getIdAdh() + public function getIdAdh(): ?int { - return $this->id_adh; + return $this->id_adh ?? null; } /** * Get rent ID * - * @return mixed + * @return ?int */ - public function getRentId() + public function getRentId(): ?int { return $this->rent_id; } @@ -814,11 +817,11 @@ public function getRentId() /** * Get category ID * - * @return mixed + * @return ?int */ - public function getCategoryId() + public function getCategoryId(): ?int { - return $this->category_id; + return $this->category_id ?? null; } /** @@ -826,7 +829,7 @@ public function getCategoryId() * * @return string */ - public function getSerialNumber() + public function getSerialNumber(): string { return $this->serial_number; } @@ -839,9 +842,9 @@ public function getSerialNumber() * @return string * @throws \Exception */ - protected function getDateField($name): string + protected function getDateField(string $name): string { - $date = $this->$name; + $date = $this->$name ?? null; if ($date == '' || $date == null) { return ''; } @@ -856,7 +859,7 @@ protected function getDateField($name): string * * @return bool */ - public function __isset($name) + public function __isset(string $name): bool { return property_exists($this, $name); } diff --git a/lib/GaletteObjectsLend/Entity/LendPDF.php b/lib/GaletteObjectsLend/Entity/LendPDF.php index 9846ea6..a51e405 100644 --- a/lib/GaletteObjectsLend/Entity/LendPDF.php +++ b/lib/GaletteObjectsLend/Entity/LendPDF.php @@ -36,9 +36,9 @@ class LendPDF extends \Galette\IO\Pdf * Main constructor, set creator and author * * @param CorePreferences $prefs Preferences - * @param PdfModel $model Related model + * @param ?PdfModel $model Related model */ - public function __construct(CorePreferences $prefs, $model = null) + public function __construct(CorePreferences $prefs, ?PdfModel $model = null) { parent::__construct($prefs, $model); $this->SetDisplayMode('real', 'OneColumn'); diff --git a/lib/GaletteObjectsLend/Entity/LendRent.php b/lib/GaletteObjectsLend/Entity/LendRent.php index 67a0f17..8323c8e 100644 --- a/lib/GaletteObjectsLend/Entity/LendRent.php +++ b/lib/GaletteObjectsLend/Entity/LendRent.php @@ -52,7 +52,8 @@ class LendRent public const TABLE = 'rents'; public const PK = 'rent_id'; - private $fields = array( + /** @var array */ + private array $fields = array( 'rent_id' => 'integer', 'object_id' => 'integer', 'date_begin' => 'datetime', @@ -62,29 +63,29 @@ class LendRent 'adherent_id' => 'integer', 'comments' => 'varchar(200)' ); - private $rent_id; - private $object_id; - private $date_begin; - private $date_forecast; - private $date_end; - private $status_id; - private $adherent_id; - private $comments = ''; - private $in_stock; - // Join sur table Status - private $status_text; - // Left join sur table adhérents - private $nom_adh = ''; - private $prenom_adh = ''; - private $pseudo_adh = ''; - private $email_adh = ''; + private int $rent_id; + private int $object_id; + private string $date_begin; + private ?string $date_forecast; + private ?string $date_end; + private int $status_id; + private ?int $adherent_id; + private string $comments = ''; + private bool $in_stock; + + private string $status_text; + + private ?string $nom_adh = ''; + private ?string $prenom_adh = ''; + private ?string $pseudo_adh = ''; + private ?string $email_adh = ''; /** * Default constructor * - * @param mixed|null $args Either an int with rent id, null, or a resultset row + * @param int|ArrayObject|null $args Either an int with rent id, null, or a resultset row */ - public function __construct($args = null) + public function __construct(int|ArrayObject $args = null) { global $zdb; @@ -114,11 +115,11 @@ public function __construct($args = null) /** * Populate object from a resultset row * - * @param ArrayObject $r the resultset row + * @param ArrayObject $r the resultset row * * @return void */ - private function loadFromRS($r) + private function loadFromRS(ArrayObject $r): void { $this->rent_id = $r->rent_id; $this->object_id = $r->object_id; @@ -135,7 +136,7 @@ private function loadFromRS($r) * * @return bool */ - public function store() + public function store(): bool { global $zdb; @@ -144,7 +145,7 @@ public function store() $values = array(); foreach ($this->fields as $k => $v) { - $values[$k] = $this->$k; + $values[$k] = $this->$k ?? null; } if (!isset($this->rent_id) || $this->rent_id == '') { @@ -195,16 +196,15 @@ public function store() } /** - * Retourne tous les historiques d'emprunts pour un objet donné trié par date de début - * les plus récents en 1er. + * Get rent histroy for a given object sorted * - * @param integer $object_id ID de l'objet dont on souhaite l'historique d'emprunt + * @param integer $object_id Object ID * @param boolean $only_last Only retrieve last rent (for list display) * @param string $order Order clause, defaults to 'date_begin DESC' * - * @return array + * @return LendRent[] */ - public static function getRentsForObjectId($object_id, $only_last = false, $order = 'date_begin desc') + public static function getRentsForObjectId(int $object_id, bool $only_last = false, string $order = 'date_begin desc'): array { global $zdb; @@ -255,14 +255,14 @@ public static function getRentsForObjectId($object_id, $only_last = false, $orde } /** - * Ferme tous les emprunts ouverts pour un objet donné avec le commentaire indiqué + * Close all open rents for a given object with given comment * - * @param int $object_id ID de l'objet surlequel fermer les emprunts - * @param string $comments Commentaire à mettre sur les emprunts + * @param int $object_id Object ID + * @param string $comments Comment to add on lend that will be closed * - * @return boolean True si OK, False si une erreur SQL est survenue + * @return boolean */ - public static function closeAllRentsForObject($object_id, $comments) + public static function closeAllRentsForObject(int $object_id, string $comments): bool { global $zdb; @@ -297,9 +297,9 @@ public static function closeAllRentsForObject($object_id, $comments) /** * Get active members sorted by name * - * @return array + * @return Adherent[] */ - public static function getAllActivesAdherents() + public static function getAllActivesAdherents(): array { try { $filters = new \Galette\Filters\MembersList(); @@ -332,7 +332,7 @@ public static function getAllActivesAdherents() * * @return mixed the called property */ - public function __get($name) + public function __get(string $name) { switch ($name) { case 'date_begin': @@ -357,11 +357,11 @@ public function __get($name) * Global setter method * * @param string $name name of the property we want to assign a value to - * @param object $value a relevant value for the property + * @param mixed $value a relevant value for the property * * @return void */ - public function __set($name, $value) + public function __set(string $name, $value): void { switch ($name) { case 'adherent_id': @@ -408,11 +408,11 @@ public function __set($name, $value) /** * Generic isset function * - * @param $name Property name + * @param string $name Property name * * @return bool */ - public function __isset($name) + public function __isset(string $name): bool { return property_exists($this, $name); } diff --git a/lib/GaletteObjectsLend/Entity/LendStatus.php b/lib/GaletteObjectsLend/Entity/LendStatus.php index e94c3fc..1c4dd00 100644 --- a/lib/GaletteObjectsLend/Entity/LendStatus.php +++ b/lib/GaletteObjectsLend/Entity/LendStatus.php @@ -42,28 +42,29 @@ class LendStatus public const TABLE = 'status'; public const PK = 'status_id'; - private $zdb; + private Db $zdb; - private $fields = array( + /** @var array */ + private array $fields = array( 'status_id' => 'integer', 'status_text' => 'varchar(100)', 'in_stock' => 'boolean', 'is_active' => 'boolean', 'rent_day_number' => 'int' ); - private $status_id; - private $status_text = ''; - private $in_stock = false; - private $is_active = true; - private $rent_day_number = null; + private int $status_id; + private string $status_text = ''; + private bool $in_stock = false; + private bool $is_active = true; + private ?int $rent_day_number = null; /** * Status constructor * - * @param Db $zdb Database instance - * @param mixed $args Can be null, an ID or a database row + * @param Db $zdb Database instance + * @param int|ArrayObject|null $args Can be null, an ID or a database row */ - public function __construct(Db $zdb, $args = null) + public function __construct(Db $zdb, int|ArrayObject $args = null) { $this->zdb = $zdb; @@ -90,16 +91,16 @@ public function __construct(Db $zdb, $args = null) /** * Populate object from a resultset row * - * @param ArrayObject $r the resultset row + * @param ArrayObject $r the resultset row * * @return void */ - private function loadFromRS($r) + private function loadFromRS(ArrayObject $r): void { $this->status_id = $r->status_id; $this->status_text = $r->status_text; - $this->in_stock = $r->in_stock == '1' ? true : false; - $this->is_active = $r->is_active == '1' ? true : false; + $this->in_stock = $r->in_stock == '1'; + $this->is_active = $r->is_active == '1'; $this->rent_day_number = $r->rent_day_number; } @@ -108,7 +109,7 @@ private function loadFromRS($r) * * @return bool */ - public function store() + public function store(): bool { try { $values = array(); @@ -132,6 +133,7 @@ public function store() $result = $this->zdb->execute($insert); if ($result->count() > 0) { if ($this->zdb->isPostgres()) { + /** @phpstan-ignore-next-line */ $this->status_id = $this->zdb->driver->getLastGeneratedValue( PREFIX_DB . 'lend_status_id_seq' ); @@ -163,9 +165,9 @@ public function store() * * @param Db $zdb Database instance * - * @return array + * @return LendStatus[] */ - public static function getActiveStatuses(Db $zdb) + public static function getActiveStatuses(Db $zdb): array { try { $select = $zdb->select(LEND_PREFIX . self::TABLE) @@ -193,9 +195,9 @@ public static function getActiveStatuses(Db $zdb) * * @param Db $zdb Database instance * - * @return array + * @return LendStatus[] */ - public static function getActiveTakeAwayStatuses(Db $zdb) + public static function getActiveTakeAwayStatuses(Db $zdb): array { try { $select = $zdb->select(LEND_PREFIX . self::TABLE) @@ -225,7 +227,7 @@ public static function getActiveTakeAwayStatuses(Db $zdb) * * @return LendStatus[] */ - public static function getActiveStockStatuses(Db $zdb) + public static function getActiveStockStatuses(Db $zdb): array { try { $select = $zdb->select(LEND_PREFIX . self::TABLE) @@ -248,7 +250,7 @@ public static function getActiveStockStatuses(Db $zdb) * * @return boolean */ - public function delete() + public function delete(): bool { try { $delete = $this->zdb->delete(LEND_PREFIX . self::TABLE) @@ -268,13 +270,13 @@ public function delete() /** * Global getter method * - * @param string $name name of the property we want to retrive + * @param string $name name of the property we want to retrieve * - * @return false|object the called property + * @return mixed the called property */ - public function __get($name) + public function __get(string $name) { - return $this->$name; + return $this->$name ?? null; } /** @@ -285,7 +287,7 @@ public function __get($name) * * @return void */ - public function __set($name, $value) + public function __set(string $name, $value): void { $this->$name = $value; } @@ -293,11 +295,11 @@ public function __set($name, $value) /** * Generic isset function * - * @param $name Property name + * @param string $name Property name * * @return bool */ - public function __isset($name) + public function __isset(string $name): bool { return property_exists($this, $name); } diff --git a/lib/GaletteObjectsLend/Entity/Picture.php b/lib/GaletteObjectsLend/Entity/Picture.php index e4b0f08..1a3a656 100644 --- a/lib/GaletteObjectsLend/Entity/Picture.php +++ b/lib/GaletteObjectsLend/Entity/Picture.php @@ -105,7 +105,7 @@ protected function getDefaultPicture(): void * * @return Response */ - public function displayThumb(Response $response, Preferences $prefs) + public function displayThumb(Response $response, Preferences $prefs): Response { $this->setThumbSizes($prefs); $response = $response->withHeader('Content-Type', $this->mime) @@ -130,9 +130,9 @@ public function displayThumb(Response $response, Preferences $prefs) * @param string $dest the destination image. * If null, we'll use the source image. Defaults to null * - * @return void|false + * @return bool */ - private function createThumb($source, $ext, $dest = null) + private function createThumb($source, $ext, $dest = null): bool { $class = get_class($this); @@ -215,12 +215,15 @@ private function createThumb($source, $ext, $dest = null) imagegif($thumb, $dest); break; } + + return true; } else { Analog::log( '[' . $class . '] GD is not present - ' . 'pictures could not be resized!', Analog::ERROR ); + return false; } } @@ -255,7 +258,7 @@ public function delete(bool $transaction = true): bool * * @return bool|int */ - public function store($file, $ajax = false, $cropping = null): bool|int + public function store($file, bool $ajax = false, array $cropping = null): bool|int { $ext = strlen(pathinfo($this->file_path, PATHINFO_EXTENSION)) + 1; $filename = substr($this->file_path, 0, strlen($this->file_path) - strlen($ext)); @@ -271,12 +274,12 @@ public function store($file, $ajax = false, $cropping = null): bool|int /** * Restore objects images from database blob * - * @param array $success Success messages - * @param array $error Error messages + * @param string[] $success Success messages + * @param string[] $error Error messages * * @return void */ - public function restorePictures(&$success, &$error) + public function restorePictures(array &$success, array &$error): void { global $zdb; @@ -321,7 +324,7 @@ public function restorePictures(&$success, &$error) * * @return string */ - public function getThumbPath() + public function getThumbPath(): string { if ($this->has_picture) { $ext = pathinfo($this->file_path, PATHINFO_EXTENSION); @@ -344,7 +347,7 @@ public function getThumbPath() * * @return void */ - private function setThumbSizes(Preferences $prefs) + private function setThumbSizes(Preferences $prefs): void { $thumb = $this->getThumbPath(); $this->thumb_max_width = $prefs->getThumbWidth(); diff --git a/lib/GaletteObjectsLend/Entity/Preferences.php b/lib/GaletteObjectsLend/Entity/Preferences.php index 6688ec0..b4d75f0 100644 --- a/lib/GaletteObjectsLend/Entity/Preferences.php +++ b/lib/GaletteObjectsLend/Entity/Preferences.php @@ -54,7 +54,8 @@ class Preferences public const PK = 'code'; private Db $zdb; - private $prefs; + /** @var array */ + private array $prefs; /** * Paramètre : voir la liste des catégories en en-têtes de la liste des objets @@ -160,7 +161,7 @@ class Preferences * * @return void */ - public function __construct(Db $zdb, $load = true) + public function __construct(Db $zdb, bool $load = true) { $this->zdb = $zdb; if ($load) { @@ -171,9 +172,9 @@ public function __construct(Db $zdb, $load = true) /** * Get preferences * - * @return array + * @return array */ - public function getpreferences() + public function getPreferences(): array { if (count($this->prefs) == 0) { $this->load(); @@ -184,11 +185,11 @@ public function getpreferences() /** * Global getter method * - * @param string $name name of the property we want to retrive + * @param string $name name of the property we want to retrieve * * @return false|object the called property */ - public function __get($name) + public function __get(string $name) { $forbidden = array(); @@ -206,12 +207,12 @@ public function __get($name) /** * Store preferences * - * @param array $data Posted data - * @param array $errors Errors + * @param array $data Posted data + * @param string[] $errors Errors * * @return boolean */ - public function store($data, &$errors) + public function store(array $data, array &$errors): bool { foreach ($data as $key => $value) { $this->prefs[$key] = $value; @@ -270,7 +271,7 @@ public function store($data, &$errors) * * @return boolean */ - public function load() + public function load(): bool { $this->prefs = array(); @@ -300,7 +301,7 @@ public function load() * * @return integer */ - public function getThumbWidth() + public function getThumbWidth(): int { return $this->prefs['THUMB_MAX_WIDTH']; } @@ -310,7 +311,7 @@ public function getThumbWidth() * * @return integer */ - public function getThumbHeight() + public function getThumbHeight(): int { return $this->prefs['THUMB_MAX_HEIGHT']; } @@ -320,7 +321,7 @@ public function getThumbHeight() * * @return boolean */ - public function imagesInLists() + public function imagesInLists(): bool { return $this->prefs['VIEW_THUMBNAIL']; } @@ -332,7 +333,7 @@ public function imagesInLists() * * @return boolean */ - public function showFullsize() + public function showFullsize(): bool { return $this->prefs['VIEW_FULLSIZE']; } diff --git a/lib/GaletteObjectsLend/Filters/CategoriesList.php b/lib/GaletteObjectsLend/Filters/CategoriesList.php index fc28906..c192a4b 100644 --- a/lib/GaletteObjectsLend/Filters/CategoriesList.php +++ b/lib/GaletteObjectsLend/Filters/CategoriesList.php @@ -41,12 +41,12 @@ class CategoriesList extends Pagination { //filters - private $filter_str; - private $active_filter; - private $not_empty; - private $objects_filters; + private ?string $filter_str; + private ?int $active_filter; + private ?bool $not_empty; + private ?ObjectsList $objects_filters; - protected $query; + protected string $query; /** @var array */ protected array $categorylist_fields = array( diff --git a/lib/GaletteObjectsLend/Filters/ObjectsList.php b/lib/GaletteObjectsLend/Filters/ObjectsList.php index 2b86625..35c6bd3 100644 --- a/lib/GaletteObjectsLend/Filters/ObjectsList.php +++ b/lib/GaletteObjectsLend/Filters/ObjectsList.php @@ -23,6 +23,7 @@ use Analog\Analog; use Galette\Core\Pagination; +use GaletteObjectsLend\Entity\Preferences; use GaletteObjectsLend\Repository\Objects; use Laminas\Db\Sql\Select; use Slim\Views\Twig; @@ -43,13 +44,14 @@ class ObjectsList extends Pagination { //filters - private $filter_str; - private $category_filter; - private $active_filter; - private $field_filter; - private $selected; + private ?string $filter_str; + private ?int $category_filter; + private ?int $active_filter; + private ?int $field_filter; + /** @var array */ + private array $selected; - protected $query; + protected string $query; /** @var array */ protected array $objectslist_fields = array( @@ -233,7 +235,7 @@ public function __set(string $name, $value): void * * @return void */ - public function setLimit(Select $select) + public function setLimit(Select $select): void { $this->setLimits($select); } @@ -246,7 +248,7 @@ public function setLimit(Select $select) * * @return void */ - public function setViewCommonsFilters($prefs, $view): void + public function setViewCommonsFilters(Preferences $prefs, Twig $view): void { $prefs = $prefs->getPreferences(); diff --git a/lib/GaletteObjectsLend/Filters/StatusList.php b/lib/GaletteObjectsLend/Filters/StatusList.php index 96424e8..8c699e5 100644 --- a/lib/GaletteObjectsLend/Filters/StatusList.php +++ b/lib/GaletteObjectsLend/Filters/StatusList.php @@ -39,11 +39,11 @@ class StatusList extends Pagination { //filters - private $filter_str; - private $active_filter; - private $stock_filter; + private ?string $filter_str; + private ?int $active_filter; + private ?int $stock_filter; - protected $query; + protected string $query; /** @var array */ protected array $statuslist_fields = array( diff --git a/lib/GaletteObjectsLend/IO/PdfObject.php b/lib/GaletteObjectsLend/IO/PdfObject.php index debf6e6..6a4f430 100644 --- a/lib/GaletteObjectsLend/IO/PdfObject.php +++ b/lib/GaletteObjectsLend/IO/PdfObject.php @@ -39,8 +39,8 @@ */ class PdfObject extends Pdf { - private $zdb; - private $lprefs; + private Db $zdb; + private LPreferences $lprefs; /** * Main constructor @@ -64,7 +64,7 @@ public function __construct(Db $zdb, Preferences $prefs, LPreferences $lprefs) * * @return void */ - private function init() + private function init(): void { // Set document information $this->SetTitle(_T('Object card', 'objectslend')); @@ -83,11 +83,11 @@ private function init() /** * Draw listed object cards * - * @param array $objects Object list + * @param LendObject[] $objects Object list * * @return void */ - public function drawCards(array $objects) + public function drawCards(array $objects): void { $this->Open(); foreach ($objects as $object) { @@ -103,7 +103,7 @@ public function drawCards(array $objects) * * @return void */ - public function drawCard(LendObject $object) + public function drawCard(LendObject $object): void { $this->SetFont(Pdf::FONT, 'B'); $wpic = 0; @@ -219,7 +219,7 @@ public function drawCard(LendObject $object) * * @return void */ - private function addCell(string $title, string $value, int $width) + private function addCell(string $title, string $value, int $width): void { if ($width > 0) { $this->Cell($width, 0, ''); diff --git a/lib/GaletteObjectsLend/IO/PdfObjects.php b/lib/GaletteObjectsLend/IO/PdfObjects.php index 76e2171..0ee5f3b 100644 --- a/lib/GaletteObjectsLend/IO/PdfObjects.php +++ b/lib/GaletteObjectsLend/IO/PdfObjects.php @@ -27,6 +27,7 @@ use Galette\Core\Preferences; use Galette\Core\Login; use Analog\Analog; +use GaletteObjectsLend\Entity\LendObject; use GaletteObjectsLend\Filters\ObjectsList; use GaletteObjectsLend\Entity\LendCategory; use GaletteObjectsLend\Entity\Preferences as LendPreferences; @@ -41,8 +42,8 @@ class PdfObjects extends Pdf public const LIST_FONT = self::FONT_SIZE - 2; private Db $zdb; - private $lendsprefs; - private $filters; + private LendPreferences $lendsprefs; + private ObjectsList $filters; private Login $login; private Plugins $plugins; @@ -80,7 +81,7 @@ public function __construct( * * @return void */ - private function init() + private function init(): void { // Set document information $this->SetTitle(_T("Objects list", "objectslend")); @@ -98,7 +99,7 @@ private function init() * * @phpcs:disable */ - public function Header() + public function Header(): void { // @phpcs:enable $this->SetFont(Pdf::FONT, 'B'); @@ -129,11 +130,11 @@ public function Header() /** * Draw objects list * - * @param array $objects List of objects + * @param LendObject[] $objects List of objects * * @return void */ - public function drawList($objects) + public function drawList(array $objects): void { $this->Open(); $this->AddPage(); diff --git a/lib/GaletteObjectsLend/Repository/Categories.php b/lib/GaletteObjectsLend/Repository/Categories.php index cf43cc2..cf4a141 100644 --- a/lib/GaletteObjectsLend/Repository/Categories.php +++ b/lib/GaletteObjectsLend/Repository/Categories.php @@ -25,6 +25,7 @@ use Galette\Entity\DynamicFields; use Analog\Analog; use Galette\Core\Db; +use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\Sql\Expression; use Laminas\Db\Sql\Predicate\Operator; use GaletteObjectsLend\Filters\CategoriesList; @@ -53,9 +54,10 @@ class Categories public const ORDERBY_NAME = 0; - private $filters = false; - private $count = null; - private $errors = array(); + private CategoriesList $filters; + private ?int $count = null; + /** @var array */ + private array $errors = array(); private Db $zdb; private Plugins $plugins; @@ -64,10 +66,10 @@ class Categories /** * Default constructor * - * @param Db $zdb Database instance - * @param Login $login Logged in instance - * @param Plugins $plugins Plugins instance - * @param CategoriesList $filters Filtering + * @param Db $zdb Database instance + * @param Login $login Logged in instance + * @param Plugins $plugins Plugins instance + * @param ?CategoriesList $filters Filtering */ public function __construct(Db $zdb, Login $login, Plugins $plugins, CategoriesList $filters = null) { @@ -86,22 +88,22 @@ public function __construct(Db $zdb, Login $login, Plugins $plugins, CategoriesL /** * Get categories list * - * @param boolean $as_cat return the results as an array of - * Categories object. - * @param array $fields field(s) name(s) to get. Should be a string or - * an array. If null, all fields will be - * returned - * @param boolean $count true if we want to count members - * @param boolean $limit true if we want records pagination + * @param boolean $as_cat return the results as an array of + * Categories object. + * @param array $fields field(s) name(s) to get. Should be a string or + * an array. If null, all fields will be + * returned + * @param boolean $count true if we want to count members + * @param boolean $limit true if we want records pagination * - * @return array|ArrayObject + * @return LendCategory[]|ResultSet */ public function getCategoriesList( - $as_cat = false, - $fields = null, - $count = true, - $limit = true - ) { + bool $as_cat = false, + array $fields = null, + bool $count = true, + bool $limit = true + ): array|ResultSet { try { $select = $this->buildSelect($fields, $count); @@ -136,13 +138,11 @@ public function getCategoriesList( * * @param boolean $as_cat return the results as an array of * Category object. - * @param array $fields field(s) name(s) to get. Should be a string or - * an array. If null, all fields will be - * returned + * @param ?array $fields field(s) name(s) to get. If null, all fields will be returned * - * @return array|ArrayObject + * @return LendCategory[]|ResultSet */ - public function getList($as_cat = false, $fields = null) + public function getList(bool $as_cat = false, array $fields = null): array|ResultSet { return $this->getCategoriesList( $as_cat, @@ -155,12 +155,12 @@ public function getList($as_cat = false, $fields = null) /** * Builds the SELECT statement * - * @param array $fields fields list to retrieve - * @param bool $count true if we want to count members, defaults to false + * @param ?array $fields fields list to retrieve + * @param bool $count true if we want to count members, defaults to false * * @return Select SELECT statement */ - private function buildSelect($fields, $count = false) + private function buildSelect(?array $fields, bool $count = false): Select { try { $fieldsList = [ @@ -213,7 +213,7 @@ private function buildSelect($fields, $count = false) * * @return void */ - private function proceedCount($select) + private function proceedCount(Select $select): void { try { $countSelect = clone $select; @@ -262,12 +262,12 @@ private function proceedCount($select) /** * Builds the order clause * - * @param array $fields Fields list to ensure ORDER clause - * references selected fields. Optional. + * @param ?string[] $fields Fields list to ensure ORDER clause + * references selected fields. Optional. * - * @return array SQL ORDER clauses + * @return array SQL ORDER clauses */ - private function buildOrderClause($fields = null) + private function buildOrderClause(array $fields = null): array { $order = array(); switch ($this->filters->orderby) { @@ -288,7 +288,7 @@ private function buildOrderClause($fields = null) * * @return void */ - private function buildWhereClause($select) + private function buildWhereClause(Select $select): void { try { //if there are filters on objects; add them @@ -341,12 +341,12 @@ private function buildWhereClause($select) * Is field allowed to order? it should be present in * provided fields list (those that are SELECT'ed). * - * @param string $field_name Field name to order by - * @param array $fields SELECTE'ed fields + * @param string $field_name Field name to order by + * @param array $fields SELECTE'ed fields * * @return bool */ - private function canOrderBy($field_name, $fields) + private function canOrderBy(string $field_name, array $fields): bool { if (!is_array($fields)) { return true; @@ -367,7 +367,7 @@ private function canOrderBy($field_name, $fields) * * @return int */ - public function getCount() + public function getCount(): int { return $this->count; } @@ -375,9 +375,9 @@ public function getCount() /** * Get registered errors * - * @return array + * @return array */ - public function getErrors() + public function getErrors(): array { return $this->errors; } diff --git a/lib/GaletteObjectsLend/Repository/Objects.php b/lib/GaletteObjectsLend/Repository/Objects.php index e8c9406..1fd102b 100644 --- a/lib/GaletteObjectsLend/Repository/Objects.php +++ b/lib/GaletteObjectsLend/Repository/Objects.php @@ -68,19 +68,20 @@ class Objects private Db $zdb; - private $filters = false; - private $count = null; - private $errors = array(); + private ObjectsList $filters; + private ?int $count = null; + /** @var array */ + private array $errors = array(); private Preferences $prefs; private Plugins $plugins; /** * Default constructor * - * @param Db $zdb Database instance - * @param Plugins $plugins Plugins instance - * @param Preferences $lprefs Lends preferences instance - * @param ObjectsList $filters Filtering + * @param Db $zdb Database instance + * @param Plugins $plugins Plugins instance + * @param Preferences $lprefs Lends preferences instance + * @param ?ObjectsList $filters Filtering */ public function __construct(Db $zdb, Plugins $plugins, Preferences $lprefs, ObjectsList $filters = null) { @@ -98,24 +99,22 @@ public function __construct(Db $zdb, Plugins $plugins, Preferences $lprefs, Obje /** * Get objects list * - * @param boolean $as_objects return the results as an array of - * Object object. - * @param array $fields field(s) name(s) to get. Should be a string or - * an array. If null, all fields will be - * returned - * @param boolean $count true if we want to count members - * @param boolean $limit true if we want records pagination - * @param boolean $all_rents true to load rents along with objects + * @param boolean $as_objects return the results as an array of + * Object object. + * @param array $fields field(s) name(s) to get. If null, all fields will be returned + * @param boolean $count true if we want to count members + * @param boolean $limit true if we want records pagination + * @param boolean $all_rents true to load rents along with objects * - * @return array|ArrayObject + * @return LendObject[]|ResultSet */ public function getObjectsList( - $as_objects = false, - $fields = null, - $count = true, - $limit = true, - $all_rents = false - ) { + bool $as_objects = false, + array $fields = null, + bool $count = true, + bool $limit = true, + bool $all_rents = false + ): array|ResultSet { try { $select = $this->buildSelect($fields, $count); @@ -152,11 +151,11 @@ public function getObjectsList( /** * Remove specified objects, and their full history * - * @param array $ids Objects identifiers to delete + * @param array $ids Objects identifiers to delete * * @return bool */ - public function removeObjects(array $ids) + public function removeObjects(array $ids): bool { try { $this->zdb->connection->beginTransaction(); @@ -210,11 +209,11 @@ public function removeObjects(array $ids) /** * Disable selected objects * - * @param array $ids List of objects id to disable + * @param array $ids List of objects id to disable * * @return ResultSet */ - public function disableObjects(array $ids) + public function disableObjects(array $ids): ResultSet { $update = $this->zdb->update(LEND_PREFIX . self::TABLE); $update->set(['is_active' => false]); @@ -229,15 +228,13 @@ public function disableObjects(array $ids) /** * Get Objects list * - * @param boolean $as_objects return the results as an array of - * Object object. - * @param array $fields field(s) name(s) to get. Should be a string or - * an array. If null, all fields will be - * returned + * @param boolean $as_objects return the results as an array of + * Object object. + * @param ?array $fields field(s) name(s) to get. If null, all fields will be returned * - * @return array|ArrayObject + * @return LendObject[]|ResultSet */ - public function getList($as_objects = false, $fields = null) + public function getList(bool $as_objects = false, array $fields = null): array|ResultSet { return $this->getObjectsList( $as_objects, @@ -251,12 +248,12 @@ public function getList($as_objects = false, $fields = null) /** * Builds the SELECT statement * - * @param array $fields fields list to retrieve - * @param bool $count true if we want to count members, defaults to false + * @param ?string[] $fields fields list to retrieve + * @param bool $count true if we want to count members, defaults to false * * @return Select SELECT statement */ - private function buildSelect($fields, $count = false) + private function buildSelect(?array $fields, bool $count = false): Select { global $zdb, $login; @@ -324,7 +321,7 @@ private function buildSelect($fields, $count = false) * * @return void */ - private function proceedCount($select) + private function proceedCount(Select $select): void { global $zdb; @@ -375,12 +372,12 @@ private function proceedCount($select) /** * Builds the order clause * - * @param array $fields Fields list to ensure ORDER clause - * references selected fields. Optionnal. + * @param array $fields Fields list to ensure ORDER clause + * references selected fields. Optional. * - * @return array SQL ORDER clauses + * @return array SQL ORDER clauses */ - private function buildOrderClause($fields = null) + private function buildOrderClause(array $fields = null): array { $order = array(); switch ($this->filters->orderby) { @@ -442,10 +439,8 @@ private function buildOrderClause($fields = null) * * @return void */ - public function buildWhereClause($select) + public function buildWhereClause(Select $select): void { - global $login; - try { if (is_array($this->filters->selected) && count($this->filters->selected) > 0) { $select->where->in('o.' . self::PK, $this->filters->selected); @@ -524,12 +519,12 @@ public function buildWhereClause($select) * Is field allowed to order? it should be present in * provided fields list (those that are SELECT'ed). * - * @param string $field_name Field name to order by - * @param array $fields SELECTE'ed fields + * @param string $field_name Field name to order by + * @param ?string[] $fields SELECTE'ed fields * * @return boolean */ - private function canOrderBy($field_name, $fields) + private function canOrderBy(string $field_name, ?array $fields): bool { if (!is_array($fields)) { return true; @@ -550,7 +545,7 @@ private function canOrderBy($field_name, $fields) * * @return int */ - public function getCount() + public function getCount(): int { return $this->count; } @@ -558,9 +553,9 @@ public function getCount() /** * Get registered errors * - * @return array + * @return array */ - public function getErrors() + public function getErrors(): array { return $this->errors; } diff --git a/lib/GaletteObjectsLend/Repository/Status.php b/lib/GaletteObjectsLend/Repository/Status.php index 63035cd..22153e7 100644 --- a/lib/GaletteObjectsLend/Repository/Status.php +++ b/lib/GaletteObjectsLend/Repository/Status.php @@ -25,6 +25,7 @@ use Galette\Entity\DynamicFields; use Analog\Analog; use Galette\Core\Db; +use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\Sql\Expression; use GaletteObjectsLend\Filters\StatusList; use GaletteObjectsLend\Entity\LendStatus; @@ -60,16 +61,17 @@ class Status private Db $zdb; private Login $login; - private $filters = false; - private $count = null; - private $errors = array(); + private StatusList $filters; + private ?int $count = null; + /** @var array */ + private array $errors = array(); /** * Default constructor * - * @param Db $zdb Database instance - * @param Login $login Logged in instance - * @param StatusList $filters Filtering + * @param Db $zdb Database instance + * @param Login $login Logged in instance + * @param ?StatusList $filters Filtering */ public function __construct(Db $zdb, Login $login, StatusList $filters = null) { @@ -87,22 +89,20 @@ public function __construct(Db $zdb, Login $login, StatusList $filters = null) /** * Get status list * - * @param boolean $as_stt return the results as an array of - * Status object. - * @param array $fields field(s) name(s) to get. Should be a string or - * an array. If null, all fields will be - * returned - * @param boolean $count true if we want to count members - * @param boolean $limit true if we want records pagination + * @param boolean $as_stt return the results as an array of + * Status object. + * @param ?string[] $fields field(s) name(s) to get. If null, all fields will be returned + * @param boolean $count true if we want to count members + * @param boolean $limit true if we want records pagination * - * @return array|ArrayObject + * @return LendStatus[]|ResultSet */ public function getStatusList( - $as_stt = false, - $fields = null, - $count = true, - $limit = true - ) { + bool $as_stt = false, + array $fields = null, + bool $count = true, + bool $limit = true + ): array|ResultSet { try { $select = $this->buildSelect($fields, false, $count); @@ -141,9 +141,9 @@ public function getStatusList( * an array. If null, all fields will be * returned * - * @return LendStatus[]|ArrayObject + * @return LendStatus[]|ResultSet */ - public function getList($as_stt = false, $fields = null) + public function getList($as_stt = false, $fields = null): array|ResultSet { return $this->getStatusList( $as_stt, @@ -156,14 +156,14 @@ public function getList($as_stt = false, $fields = null) /** * Builds the SELECT statement * - * @param array $fields fields list to retrieve - * @param bool $photos true if we want to get only members with photos - * Default to false, only relevant for SHOW_PUBLIC_LIST - * @param bool $count true if we want to count members, defaults to false + * @param string[] $fields fields list to retrieve + * @param bool $photos true if we want to get only members with photos + * Default to false, only relevant for SHOW_PUBLIC_LIST + * @param bool $count true if we want to count members, defaults to false * * @return Select SELECT statement */ - private function buildSelect($fields, $photos, $count = false) + private function buildSelect(?array $fields, bool $photos = false, bool $count = false): Select { try { $fieldsList = ($fields != null) @@ -199,7 +199,7 @@ private function buildSelect($fields, $photos, $count = false) * * @return void */ - private function proceedCount($select) + private function proceedCount(Select $select): void { try { $countSelect = clone $select; @@ -237,12 +237,12 @@ private function proceedCount($select) /** * Builds the order clause * - * @param array $fields Fields list to ensure ORDER clause - * references selected fields. Optional. + * @param ?string[] $fields Fields list to ensure ORDER clause + * references selected fields. Optional. * - * @return array SQL ORDER clauses + * @return array SQL ORDER clauses */ - private function buildOrderClause($fields = null) + private function buildOrderClause(array $fields = null): array { $order = array(); switch ($this->filters->orderby) { @@ -283,7 +283,7 @@ private function buildOrderClause($fields = null) * * @return void */ - private function buildWhereClause($select) + private function buildWhereClause(Select $select): void { try { if ($this->filters->active_filter == self::ACTIVE) { @@ -321,12 +321,12 @@ private function buildWhereClause($select) * Is field allowed to order? it should be present in * provided fields list (those that are SELECT'ed). * - * @param string $field_name Field name to order by - * @param array $fields SELECTE'ed fields + * @param string $field_name Field name to order by + * @param ?string[] $fields SELECTE'ed fields * * @return boolean */ - private function canOrderBy($field_name, $fields) + private function canOrderBy(string $field_name, ?array $fields): bool { if (!is_array($fields)) { return true; @@ -347,7 +347,7 @@ private function canOrderBy($field_name, $fields) * * @return int */ - public function getCount() + public function getCount(): int { return $this->count; } @@ -355,9 +355,9 @@ public function getCount() /** * Get registered errors * - * @return array + * @return array */ - public function getErrors() + public function getErrors(): array { return $this->errors; }