Skip to content

Replace non-display uses of getField#22517

Open
cconard96 wants to merge 3 commits intoglpi-project:mainfrom
cconard96:task/replace_getField
Open

Replace non-display uses of getField#22517
cconard96 wants to merge 3 commits intoglpi-project:mainfrom
cconard96:task/replace_getField

Conversation

@cconard96
Copy link
Contributor

@cconard96 cconard96 commented Dec 31, 2025

Checklist before requesting a review

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.

Description

CommonDBTM::getField is not meant to be used except for values that will displayed to the user. Maybe due to its name making it look like a simple getter, there are a lot of cases where the result of the function is used for SQL queries and other backend logic.

@trasher
Copy link
Contributor

trasher commented Jan 5, 2026

There are plenty of get* that did not get anything, is* that does not check anything, or set* that does not set nothing...
Some - like getID() must be used, because id is not always the id field.

I'm not really against this change, but maybe it should be documented (and maybe that kind of methods should really be deprecated in the future).

@cconard96
Copy link
Contributor Author

getID() must be used, because id is not always the id field

I did start with making those replacements here but the use of something besides "id" as the identifier is not always so clear and maybe it was required to get the "id" field specifically. When used with SQL queries, surely an "items_id" column should be set to the "id"? If it should just be the identifier, it isn't supported because the identifier may not be an integer like Dashboards which use a string "key".

I don't think these edge cases affected any of the getField('id') replacements but it required more careful thought than the simple replacements done here.

@trasher
Copy link
Contributor

trasher commented Jan 5, 2026

getID() must be used, because id is not always the id field

That was just a global remark (ofr me aswell); since the getID() behaves differently than most of get* methods.

Copy link
Contributor

@trasher trasher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@trasher
Copy link
Contributor

trasher commented Jan 6, 2026

Tests and lint are failing. Also, a rebase is needed to fix conflicts.

Copy link
Contributor

@trasher trasher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests and lint must be fixed

Copy link
Member

@cedric-anne cedric-anne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, getField() occurences in the NotificationTarget*::getDataForObject() methods was intended. They result in having N/A displayed in notifications rather than no value displayed at all.

@cconard96 cconard96 force-pushed the task/replace_getField branch from c63bee4 to 8eaa0f4 Compare January 14, 2026 12:06
@cedric-anne cedric-anne added this to the 12.0.0 milestone Jan 21, 2026
Comment on lines +244 to 276
if ($locations->fields['comment']) {
$data['##ticket.location.comment##'] = $locations->fields['comment'];
}
if ($locations->getField('room')) {
$data['##ticket.location.room##'] = $locations->getField('room');
if ($locations->fields['room']) {
$data['##ticket.location.room##'] = $locations->fields['room'];
}
if ($locations->getField('building')) {
$data['##ticket.location.building##'] = $locations->getField('building');
if ($locations->fields['building']) {
$data['##ticket.location.building##'] = $locations->fields['building'];
}
if ($locations->getField('latitude')) {
$data['##ticket.location.latitude##'] = $locations->getField('latitude');
if ($locations->fields['latitude']) {
$data['##ticket.location.latitude##'] = $locations->fields['latitude'];
}
if ($locations->getField('longitude')) {
$data['##ticket.location.longitude##'] = $locations->getField('longitude');
if ($locations->fields['longitude']) {
$data['##ticket.location.longitude##'] = $locations->fields['longitude'];
}
if ($locations->getField('altitude')) {
$data['##ticket.location.altitude##'] = $locations->getField('altitude');
if ($locations->fields['altitude']) {
$data['##ticket.location.altitude##'] = $locations->fields['altitude'];
}
if ($locations->getField('address')) {
$data['##ticket.location.address##'] = $locations->getField('address');
if ($locations->fields['address']) {
$data['##ticket.location.address##'] = $locations->fields['address'];
}
if ($locations->getField('postcode')) {
$data['##ticket.location.postcode##'] = $locations->getField('postcode');
if ($locations->fields['postcode']) {
$data['##ticket.location.postcode##'] = $locations->fields['postcode'];
}
if ($locations->getField('town')) {
$data['##ticket.location.town##'] = $locations->getField('town');
if ($locations->fields['town']) {
$data['##ticket.location.town##'] = $locations->fields['town'];
}
if ($locations->getField('state')) {
$data['##ticket.location.state##'] = $locations->getField('state');
if ($locations->fields['state']) {
$data['##ticket.location.state##'] = $locations->fields['state'];
}
if ($locations->getField('country')) {
$data['##ticket.location.country##'] = $locations->getField('country');
if ($locations->fields['country']) {
$data['##ticket.location.country##'] = $locations->fields['country'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, for these lines, the solution is to do the same way as in line 350:

if ($locations->getFromDB($item->fields['locations_id'])) {
    $data['##ticket.location.comment##'] = $locations->getField('comment');
    // ...
}

= $row['problems_id'];
$tmp['##problem.date##']
= $problem->getField('date');
= $problem->fields['date'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should probably be converted.

= $problem->fields['date'];
$tmp['##problem.title##']
= $problem->getField('name');
= $problem->fields['name'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
= $problem->fields['name'];
= $problem->getField('name');

);
$tmp['##problem.content##']
= $problem->getField('content');
= $problem->fields['content'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
= $problem->fields['content'];
= $problem->getField('content');

->onlyMethods(['getPasswordExpirationTime'])
->getMock();
$user->method('getPasswordExpirationTime')->willReturn($expiration_time);
$user->getEmpty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants