Skip to content

Commit

Permalink
Types fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 12, 2024
1 parent 7013f71 commit 5d9d090
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 73 deletions.
7 changes: 3 additions & 4 deletions galette/lib/Galette/Core/Login.php
Expand Up @@ -180,8 +180,7 @@ public function logIn(string $user, string $passe): bool
'An error occurred: ' . $e->getMessage(),
Analog::WARNING
);
Analog::log($e->getTrace(), Analog::ERROR);
return false;
throw $e;
}
}

Expand Down Expand Up @@ -226,12 +225,12 @@ private function logUser(ArrayObject $row): void
Analog::log('User `' . $row->login_adh . '` logged in.', Analog::INFO);
$this->id = $row->id_adh;
$this->login = $row->login_adh;
$this->admin = $row->bool_admin_adh;
$this->admin = (bool)$row->bool_admin_adh;
$this->name = $row->nom_adh;
$this->surname = $row->prenom_adh;
$this->lang = $row->pref_lang;
$this->i18n->changeLanguage($this->lang);
$this->active = $row->activite_adh;
$this->active = (bool)$row->activite_adh;
$this->logged = true;
if ($row->priorite_statut < Members::NON_STAFF_MEMBERS) {
$this->staff = true;
Expand Down
3 changes: 3 additions & 0 deletions galette/lib/Galette/Core/Picture.php
Expand Up @@ -802,6 +802,9 @@ private function resizeImage(string $source, string $ext, string $dest = null, a
}
}

$h = (int)$h;
$w = (int)$w;

// Resized image.
$thumb = imagecreatetruecolor($w, $h);

