Skip to content

Commit

Permalink
Strict types declaration, several fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 17, 2024
1 parent 0ef2f8e commit a9c0075
Show file tree
Hide file tree
Showing 27 changed files with 101 additions and 52 deletions.
1 change: 1 addition & 0 deletions .composer-require-checker.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"GALETTE_CARD_ROWS",
"GALETTE_CARD_WIDTH",
"GALETTE_COMPAT_VERSION",
"GALETTE_SYSCONFIG_PATH",
"GALETTE_CONFIG_PATH",
"GALETTE_DATA_PATH",
"GALETTE_DB_VERSION",
Expand Down
2 changes: 2 additions & 0 deletions _config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

define('AUTO_PREFIX', 'auto_');
2 changes: 2 additions & 0 deletions _define.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

$this->register(
'Galette Auto', //Name
'Plugin to manage Automobile clubs', //Short description
Expand Down
2 changes: 2 additions & 0 deletions _routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

use GaletteAuto\Controllers\Controller;
use GaletteAuto\Controllers\Crud\PropertiesController;
use GaletteAuto\Controllers\Crud\ModelsController;
Expand Down
12 changes: 7 additions & 5 deletions lib/GaletteAuto/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Analog\Analog;
Expand All @@ -45,7 +47,7 @@ abstract class AbstractObject

protected Db $zdb;
protected ?int $id;
protected string $value;
protected ?string $value;
protected ?PropertiesList $filters = null;

private int $count;
Expand Down Expand Up @@ -168,7 +170,7 @@ public function store(bool $new = false): bool
} catch (\Exception $e) {
Analog::log(
'[' . get_class($this) . '] Cannot store ' . $this->name .
' values `' . $this->id . '`, `' . $this->value . '` | ' .
' values `' . ($this->id ?? '') . '`, `' . $this->value . '` | ' .
$e->getMessage(),
Analog::WARNING
);
Expand Down Expand Up @@ -236,14 +238,14 @@ abstract public function getRouteName(): string;
*/
public function __get(string $name): mixed
{
if (isset($this->$name)) {
return $this->$name;
if (property_exists($this, $name)) {
return $this->$name ?? null;
} else {
Analog::log(
'[' . get_class($this) . '] Unable to retrieve `' . $name . '`',
Analog::INFO
);
return false;
throw new \RuntimeException('Unable to retrieve `' . $name . '`');
}
}

Expand Down
35 changes: 21 additions & 14 deletions lib/GaletteAuto/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use ArrayObject;
Expand Down Expand Up @@ -53,7 +55,8 @@
* @property Transmission $transmission
* @property Finition $finition
* @property Model $model
* @property Adherent|int $owner
* @property int $owner_id
* @property Adherent $owner
* @property Picture $picture
* @property History $history
*/
Expand Down Expand Up @@ -125,6 +128,7 @@ class Auto
private Body $body;
private History $history;
private State $state;
private int $owner_id;
private Adherent $owner;

public const FUEL_PETROL = 1;
Expand Down Expand Up @@ -271,7 +275,8 @@ private function loadFromRS(ArrayObject $r): void
$bpk = Body::PK;
$this->body->load((int)$r->$bpk);
$opk = Adherent::PK;
$this->owner->load((int)$r->$opk);
$this->owner_id = (int)$r->$opk;
$this->owner->load($this->owner_id);
$spk = State::PK;
$this->state->load((int)$r->$spk);
$this->history->load((int)$this->id);
Expand Down Expand Up @@ -345,7 +350,7 @@ public function store(bool $new = false): bool
switch ($v) {
case 'string':
case 'date':
$values[$k] = $this->$propName;
$values[$k] = $this->$propName ?? null;
break;
case 'integer':
$values[$k] = (
Expand Down Expand Up @@ -501,7 +506,8 @@ public function hasPicture(): bool
*/
public function appropriateCar(Login $login): void
{
$this->owner->load($login->id);
$this->owner_id = $login->id;
$this->owner->load($this->owner_id);
}

/**
Expand Down Expand Up @@ -545,7 +551,7 @@ public function __get(string $name): mixed
case 'first_registration_date':
case 'first_circulation_date':
case 'creation_date':
if ($this->$name != '') {
if (isset($this->$name)) {
try {
$d = new \DateTime($this->$name);
return $d->format(_T("Y-m-d"));
Expand All @@ -559,12 +565,11 @@ public function __get(string $name): mixed
return $this->$name;
}
}
break;
return null;
case 'picture':
return $this->picture;
default:
return $this->$name;
break;
return $this->$name ?? '';
}
}

Expand Down Expand Up @@ -604,8 +609,9 @@ public function __set(string $name, mixed $value): void
case 'body':
$this->body->load((int)$value);
break;
case 'owner':
$this->owner->load((int)$value);
case 'owner_id':
$this->owner_id = (int)$value;
$this->owner->load($this->owner_id);
break;
case 'state':
$this->state->load((int)$value);
Expand Down Expand Up @@ -739,7 +745,7 @@ public function check(array $post): bool
//constants
case 'fuel':
if (in_array($value, array_keys($this->listFuels()))) {
$this->fuel = $value;
$this->fuel = (int)$value;
} else {
$this->errors[] = _T("- You must choose a fuel in the list", "auto");
}
Expand All @@ -752,7 +758,7 @@ public function check(array $post): bool
case 'body':
case 'state':
if ($value > 0) {
$this->$prop->load($value);
$this->$prop->load((int)$value);
} else {
$class = 'GaletteAuto\\' . ucwords($prop);
$name = $class::FIELD;
Expand All @@ -763,11 +769,12 @@ public function check(array $post): bool
);
}
break;
case 'owner':
case 'owner_id':
if (isset($post['change_owner']) || !isset($this->id)) {
$value = (int)$value;
if ($value > 0) {
$this->$prop->load($value);
$this->owner_id = $value;
$this->owner->load($value);
} else {
$this->errors[] = _T("- you must attach an owner to this car", "auto");
}
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Autos.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Analog\Analog;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Galette\Core\Db;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Galette\Core\Db;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Galette\Core\Db;
Expand Down
18 changes: 11 additions & 7 deletions lib/GaletteAuto/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Controllers;

use ArrayObject;
Expand Down Expand Up @@ -299,7 +301,7 @@ public function showAddEditVehicle(Request $request, Response $response, string
isset($get['id_adh'])
&& ($this->login->isAdmin() || $this->login->isStaff())
) {
$auto->owner = (int)$get['id_adh'];
$auto->owner_id = (int)$get['id_adh'];
} else {
$auto->appropriateCar($this->login);
}
Expand Down Expand Up @@ -328,7 +330,7 @@ public function showAddEditVehicle(Request $request, Response $response, string
'require_calendar' => true,
'require_dialog' => true,
'car' => $auto,
'models' => $models->getList($auto->model->brand->id),
'models' => $models->getList($auto->model->brand->id ?? null),
'js_init_models' => !isset($auto->model->brand),
'brands' => $auto->model->brand->getList(),
'colors' => $auto->color->getList(),
Expand Down Expand Up @@ -361,6 +363,7 @@ public function showAddEditVehicle(Request $request, Response $response, string
if (count($members)) {
$params['members']['list'] = $members;
}
$params['autocomplete'] = true;

// display page
$this->view->render(
Expand Down Expand Up @@ -412,7 +415,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a
{
$post = $request->getParsedBody();

$is_new = ($action === 'add');
$is_new = ($action === 'add' || $action === 'new');

// initialize warnings
$error_detected = array();
Expand All @@ -424,7 +427,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a

$auto = new Auto($this->plugins, $this->zdb);
if (!$is_new) {
$auto->load($post[Auto::PK]);
$auto->load((int)$post[Auto::PK]);
}

$res = $auto->check($post);
Expand Down Expand Up @@ -488,14 +491,15 @@ public function doAddEditVehicle(Request $request, Response $response, string $a
*/
public function vehicleHistory(Request $request, Response $response, int $id): Response
{
$apk = Auto::PK;
$history = new History($this->zdb, $id);
$auto = new Auto($this->plugins, $this->zdb, $history->{Auto::PK});
$auto = new Auto($this->plugins, $this->zdb);
$auto->load($history->$apk);
$this->checkAclsFor($response, $auto->owner->id);

$apk = Auto::PK;
$params = [
'entries' => $history->getEntries(),
'page_title' => str_replace('%d', $history->$apk, _T("History of car #%d", "auto")),
'page_title' => str_replace('%d', (string)$history->$apk, _T("History of car #%d", "auto")),
'mode' => $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : ''
];

Expand Down
6 changes: 4 additions & 2 deletions lib/GaletteAuto/Controllers/Crud/ModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Controllers\Crud;

use DI\Attribute\Inject;
Expand Down Expand Up @@ -185,9 +187,9 @@ public function edit(Request $request, Response $response, int $id = null, strin
{
$model = new Model($this->zdb);

if ($this->session->automodel !== null) {
if ($this->session->auto_model !== null) {
$model->check($this->session->auto_model);
$this->session->automodel = null;
unset($this->session->auto_model);
}

$model_id = null;
Expand Down
4 changes: 3 additions & 1 deletion lib/GaletteAuto/Controllers/Crud/PropertiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Controllers\Crud;

use DI\Attribute\Inject;
Expand Down Expand Up @@ -406,7 +408,7 @@ public function doPropertyEdit(

if (!$is_new) {
if (isset($post[$object->pk])) {
$object->load($post[$object->pk]);
$object->load((int)$post[$object->pk]);
} else {
$error_detected[]
= _T("- No id provided for modifying this record! (internal)", "auto");
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Filters/AutosList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Filters;

use Galette\Core\Pagination;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Filters/ModelsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Filters;

use Galette\Core\Pagination;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Filters/PropertiesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto\Filters;

use Galette\Core\Pagination;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/Finition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Galette\Core\Db;
Expand Down
2 changes: 2 additions & 0 deletions lib/GaletteAuto/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace GaletteAuto;

use Analog\Analog;
Expand Down
Loading

0 comments on commit a9c0075

Please sign in to comment.