mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-66968 php74: array_key_exists() for objects is deprecated
Replace it for correct property_exists() when the element being inspected is a property of object/class. Amended and squased changes: - keep mongo unmodified. The information is array, hence correct. - fix a couple of messaging phpdocs that were incorrect. Amended take#2: - As far as mongo resturns BSONDocument that is ArrayObject, aka implements ArrayAccess, we have decided to explicitly cast results to array so existing array_key_exists() and other accesses will continue working the same.
This commit is contained in:
parent
aaff6692a1
commit
f4feabb83f
@ -77,7 +77,7 @@ if (($form = data_submitted()) && confirm_sesskey()) {
|
||||
// Record the site preference.
|
||||
$preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
|
||||
}
|
||||
} else if (array_key_exists($componentprovidersetting, $form)) {
|
||||
} else if (property_exists($form, $componentprovidersetting)) {
|
||||
// We must be processing loggedin or loggedoff checkboxes. Store
|
||||
// defained comma-separated processors as setting value.
|
||||
// Using array_filter eliminates elements set to 0 above.
|
||||
|
@ -123,13 +123,13 @@ if (($action == 'edit') || ($action == 'new')) {
|
||||
}
|
||||
$instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
|
||||
if (!empty($instanceoptionnames)) {
|
||||
if (array_key_exists('enablecourseinstances', $fromform)) {
|
||||
if (property_exists($fromform, 'enablecourseinstances')) {
|
||||
$settings['enablecourseinstances'] = $fromform->enablecourseinstances;
|
||||
}
|
||||
else {
|
||||
$settings['enablecourseinstances'] = 0;
|
||||
}
|
||||
if (array_key_exists('enableuserinstances', $fromform)) {
|
||||
if (property_exists($fromform, 'enableuserinstances')) {
|
||||
$settings['enableuserinstances'] = $fromform->enableuserinstances;
|
||||
}
|
||||
else {
|
||||
|
8
cache/stores/mongodb/lib.php
vendored
8
cache/stores/mongodb/lib.php
vendored
@ -254,7 +254,13 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
|
||||
}
|
||||
|
||||
$result = $this->collection->findOne($key);
|
||||
if ($result === null || !array_key_exists('data', $result)) {
|
||||
// Note $result is really an object, BSONDocument extending ArrayObject,
|
||||
// which implements ArrayAccess. That enables access to its information
|
||||
// using square brackets and some array operations. But, it seems that
|
||||
// it's not enough for array_key_exists() to operate on it. Hence, we
|
||||
// are explicitly casting to array, after having checked that the operation
|
||||
// doesn't incur into any performance penalty.
|
||||
if ($result === null || !array_key_exists('data', (array)$result)) {
|
||||
return false;
|
||||
}
|
||||
$data = @unserialize($result['data']);
|
||||
|
@ -3839,7 +3839,7 @@ class api {
|
||||
if (!$userevidence->can_manage()) {
|
||||
throw new required_capability_exception($context, 'moodle/competency:userevidencemanage', 'nopermissions', '');
|
||||
|
||||
} else if (array_key_exists('userid', $data) && $data->userid != $userevidence->get('userid')) {
|
||||
} else if (property_exists($data, 'userid') && $data->userid != $userevidence->get('userid')) {
|
||||
throw new coding_exception('Can not change the userid of a user evidence.');
|
||||
}
|
||||
|
||||
|
@ -460,7 +460,7 @@ class enrol_ldap_plugin extends enrol_plugin {
|
||||
// this is an odd array -- mix of hash and array --
|
||||
$ldapmembers = array();
|
||||
|
||||
if (array_key_exists('memberattribute_role'.$role->id, $this->config)
|
||||
if (property_exists($this->config, 'memberattribute_role'.$role->id)
|
||||
&& !empty($this->config->{'memberattribute_role'.$role->id})
|
||||
&& !empty($course[$this->config->{'memberattribute_role'.$role->id}])) { // May have no membership!
|
||||
|
||||
|
@ -2192,7 +2192,7 @@ class grade_category extends grade_object {
|
||||
$children_array = array();
|
||||
foreach ($category->children as $sortorder=>$child) {
|
||||
|
||||
if (array_key_exists('itemtype', $child)) {
|
||||
if (property_exists($child, 'itemtype')) {
|
||||
$grade_item = new grade_item($child, false);
|
||||
|
||||
if (in_array($grade_item->itemtype, array('course', 'category'))) {
|
||||
|
@ -228,7 +228,7 @@ class user_picture implements renderable {
|
||||
// only touch the DB if we are missing data and complain loudly...
|
||||
$needrec = false;
|
||||
foreach (self::$fields as $field) {
|
||||
if (!array_key_exists($field, $user)) {
|
||||
if (!property_exists($user, $field)) {
|
||||
$needrec = true;
|
||||
debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
|
||||
.'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
|
||||
|
@ -1996,7 +1996,7 @@ class core_renderer extends renderer_base {
|
||||
$button = new single_button($url, $label, $method);
|
||||
|
||||
foreach ((array)$options as $key=>$value) {
|
||||
if (array_key_exists($key, $button)) {
|
||||
if (property_exists($button, $key)) {
|
||||
$button->$key = $value;
|
||||
} else {
|
||||
$button->set_attribute($key, $value);
|
||||
@ -2479,7 +2479,7 @@ class core_renderer extends renderer_base {
|
||||
public function user_picture(stdClass $user, array $options = null) {
|
||||
$userpicture = new user_picture($user);
|
||||
foreach ((array)$options as $key=>$value) {
|
||||
if (array_key_exists($key, $userpicture)) {
|
||||
if (property_exists($userpicture, $key)) {
|
||||
$userpicture->$key = $value;
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ if (($form = data_submitted()) && confirm_sesskey()) {
|
||||
// record the site preference
|
||||
$preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
|
||||
}
|
||||
} else if (array_key_exists($componentprovidersetting, $form)) {
|
||||
} else if (property_exists($form, $componentprovidersetting)) {
|
||||
// we must be processing loggedin or loggedoff checkboxes. Store
|
||||
// defained comma-separated processors as setting value.
|
||||
// Using array_filter eliminates elements set to 0 above
|
||||
|
@ -42,7 +42,7 @@ class core_message_renderer extends plugin_renderer_base {
|
||||
* @param array $allprocessors array of objects containing all message processors
|
||||
* @param array $processors array of objects containing active message processors
|
||||
* @param array $providers array of objects containing message providers
|
||||
* @param array $preferences array of objects containing current preferences
|
||||
* @param stdClass $preferences object containing current preferences
|
||||
* @return string The text to render
|
||||
*/
|
||||
public function manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences) {
|
||||
@ -120,7 +120,7 @@ class core_message_renderer extends plugin_renderer_base {
|
||||
*
|
||||
* @param array $processors array of objects containing message processors
|
||||
* @param array $providers array of objects containing message providers
|
||||
* @param array $preferences array of objects containing current preferences
|
||||
* @param stdClass $preferences object containing current preferences
|
||||
* @return string The text to render
|
||||
*/
|
||||
public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
|
||||
@ -177,7 +177,7 @@ class core_message_renderer extends plugin_renderer_base {
|
||||
$preference = $processor->name.'_provider_'.$preferencebase;
|
||||
if ($providerdisabled) {
|
||||
$select = MESSAGE_DISALLOWED;
|
||||
} else if (array_key_exists($preference, $preferences)) {
|
||||
} else if (property_exists($preferences, $preference)) {
|
||||
$select = $preferences->{$preference};
|
||||
}
|
||||
// dropdown menu
|
||||
@ -192,7 +192,7 @@ class core_message_renderer extends plugin_renderer_base {
|
||||
$checked = true;
|
||||
} else if ($select == 'permitted') {
|
||||
$preference = 'message_provider_'.$preferencebase;
|
||||
if (array_key_exists($preference, $preferences)) {
|
||||
if (property_exists($preferences, $preference)) {
|
||||
$checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
|
||||
}
|
||||
}
|
||||
|
@ -669,7 +669,7 @@ function lesson_grade_item_update($lesson, $grades=null) {
|
||||
require_once($CFG->libdir.'/gradelib.php');
|
||||
}
|
||||
|
||||
if (array_key_exists('cmidnumber', $lesson)) { //it may not be always present
|
||||
if (property_exists($lesson, 'cmidnumber')) { //it may not be always present
|
||||
$params = array('itemname'=>$lesson->name, 'idnumber'=>$lesson->cmidnumber);
|
||||
} else {
|
||||
$params = array('itemname'=>$lesson->name);
|
||||
|
@ -741,7 +741,7 @@ function quiz_grade_item_update($quiz, $grades = null) {
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
require_once($CFG->libdir . '/gradelib.php');
|
||||
|
||||
if (array_key_exists('cmidnumber', $quiz)) { // May not be always present.
|
||||
if (property_exists($quiz, 'cmidnumber')) { // May not be always present.
|
||||
$params = array('itemname' => $quiz->name, 'idnumber' => $quiz->cmidnumber);
|
||||
} else {
|
||||
$params = array('itemname' => $quiz->name);
|
||||
|
@ -100,12 +100,12 @@ class user_filter_checkbox extends user_filter_type {
|
||||
// Check if disable if options are set. if yes then don't add this..
|
||||
if (!empty($this->disableelements) && is_array($this->disableelements)) {
|
||||
foreach ($this->disableelements as $disableelement) {
|
||||
if (array_key_exists($disableelement, $formdata)) {
|
||||
if (property_exists($formdata, $disableelement)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
|
||||
if (property_exists($formdata, $field) and $formdata->$field !== '') {
|
||||
return array('value' => (string)$formdata->$field);
|
||||
}
|
||||
return false;
|
||||
|
@ -92,7 +92,7 @@ class user_filter_cohort extends user_filter_type {
|
||||
$field = $this->_name;
|
||||
$operator = $field.'_op';
|
||||
|
||||
if (array_key_exists($operator, $formdata)) {
|
||||
if (property_exists($formdata, $operator)) {
|
||||
if ($formdata->$operator != 5 and $formdata->$field == '') {
|
||||
// No data - no change except for empty filter.
|
||||
return false;
|
||||
|
@ -98,7 +98,7 @@ class user_filter_courserole extends user_filter_type {
|
||||
$role = $field .'_rl';
|
||||
$category = $field .'_ct';
|
||||
|
||||
if (array_key_exists($field, $formdata)) {
|
||||
if (property_exists($formdata, $field)) {
|
||||
if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
|
||||
// Nothing selected.
|
||||
return false;
|
||||
|
@ -82,7 +82,7 @@ class user_filter_globalrole extends user_filter_type {
|
||||
public function check_data($formdata) {
|
||||
$field = $this->_name;
|
||||
|
||||
if (array_key_exists($field, $formdata) and !empty($formdata->$field)) {
|
||||
if (property_exists($formdata, $field) and !empty($formdata->$field)) {
|
||||
return array('value' => (int)$formdata->$field);
|
||||
}
|
||||
return false;
|
||||
|
@ -122,7 +122,7 @@ class user_filter_profilefield extends user_filter_type {
|
||||
$operator = $field.'_op';
|
||||
$profile = $field.'_fld';
|
||||
|
||||
if (array_key_exists($profile, $formdata)) {
|
||||
if (property_exists($formdata, $profile)) {
|
||||
if ($formdata->$operator < 5 and $formdata->$field === '') {
|
||||
return false;
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ class user_filter_select extends user_filter_type {
|
||||
$field = $this->_name;
|
||||
$operator = $field.'_op';
|
||||
|
||||
if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
|
||||
if (property_exists($formdata, $field) and !empty($formdata->$operator)) {
|
||||
return array('operator' => (int)$formdata->$operator,
|
||||
'value' => (string)$formdata->$field);
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ class user_filter_simpleselect extends user_filter_type {
|
||||
public function check_data($formdata) {
|
||||
$field = $this->_name;
|
||||
|
||||
if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
|
||||
if (property_exists($formdata, $field) and $formdata->$field !== '') {
|
||||
return array('value' => (string)$formdata->$field);
|
||||
}
|
||||
|
||||
|
@ -96,7 +96,7 @@ class user_filter_text extends user_filter_type {
|
||||
$field = $this->_name;
|
||||
$operator = $field.'_op';
|
||||
|
||||
if (array_key_exists($operator, $formdata)) {
|
||||
if (property_exists($formdata, $operator)) {
|
||||
if ($formdata->$operator != 5 and $formdata->$field == '') {
|
||||
// No data - no change except for empty filter.
|
||||
return false;
|
||||
|
@ -116,11 +116,11 @@ class core_user_privacy_testcase extends provider_testcase {
|
||||
$courserequestdata = (array) $writer->get_data([get_string('privacy:courserequestpath', 'user')]);
|
||||
$entry = array_shift($courserequestdata);
|
||||
// Make sure that the password is not exported.
|
||||
$this->assertFalse(array_key_exists('password', $entry));
|
||||
$this->assertFalse(property_exists($entry, 'password'));
|
||||
// Check that some of the other fields are present.
|
||||
$this->assertTrue(array_key_exists('fullname', $entry));
|
||||
$this->assertTrue(array_key_exists('shortname', $entry));
|
||||
$this->assertTrue(array_key_exists('summary', $entry));
|
||||
$this->assertTrue(property_exists($entry, 'fullname'));
|
||||
$this->assertTrue(property_exists($entry, 'shortname'));
|
||||
$this->assertTrue(property_exists($entry, 'summary'));
|
||||
|
||||
// User details.
|
||||
$userdata = (array) $writer->get_data([]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user