Skip to content

Commit

Permalink
Issue #70: Add type hinting for all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petersistrom committed Feb 8, 2024
1 parent 32f47d0 commit 5d14343
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function dismiss_notice_parameters() {
* @param int $noticeid Notice ID.
* @return array
*/
public static function dismiss_notice($noticeid) {
public static function dismiss_notice(int $noticeid) {
$params = self::validate_parameters(self::dismiss_notice_parameters(),
array('noticeid' => $noticeid));

Expand Down Expand Up @@ -98,7 +98,7 @@ public static function acknowledge_notice_parameters() {
* @param int $noticeid Notice ID.
* @return []
*/
public static function acknowledge_notice($noticeid) {
public static function acknowledge_notice(int $noticeid) {
$params = self::validate_parameters(self::acknowledge_notice_parameters(),
array('noticeid' => $noticeid));

Expand Down Expand Up @@ -146,7 +146,7 @@ public static function track_link_parameters() {
* @param int $linkid Link ID.
* @return array
*/
public static function track_link($linkid) {
public static function track_link(int $linkid) {
$params = self::validate_parameters(self::track_link_parameters(), array('linkid' => $linkid));
return helper::track_link($params['linkid']);
}
Expand Down
22 changes: 11 additions & 11 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function process_content(sitenotice $sitenotice) {
* @throws \core\invalid_persistent_exception
* @throws \required_capability_exception
*/
public static function create_new_notice($data) {
public static function create_new_notice(\stdClass $data) {
self::check_manage_capability();
// Create new notice.
self::sanitise_data($data);
Expand Down Expand Up @@ -93,7 +93,7 @@ public static function create_new_notice($data) {
* @throws \dml_exception
* @throws \required_capability_exception
*/
public static function update_notice(sitenotice $sitenotice, $data) {
public static function update_notice(sitenotice $sitenotice, \stdClass $data) {
self::check_manage_capability();
if (!get_config('local_sitenotice', 'allow_update')) {
return;
Expand Down Expand Up @@ -135,7 +135,7 @@ private static function sanitise_data(\stdClass $data) {
* @param string $content notice content
* @return string
*/
private static function update_hyperlinks(sitenotice $notice, $content): string {
private static function update_hyperlinks(sitenotice $notice, string $content): string {
// Replace file URLs before processing.
$content = file_rewrite_pluginfile_urls($content, 'pluginfile.php',
\context_system::instance()->id, 'local_sitenotice', 'content', $notice->get('id'));
Expand Down Expand Up @@ -303,7 +303,7 @@ public static function built_cohorts_options() {
* @param int $noticeid notice id
* @return bool|\stdClass
*/
public static function retrieve_notice($noticeid) {
public static function retrieve_notice(int $noticeid) {
$sitenotice = sitenotice::get_record(['id' => $noticeid]);
if ($sitenotice) {
return $sitenotice->to_record();
Expand Down Expand Up @@ -426,7 +426,7 @@ private static function load_viewed_notices() {
* @param \local_sitenotice\persistent\sitenotice $notice Notice instance.
* @param string $action Action.
*/
private static function add_to_viewed_notices(sitenotice $notice, $action) {
private static function add_to_viewed_notices(sitenotice $notice, string $action) {
global $USER;
// Add to viewed notices.
$noticeview = noticeview::add_notice_view($notice->get('id'), $USER->id, $action);
Expand All @@ -441,7 +441,7 @@ private static function add_to_viewed_notices(sitenotice $notice, $action) {
*
* @return \core\persistent
*/
private static function create_new_acknowledge_record(sitenotice $notice, $action) {
private static function create_new_acknowledge_record(sitenotice $notice, string $action) {
global $USER;

// New record.
Expand Down Expand Up @@ -544,7 +544,7 @@ public static function acknowledge_notice(sitenotice $notice): array {
* @param int $linkid link ID
* @return array
*/
public static function track_link($linkid) {
public static function track_link(int $linkid) {
global $USER;
$data = new \stdClass();
$data->hlinkid = $linkid;
Expand All @@ -563,7 +563,7 @@ public static function track_link($linkid) {
* @param string $time Time.
* @return string
*/
public static function format_interval_time($time) {
public static function format_interval_time(string $time) {
// Datetime for 01/01/1970.
$datefrom = new \DateTime("@0");
// Datetime for 01/01/1970 after the specified time (in seconds).
Expand All @@ -578,7 +578,7 @@ public static function format_interval_time($time) {
* @param bool $value boolean
* @return string
*/
public static function format_boolean($value) {
public static function format_boolean(bool $value) {
if ($value) {
return get_string('booleanformat:true', 'local_sitenotice');
} else {
Expand All @@ -592,7 +592,7 @@ public static function format_boolean($value) {
* @param int $cohortid Cohort id
* @return mixed
*/
public static function get_cohort_name($cohortid) {
public static function get_cohort_name(int $cohortid) {
if ($cohortid == 0) {
return get_string('notice:cohort:all', 'local_sitenotice');
}
Expand All @@ -607,7 +607,7 @@ public static function get_cohort_name($cohortid) {
* @return mixed
* @throws \coding_exception
*/
public static function get_course_name($courseid) {
public static function get_course_name(int $courseid) {
global $DB;

if ($courseid == 0) {
Expand Down
2 changes: 1 addition & 1 deletion classes/persistent/acknowledgement.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected static function define_properties() {
*
* @param int $noticeid notice id
*/
public static function delete_notice_acknowledgement($noticeid) {
public static function delete_notice_acknowledgement(int $noticeid) {
global $DB;
$DB->delete_records(static::TABLE, ['noticeid' => $noticeid]);
}
Expand Down
4 changes: 2 additions & 2 deletions classes/persistent/linkhistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected static function define_properties() {
*
* @param array $linkids array of link ids
*/
public static function delete_link_history($linkids) {
public static function delete_link_history(array $linkids) {
global $DB;
if (!empty($linkids)) {
list($linkidssql, $param) = $DB->get_in_or_equal($linkids, SQL_PARAMS_NAMED);
Expand All @@ -70,7 +70,7 @@ public static function delete_link_history($linkids) {
*
* @return array
*/
public static function count_clicked_links($userid, $noticeid, $linkid = 0) {
public static function count_clicked_links(int $userid, int $noticeid, int $linkid = 0) {
global $DB;
$params = [];
if ($linkid > 0 && is_numeric($linkid)) {
Expand Down
8 changes: 4 additions & 4 deletions classes/persistent/noticelink.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected static function define_properties() {
*
* @return array
*/
public static function get_notice_link_records($noticeid, $sort = 'id', $order = 'ASC') {
public static function get_notice_link_records(int $noticeid, string $sort = 'id', string $order = 'ASC') {
$persistents = self::get_records(['noticeid' => $noticeid], $sort, $order);
$result = [];
foreach ($persistents as $persistent) {
Expand All @@ -76,7 +76,7 @@ public static function get_notice_link_records($noticeid, $sort = 'id', $order =
*
* @param array $linkids array of link ids
*/
public static function delete_links($linkids) {
public static function delete_links(array $linkids) {
global $DB;
if (!empty($linkids)) {
list($linkidssql, $param) = $DB->get_in_or_equal($linkids, SQL_PARAMS_NAMED);
Expand All @@ -89,7 +89,7 @@ public static function delete_links($linkids) {
*
* @param int $noticeid notice id
*/
public static function delete_notice_links($noticeid) {
public static function delete_notice_links(int $noticeid) {
global $DB;
$DB->delete_records(static::TABLE, ['noticeid' => $noticeid]);
}
Expand All @@ -100,7 +100,7 @@ public static function delete_notice_links($noticeid) {
* @param \stdClass $data link data
* @return persistent
*/
public static function create_new_link($data) {
public static function create_new_link(\stdClass $data) {
$linkpersistent = self::get_record([
'noticeid' => $data->noticeid,
'text' => $data->text,
Expand Down
4 changes: 2 additions & 2 deletions classes/persistent/noticeview.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function after_delete($result) {
*
* @return persistent|false|noticeview
*/
public static function add_notice_view($noticeid, $userid, $action) {
public static function add_notice_view(int $noticeid, int $userid, int $action) {
$persistent = self::get_record(['noticeid' => $noticeid, 'userid' => $userid]);
if (!empty($persistent)) {
$persistent->set('action', $action);
Expand All @@ -130,7 +130,7 @@ public static function add_notice_view($noticeid, $userid, $action) {
*
* @param int $noticeid notice id
*/
public static function delete_notice_view($noticeid) {
public static function delete_notice_view(int $noticeid) {
global $DB;
$DB->delete_records(static::TABLE, ['noticeid' => $noticeid]);
}
Expand Down
6 changes: 3 additions & 3 deletions classes/persistent/sitenotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function get_all_notices(): array {
* @throws \coding_exception
* @throws \core\invalid_persistent_exception
*/
public static function create_new_notice($data) {
public static function create_new_notice(\stdClass $data) {
if (isset($data->cohorts) && is_array($data->cohorts)) {
$data->cohorts = implode(',', $data->cohorts);
} else {
Expand All @@ -210,7 +210,7 @@ public static function create_new_notice($data) {
* @throws \coding_exception
* @throws \core\invalid_persistent_exception
*/
public static function update_notice_content(sitenotice $persistent, $content) {
public static function update_notice_content(sitenotice $persistent, string $content) {
$persistent->set('content', $content);
return $persistent->update();
}
Expand All @@ -223,7 +223,7 @@ public static function update_notice_content(sitenotice $persistent, $content) {
* @throws \coding_exception
* @throws \core\invalid_persistent_exception
*/
public static function update_notice_data(sitenotice $persistent, $data) {
public static function update_notice_data(sitenotice $persistent, \stdClass $data) {
if (isset($data->cohorts) && is_array($data->cohorts)) {
$data->cohorts = implode(',', $data->cohorts);
} else {
Expand Down
2 changes: 1 addition & 1 deletion classes/report_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class report_filter {
*
* @throws \coding_exception
*/
public function __construct($baseurl, $tablealias = '') {
public function __construct(string $baseurl, string $tablealias = '') {
$tablealias = $tablealias ? $tablealias . "." : '';
$this->filterfields = [
"timecreated" => new \user_filter_date('time', get_string('time'), false, "{$tablealias}timecreated"),
Expand Down
12 changes: 6 additions & 6 deletions classes/table/acknowledged_notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class acknowledged_notice extends table_sql implements renderable {
* @param int $page current page.
* @param int $perpage number of record per page.
*/
public function __construct($uniqueid, \moodle_url $url, $noticeid, $filters = [],
$download = '', $page = 0, $perpage = 20) {
public function __construct(string $uniqueid, \moodle_url $url, int $noticeid, array $filters = [],
string $download = '', int $page = 0, int $perpage = 20) {
parent::__construct($uniqueid);

$this->set_attribute('class', 'local_sitenotice acknowledged_notices');
Expand Down Expand Up @@ -150,7 +150,7 @@ protected function define_table_configs(\moodle_url $url) {
* @param bool $count whether count or get records.
* @return array
*/
protected function get_sql_and_params($count = false) {
protected function get_sql_and_params(bool $count = false) {
$alias = self::TABLE_ALIAS;
if ($count) {
$select = "COUNT(1)";
Expand Down Expand Up @@ -220,7 +220,7 @@ public function query_db($pagesize, $useinitialsbar = true) {
* @param \stdClass $row dismissed notice record
* @return string
*/
protected function col_timecreated($row) {
protected function col_timecreated(\stdClass $row) {
$this->previoususer = $row->userid;
if ($row->timecreated) {
return userdate($row->timecreated, get_string('report:timeformat:sortable', 'local_sitenotice'), null, false);
Expand All @@ -235,7 +235,7 @@ protected function col_timecreated($row) {
* @param \stdClass $row dismissed notice record
* @return string
*/
protected function col_timecreated_spreadsheet($row) {
protected function col_timecreated_spreadsheet(\stdClass $row) {
if ($row->timecreated) {
return self::DAY_SECS_SPREADSHEET_DIFF + ($row->timecreated / DAYSECS);
} else {
Expand All @@ -249,7 +249,7 @@ protected function col_timecreated_spreadsheet($row) {
* @param \stdClass $row acknowledged notice record
* @return string
*/
protected function col_hlinkcount($row) {
protected function col_hlinkcount(\stdClass $row) {
// Only count on the first record of a user.
if ($this->previoususer == $row->userid) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion classes/table/all_notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function define_table_configs(\moodle_url $url) {
* @param int $pagesize number of records to fetch
* @param bool $useinitialsbar initial bar
*/
public function query_db($pagesize, $useinitialsbar = true): void {
public function query_db(int $pagesize, bool $useinitialsbar = true): void {
$records = sitenotice::get_records([], 'enabled, timemodified', 'DESC', $this->pagesize * $this->page, $this->pagesize);
$total = count($records);
$this->pagesize($pagesize, $total);
Expand Down
12 changes: 6 additions & 6 deletions classes/table/dismissed_notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class dismissed_notice extends table_sql implements renderable {
* @param int $page current page.
* @param int $perpage number of record per page.
*/
public function __construct($uniqueid, \moodle_url $url, $noticeid, $filters = [], $download = '',
$page = 0, $perpage = 20) {
public function __construct(string $uniqueid, \moodle_url $url, int $noticeid, array $filters = [], string $download = '',
int $page = 0, int $perpage = 20) {
parent::__construct($uniqueid);

$this->set_attribute('class', 'local_sitenotice dismissed_notices');
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function define_table_configs(\moodle_url $url) {
* @param bool $count whether count or get records.
* @return array
*/
protected function get_sql_and_params($count = false) {
protected function get_sql_and_params(bool $count = false) {
$alias = self::TABLE_ALIAS;
if ($count) {
$select = "COUNT(1)";
Expand Down Expand Up @@ -173,7 +173,7 @@ protected function get_filters_sql_and_params() {
* @param int $pagesize number of records to fetch
* @param bool $useinitialsbar initial bar
*/
public function query_db($pagesize, $useinitialsbar = true) {
public function query_db(int $pagesize, bool $useinitialsbar = true) {
global $DB;

list($countsql, $countparams) = $this->get_sql_and_params(true);
Expand All @@ -199,7 +199,7 @@ public function query_db($pagesize, $useinitialsbar = true) {
* @param \stdClass $row dismissed notice record
* @return string
*/
protected function col_timecreated($row) {
protected function col_timecreated(\stdClass $row) {
if ($row->timecreated) {
return userdate($row->timecreated, get_string('report:timeformat:sortable', 'local_sitenotice'), null, false);
} else {
Expand All @@ -213,7 +213,7 @@ protected function col_timecreated($row) {
* @param \stdClass $row dismissed notice record
* @return string
*/
protected function col_timecreated_spreadsheet($row) {
protected function col_timecreated_spreadsheet(\stdClass $row) {
if ($row->timecreated) {
return self::DAY_SECS_SPREADSHEET_DIFF + ($row->timecreated / DAYSECS);
} else {
Expand Down

0 comments on commit 5d14343

Please sign in to comment.