From 9e384114341bdf9a7db130232dffb19425f6c810 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 27 Oct 2021 13:09:10 +0800 Subject: [PATCH] Changes to make GHA happy and added usages of the coalescing operator (#121) --- classes/element.php | 39 +++++++++++++++++++------------------- classes/element_helper.php | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/classes/element.php b/classes/element.php index f19793e..ea77e4a 100644 --- a/classes/element.php +++ b/classes/element.php @@ -51,7 +51,7 @@ abstract class element { * @var string The right alignment constant. */ const ALIGN_RIGHT = 'R'; - + /** * @var \stdClass $element The data for the element we are adding - do not use, kept for legacy reasons. */ @@ -150,7 +150,7 @@ abstract class element { $this->width = $element->width; $this->refpoint = $element->refpoint; $this->showposxy = isset($showposxy) && $showposxy; - $this->set_alignment(isset($element->alignment) ? $element->alignment : element::ALIGN_LEFT); + $this->set_alignment($element->alignment ?? self::ALIGN_LEFT); } /** @@ -254,24 +254,25 @@ abstract class element { /** * Returns the alignment. - * + * * @return string The current alignment value. */ public function get_alignment() { - return isset($this->alignment) ? $this->alignment : element::ALIGN_LEFT; + return $this->alignment ?? self::ALIGN_LEFT; } /** * Sets the alignment. - * + * * @param string $alignment The new alignment. - * - * @throws InvalidArgumentException if the provided new alignment is not valid. + * + * @throws \InvalidArgumentException if the provided new alignment is not valid. */ - protected function set_alignment($alignment) { - $valid_values = array(element::ALIGN_LEFT, element::ALIGN_CENTER, element::ALIGN_RIGHT); - if (!in_array($alignment, $valid_values)) { - throw new \InvalidArgumentException("'$alignment' is not a valid alignment value. It has to be one of " . implode(', ', $valid_values)); + protected function set_alignment(string $alignment) { + $validvalues = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT); + if (!in_array($alignment, $validvalues)) { + throw new \InvalidArgumentException("'$alignment' is not a valid alignment value. It has to be one of " . + implode(', ', $validvalues)); } $this->alignment = $alignment; } @@ -358,16 +359,16 @@ abstract class element { $element = new \stdClass(); $element->name = $data->name; $element->data = $this->save_unique_data($data); - $element->font = (isset($data->font)) ? $data->font : null; - $element->fontsize = (isset($data->fontsize)) ? $data->fontsize : null; - $element->colour = (isset($data->colour)) ? $data->colour : null; + $element->font = $data->font ?? null; + $element->fontsize = $data->fontsize ?? null; + $element->colour = $data->colour ?? null; if ($this->showposxy) { - $element->posx = (isset($data->posx)) ? $data->posx : null; - $element->posy = (isset($data->posy)) ? $data->posy : null; + $element->posx = $data->posx ?? null; + $element->posy = $data->posy ?? null; } - $element->width = (isset($data->width)) ? $data->width : null; - $element->refpoint = (isset($data->refpoint)) ? $data->refpoint : null; - $element->alignment = (isset($data->alignment)) ? $data->alignment : element::ALIGN_LEFT; + $element->width = $data->width ?? null; + $element->refpoint = $data->refpoint ?? null; + $element->alignment = $data->alignment ?? self::ALIGN_LEFT; $element->timemodified = time(); // Check if we are updating, or inserting a new element. diff --git a/classes/element_helper.php b/classes/element_helper.php index 39438c2..7053dc5 100644 --- a/classes/element_helper.php +++ b/classes/element_helper.php @@ -216,7 +216,7 @@ class element_helper { $alignmentoptions[element::ALIGN_LEFT] = get_string('alignleft', 'customcert'); $alignmentoptions[element::ALIGN_CENTER] = get_string('aligncenter', 'customcert'); $alignmentoptions[element::ALIGN_RIGHT] = get_string('alignright', 'customcert'); - + $mform->addElement('select', 'alignment', get_string('alignment', 'customcert'), $alignmentoptions); $mform->setType('alignment', PARAM_ALPHA); $mform->setDefault('alignment', element::ALIGN_LEFT);