Skip to content

Commit

Permalink
MDL-67114 core: php74 fix. Fix use of scalar as array in core
Browse files Browse the repository at this point in the history
There are various places where it's not guaranteed that the
variable being used is array, and instead, can be null, bool, int...

We need to check that because php74 warns about it.

Where possible we have used the coalesce operator as
replacement for isset() ternary operations.
  • Loading branch information
stronk7 committed Jan 3, 2020
1 parent a6e544e commit 4621917
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions competency/tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,7 @@ public function test_export_data_for_user_about_their_learning_plans() {
$this->assertEquals('-', $comp['rating']['rating']);
$comp = $data->competencies[2];
$this->assertEquals($comp4->get('shortname'), $comp['name']);
$this->assertNull($comp['rating']['rating']);
$this->assertNull($comp['rating']);
$data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})",
get_string('commentsubcontext', 'core_comment')]));
$this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments);
Expand All @@ -2320,7 +2320,7 @@ public function test_export_data_for_user_about_their_learning_plans() {
$this->assertEquals('C', $comp['rating']['rating']);
$comp = $data->competencies[2];
$this->assertEquals($comp4->get('shortname'), $comp['name']);
$this->assertNull($comp['rating']['rating']);
$this->assertNull($comp['rating']);

// This plan is complete.
$data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
Expand Down
6 changes: 4 additions & 2 deletions enrol/meta/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ public function add_instance($course, array $fields = null) {
require_once("$CFG->dirroot/enrol/meta/locallib.php");

// Support creating multiple at once.
if (is_array($fields['customint1'])) {
if (isset($fields['customint1']) && is_array($fields['customint1'])) {
$courses = array_unique($fields['customint1']);
} else {
} else if (isset($fields['customint1'])) {
$courses = array($fields['customint1']);
} else {
$courses = array(null); // Strange? Yes, but that's how it's working or instance is not created ever.
}
foreach ($courses as $courseid) {
if (!empty($fields['customint2']) && $fields['customint2'] == ENROL_META_CREATE_GROUP) {
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/tests/fixtures/editor_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class editor_form extends moodleform {
*/
protected function definition() {
$mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions'];
$editoroptions = $this->_customdata['editoroptions'] ?? null;

// Add header.
$mform->addElement('header', 'myheader', 'Editor in Moodle form');
Expand Down
5 changes: 5 additions & 0 deletions lib/filestorage/file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,16 @@ public function is_image_from_storedfile(stored_file $file) {
protected function get_imageinfo_from_path($path) {
$imageinfo = getimagesize($path);

if (!is_array($imageinfo)) {
return false; // Nothing to process, the file was not recognised as image by GD.
}

$image = array(
'width' => $imageinfo[0],
'height' => $imageinfo[1],
'mimetype' => image_type_to_mime_type($imageinfo[2]),
);

if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
// GD can not parse it, sorry.
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/form/duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function onQuickFormEvent($event, $arg, &$caller) {
break;

case 'createElement':
if ($arg[2]['optional']) {
if (!empty($arg[2]['optional'])) {
$caller->disabledIf($arg[0], $arg[0] . '[enabled]');
}
$caller->setType($arg[0] . '[number]', PARAM_FLOAT);
Expand Down
2 changes: 2 additions & 0 deletions lib/form/filetypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ public function onQuickFormEvent($event, $arg, &$caller) {
*/
public function validateSubmitValue($value) {

$value = $value ?? ['filetypes' => null]; // A null $value can arrive here. Coalesce, creating the default array.

if (!$this->allowall) {
// Assert that there is an actual list provided.
$normalized = $this->util->normalize_file_types($value['filetypes']);
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/session_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ public function test_get_locked_page_at($url, $time) {
$SESSION->recentsessionlocks = $this->sessionlock_history();

$page = \core\session\manager::get_locked_page_at($time);
$this->assertEquals($url, $page['url']);
$this->assertEquals($url, is_array($page) ? $page['url'] : null);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod/glossary/import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class mod_glossary_import_form extends moodleform {
function definition() {
global $CFG;
$mform =& $this->_form;
$cmid = $this->_customdata['id'];
$cmid = $this->_customdata['id'] ?? null;

$mform->addElement('filepicker', 'file', get_string('filetoimport', 'glossary'));
$mform->addHelpButton('file', 'filetoimport', 'glossary');
Expand Down
4 changes: 2 additions & 2 deletions mod/wiki/comments_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class mod_wiki_comments_form extends moodleform {
protected function definition() {
$mform = $this->_form;

$current = $this->_customdata['current'];
$commentoptions = $this->_customdata['commentoptions'];
$current = $this->_customdata['current'] ?? null;
$commentoptions = $this->_customdata['commentoptions'] ?? null;

// visible elements
$mform->addElement('editor', 'entrycomment_editor', get_string('comment', 'glossary'), null, $commentoptions);
Expand Down
2 changes: 1 addition & 1 deletion mod/wiki/parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function get_section(&$string, $type, $section, $all_content = fal
return $content;
}
else {
return $content[1];
return is_array($content) ? $content[1] : null;
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion question/engine/questionusage.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public function prepare_simulated_post_data($simulatedresponses) {
// Behaviour vars should not be processed by question type, just add prefix.
$behaviourvars = $this->get_question_attempt($slot)->get_behaviour()->get_expected_data();
foreach (array_keys($responsedata) as $responsedatakey) {
if ($responsedatakey[0] === '-') {
if (is_string($responsedatakey) && $responsedatakey[0] === '-') {
$behaviourvarname = substr($responsedatakey, 1);
if (isset($behaviourvars[$behaviourvarname])) {
// Expected behaviour var found.
Expand Down
2 changes: 1 addition & 1 deletion repository/dropbox/classes/dropbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function fetch_dropbox_content($endpoint, $data = []) {
* @throws moodle_exception
*/
protected function check_and_handle_api_errors($data) {
if ($this->info['http_code'] == 200) {
if (!is_array($this->info) or $this->info['http_code'] == 200) {
// Dropbox only returns errors on non-200 response codes.
return;
}
Expand Down

0 comments on commit 4621917

Please sign in to comment.