Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Typehint #19

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteObjectsLend/Controllers/ImagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteObjectsLend/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 38 additions & 40 deletions lib/GaletteObjectsLend/Entity/LendCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,47 @@ class LendCategory
public const TABLE = 'category';
public const PK = 'category_id';

private $fields = array(
/** @var array<string> */
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<string, bool> */
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<string,int|string>|null $args Maybe null, an RS object or an id from database
* @param array<string,bool> $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) {
Expand Down Expand Up @@ -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<string, int|string> $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;
Expand All @@ -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();
Expand All @@ -166,14 +163,15 @@ 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'
);
} else {
$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)
Expand All @@ -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();
Expand Down Expand Up @@ -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");

Expand All @@ -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;
}
}

Expand All @@ -272,19 +270,19 @@ public function __get($name)
*
* @return void
*/
public function __set($name, $value)
public function __set(string $name, $value): void
{
$this->$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;
}

/**
Expand All @@ -294,7 +292,7 @@ public function getId(): int
*/
public function isActive(): bool
{
return (bool)$this->is_active;
return $this->is_active;
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
Loading
Loading