Expand Down
75 changes: 71 additions & 4 deletions galette/lib/Galette/Core/Preferences.php
Expand Up @@ -50,7 +50,7 @@
* @property string $pref_pays Country
* @property integer $pref_postal_address Postal address to use, one of self::POSTAL_ADDRESS*
* @property integer $pref_postal_staff_member Staff member ID from which retrieve postal address
* @property boolean $pref_disable_members_socials
* @property boolean $pref_disable_members_socials Disable social networks for members
* @property string $pref_lang Default instance language
* @property integer $pref_numrows Default number of rows in lists
* @property integer $pref_log History, one of self::LOG_*
Expand Down Expand Up @@ -506,7 +506,11 @@ public function check(array $values, Login $login): bool
// obtain fields
foreach ($this->getFieldsNames() as $fieldname) {
if (isset($values[$fieldname])) {
$value = trim($values[$fieldname]);
if (is_string($values[$fieldname])) {
$value = trim($values[$fieldname]);
} else {
$value = $values[$fieldname];
}
} else {
$value = "";
}
Expand Down Expand Up @@ -590,7 +594,7 @@ public function check(array $values, Login $login): bool

// missing required fields?
foreach ($this->required as $val) {
if (!isset($values[$val]) || isset($values[$val]) && trim($values[$val]) == '') {
if (empty($values[$val])) {
$this->errors[] = str_replace(
'%field',
$val,
Expand Down Expand Up @@ -984,6 +988,61 @@ public function __get(string $name): mixed
{
$forbidden = array('defaults');
$virtuals = array('vpref_email', 'vpref_email_newadh');
$types = [
'int' => [
'pref_card_address',
'pref_card_hsize',
'pref_card_hspace',
'pref_card_marges_h',
'pref_card_marges_v',
'pref_card_vsize',
'pref_card_vspace',
'pref_default_paymenttype',
'pref_etiq_marges_v',
'pref_etiq_marges_h',
'pref_etiq_hspace',
'pref_etiq_vspace',
'pref_etiq_hsize',
'pref_etiq_vsize',
'pref_etiq_cols',
'pref_etiq_rows',
'pref_etiq_corps',
'pref_filter_account',
'pref_log',
'pref_mail_method',
'pref_membership_ext',
'pref_numrows',
'pref_postal_address',
'pref_postal_staff_member',
'pref_password_length',
'pref_password_strength',
'pref_publicpages_visibility',
'pref_redirect_on_create',
'pref_statut'
],
'bool' => [
'pref_bool_create_member',
'pref_bool_groupsmanagers_create_member',
'pref_bool_groupsmanagers_edit_member',
'pref_bool_groupsmanagers_edit_groups',
'pref_bool_groupsmanagers_exports',
'pref_bool_groupsmanagers_mailings',
'pref_bool_mailadh',
'pref_bool_mailowner',
'pref_bool_publicpages',
'pref_bool_selfsubscribe',
'pref_bool_wrap_mails',
'pref_disable_members_socials',
'pref_editor_enabled',
'pref_etiq_border',
'pref_force_picture_ratio',
'pref_mail_smtp_auth',
'pref_mail_smtp_secure',
'pref_mail_allow_unsecure',
'pref_password_blacklist',
'pref_show_id',
]
];

if (!in_array($name, $forbidden) && isset($this->prefs[$name])) {
if (
Expand All @@ -998,7 +1057,7 @@ public function __get(string $name): mixed
$this->prefs[$name] = self::$defaults['pref_adhesion_form'];
}
$value = $this->prefs[$name];
if (TYPE_DB === \Galette\Core\Db::PGSQL) {
if (TYPE_DB === Db::PGSQL) {
if ($value === 'f') {
$value = false;
}
Expand All @@ -1009,6 +1068,14 @@ public function __get(string $name): mixed
$value = $values[0]; //take first as default
}

if (in_array($name, $types['int']) && $value !== '') {
$value = (int)$value;
}

if (in_array($name, $types['bool']) && $value !== '') {
$value = (bool)$value;
}

return $value;
}
} elseif (in_array($name, $virtuals)) {
Expand Down
10 changes: 5 additions & 5 deletions galette/lib/Galette/DynamicFields/DynamicField.php
Expand Up @@ -633,31 +633,31 @@ public function check(array $values): bool
$this->errors[] = _T("- Field name already used.");
}

if ($this->hasWidth() && isset($values['field_width']) && trim($values['field_width']) != '') {
if ($this->hasWidth() && isset($values['field_width'])) {
if (!is_numeric($values['field_width']) || $values['field_width'] <= 0) {
$this->errors[] = _T("- Width must be a positive integer!");
} else {
$this->width = $values['field_width'];
}
}

if ($this->hasHeight() && isset($values['field_height']) && trim($values['field_height']) != '') {
if ($this->hasHeight() && isset($values['field_height'])) {
if (!is_numeric($values['field_height']) || $values['field_height'] <= 0) {
$this->errors[] = _T("- Height must be a positive integer!");
} else {
$this->height = $values['field_height'];
}
}

if ($this->hasSize() && isset($values['field_size']) && trim($values['field_size']) != '') {
if ($this->hasSize() && isset($values['field_size'])) {
if (!is_numeric($values['field_size']) || $values['field_size'] <= 0) {
$this->errors[] = _T("- Size must be a positive integer!");
} else {
$this->size = $values['field_size'];
}
}

if ($this->hasMinSize() && isset($values['field_min_size']) && trim($values['field_min_size']) != '') {
if ($this->hasMinSize() && isset($values['field_min_size'])) {
if (!is_numeric($values['field_min_size']) || $values['field_min_size'] <= 0) {
$this->errors[] = _T("- Min size must be a positive integer!");
} else {
Expand All @@ -676,7 +676,7 @@ public function check(array $values): bool
}
}

if (isset($values['field_repeat']) && trim($values['field_repeat']) != '') {
if (isset($values['field_repeat'])) {
if (!is_numeric($values['field_repeat'])) {
$this->errors[] = _T("- Repeat must be an integer!");
} else {
Expand Down
41 changes: 24 additions & 17 deletions galette/lib/Galette/Entity/Adherent.php
Expand Up @@ -513,15 +513,7 @@ public function loadSocials(): void
private function getDefaultStatus(): int
{
global $preferences;
if ($preferences->pref_statut != '') {
return $preferences->pref_statut;
} else {
Analog::log(
'Unable to get pref_statut; is it defined in preferences?',
Analog::ERROR
);
return Status::DEFAULT_STATUS;
}
return $preferences->pref_statut;
}

/**
Expand Down Expand Up @@ -1174,9 +1166,8 @@ public function check(array $values, array $required, array $disabled): bool|arr

if (isset($values[$key])) {
$value = $values[$key];
if ($value !== true && $value !== false) {
//@phpstan-ignore-next-line
$value = trim($value ?? '');
if (is_string($value)) {
$value = trim($value);
}
} elseif (empty($this->id)) {
switch ($key) {
Expand Down Expand Up @@ -1217,7 +1208,7 @@ public function check(array $values, array $required, array $disabled): bool|arr
// if the field is enabled, check it
if (!in_array($key, $disabled)) {
// fill up the adherent structure
if ($value !== null && $value !== true && $value !== false && !is_object($value)) {
if (is_string($value)) {
$value = stripslashes($value);
}

Expand Down Expand Up @@ -1276,8 +1267,8 @@ public function check(array $values, array $required, array $disabled): bool|arr
} else {
$owned_group = false;
foreach ($values['groups_adh'] as $group) {
list($gid) = explode('|', $group);
if ($login->isGroupManager($gid)) {
list($gid) = explode('|', (string)$group);
if ($login->isGroupManager((int)$gid)) {
$owned_group = true;
}
}
Expand Down Expand Up @@ -1330,6 +1321,16 @@ public function validate(string $field, mixed $value, array $values): void
//parent_id cannot be a string
$value = null;
}

$booleans = [
'bool_admin_adh',
'bool_exempt_adh',
'bool_display_info'
];
if (in_array($field, $booleans) && $value !== null) {
$value = false;
}

$this->$prop = $value;
return;
}
Expand Down Expand Up @@ -1386,6 +1387,12 @@ public function validate(string $field, mixed $value, array $values): void
);
}
break;
//booleans
case 'bool_admin_adh':
case 'bool_exempt_adh':
case 'bool_display_info':
$this->$prop = (bool)$value;
break;
case 'titre_adh':
if ($value !== '') {
if ($value == '-1') {
Expand Down Expand Up @@ -1516,7 +1523,7 @@ public function validate(string $field, mixed $value, array $values): void
if (!$result) {
$this->errors[] = str_replace(
'%id',
$value,
(string)$value,
_T("Status #%id does not exists in database.")
);
break;
Expand All @@ -1526,7 +1533,7 @@ public function validate(string $field, mixed $value, array $values): void
'An error occurred checking status existence: ' . $e->getMessage(),
Analog::ERROR
);
$this->errors[] = _T("An error has occurred while looking if status does exists.");
throw $e;
}
break;
case 'sexe_adh':
Expand Down
17 changes: 10 additions & 7 deletions galette/lib/Galette/Entity/Contribution.php
Expand Up @@ -288,7 +288,7 @@ private function retrieveEndDate(): void
}

$end_date = clone $next_begin_date;
} elseif ($preferences->pref_membership_ext != '') {
} elseif ($preferences->pref_membership_ext != '' && $preferences->pref_membership_ext != 0) {
//case membership extension
if ($this->extension == null) {
$this->extension = $preferences->pref_membership_ext;
Expand Down Expand Up @@ -428,15 +428,17 @@ public function check(array $values, array $required, array $disabled): bool|arr
$prop = $this->fields[$key]['propname'];

if (isset($values[$key])) {
$value = trim($values[$key]);
$value = $values[$key];
if (is_string($value)) {
$value = trim($value);
}
} else {
$value = '';
$value = null;
}

// if the field is enabled, check it
if (!isset($disabled[$key])) {
// fill up the adherent structure
//$this->$prop = stripslashes($value); //not relevant here!
// fill up the contribution structure

// now, check validity
switch ($key) {
Expand All @@ -459,7 +461,8 @@ public function check(array $values, array $required, array $disabled): bool|arr
}
break;
case 'montant_cotis':
$value = strtr($value, ',', '.');
//FIXME: this is a hack to allow comma as decimal separator
$value = strtr((string)$value, ',', '.');
if (!empty($value) || $value === '0') {
$this->amount = (double)$value;
}
Expand Down Expand Up @@ -1233,7 +1236,7 @@ public function __set(string $name, mixed $value): void
break;
case 'amount':
if (is_numeric($value) && $value > 0) {
$this->$name = $value;
$this->$name = (float)$value;
} else {
Analog::log(
'Trying to set an amount with a non numeric value, ' .
Expand Down
2 changes: 1 addition & 1 deletion galette/lib/Galette/Entity/ContributionsTypes.php
Expand Up @@ -138,7 +138,7 @@ private function loadFromRS(ArrayObject $r): void
{
$this->id = $r->{self::PK};
$this->label = $r->libelle_type_cotis;
$this->amount = $r->amount;
$this->amount = (float)$r->amount;
$this->extension = (int)$r->cotis_extension;
}

Expand Down
4 changes: 2 additions & 2 deletions galette/lib/Galette/Entity/ScheduledPayment.php
Expand Up @@ -126,7 +126,7 @@ private function loadFromRs(ArrayObject $rs): void
$this->payment_type = new PaymentType($this->zdb, $rs->id_paymenttype);
$this->creation_date = $rs->creation_date;
$this->scheduled_date = $rs->scheduled_date;
$this->amount = $rs->amount;
$this->amount = (float)$rs->amount;
$this->is_paid = (bool)$rs->paid;
$this->comment = $rs->comment;
}
Expand Down Expand Up @@ -518,7 +518,7 @@ public function getAllocation(int $id_cotis): float

$results = $this->zdb->execute($select);
$result = $results->current();
return $result->allocation ?? 0;
return (float)($result->allocation ?? 0);
}

/**
Expand Down

0 comments on commit 5d9d090

Please sign in to comment.