Alan Lam 7 лет назад
Родитель
Сommit
6e1258877e

+ 1 - 1
engine/account_icon.php

@@ -18,4 +18,4 @@ class AccountIcon {
 		$this->counter = $counter;
 	}
 
-}
+}

+ 17 - 7
engine/chart.php

@@ -1,8 +1,10 @@
-<?php
+<?php /** @noinspection DuplicatedCode */
+/** @noinspection PhpUnused */
+/** @noinspection PhpUndefinedMethodInspection */
+/** @noinspection PhpUnusedLocalVariableInspection */
 
-use PhpOffice\PhpPresentation\Style\Color;
+use PhpOffice\PhpPresentation\Slide;
 use PhpOffice\PhpPresentation\Style\Alignment;
-use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
 
 class Chart {
 
@@ -59,6 +61,10 @@ class Chart {
 	public $printPercentages = false;
 	public $printTotalColumn = false;
 
+	/**
+	 * Chart constructor.
+	 * @throws Exception
+	 */
 	public function __construct() {
 		$this->id = bin2hex(random_bytes(8));
 	}
@@ -383,7 +389,7 @@ class Chart {
 	}
 
 	// Presentation methods
-	public function addChart($slide, $path) {
+	public function addChart(Slide $slide, string $path) {
 
 		$chart_offset_y = 100;
 		if($this->print) {
@@ -401,7 +407,13 @@ class Chart {
 		return true;
 	}
 
-	public function addDataTable($slide, $offset_x = 30, $offset_y = 540) {
+	/**
+	 * @param Slide $slide
+	 * @param int $offset_x
+	 * @param int $offset_y
+	 * @throws Exception
+	 */
+	public function addDataTable(Slide $slide, int $offset_x = 30, int $offset_y = 540) {
 
 		$labels = $this->getLabels();
 		$datasets = array_merge($this->getDatasets(), $this->getPrintDatasets());
@@ -506,5 +518,3 @@ class Chart {
 	}
 
 }
-
-?>

+ 6 - 3
engine/constraint.php

@@ -22,7 +22,12 @@ class Constraint {
 	private $type;
 	private $values = [];
 
-	public function __construct($type) {
+	/**
+	 * Constraint constructor.
+	 * @param string $type
+	 * @throws Exception
+	 */
+	public function __construct(string $type) {
 		$type = "TYPE_" . strtoupper($type);
 		if(!defined("Constraint::$type")) throw new Exception("Constraint::$type is not defined.");
 		$this->type = constant("Constraint::$type");
@@ -73,5 +78,3 @@ class Constraint {
 	}
 
 }
-
-?>

+ 17 - 4
engine/core.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnused */
 
 
 class Core {
@@ -85,6 +85,9 @@ class Core {
 		}
 	}
 
+	/**
+	 * @throws Exception
+	 */
 	public static function requireAdmin() {
 		Core::requireLogIn();
 		if(Core::$user->canAdmin() === false) {
@@ -92,6 +95,10 @@ class Core {
 		}
 	}
 
+	/**
+	 * @param $level
+	 * @throws Exception
+	 */
 	public static function requireUserLevel($level) {
 		Core::requireLogIn();
 		if(Core::$user->level < $level) {
@@ -103,11 +110,19 @@ class Core {
 		return !empty($_SESSION["user_id"]);
 	}
 
-	public static function validateCSRFToken($token) {
+	/**
+	 * @param string $token
+	 * @return bool
+	 * @throws Exception
+	 */
+	public static function validateCSRFToken(string $token) {
 		if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
 		return true;
 	}
 
+	/**
+	 * @throws Exception
+	 */
 	public static function refuseAJAX() {
 		if(HTTP::isAJAX()) {
 			header("HTTP/1.1 403 Forbidden");
@@ -116,5 +131,3 @@ class Core {
 	}
 
 }
-
-?>

+ 19 - 5
engine/customer.php

@@ -17,8 +17,6 @@
  * @property string $internal_id_field_name
  * @property string $region_field_name
  * @property string $area_manager_field_name
- *
- * @method Customer get(int $id)
  */
 
 class Customer extends Model {
@@ -33,7 +31,12 @@ class Customer extends Model {
 	];
 
 	// Static methods
-	public static function getForAdmin() {
+
+	/**
+	 * @return Customer
+	 * @throws Exception
+	 */
+	public static function getForAdmin(): ?Customer {
 		$customers = Core::$user->getCustomers();
 		if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
 		else if(count($customers) == 1) {
@@ -54,7 +57,7 @@ class Customer extends Model {
 			// Enforce restriction if CUSTOMER or lower
 			if(Core::$user->level < User::LEVEL_ADMIN) {
 				$link = UserCustomer::get(Core::$user->id, $customer_id);
-				if(!$link) $customer = false;
+				if(!$link) $customer = null;
 			}
 		}
 
@@ -64,13 +67,20 @@ class Customer extends Model {
 	}
 
 	// Object methods
-	public function getLocations($query = []) {
+
+	/**
+	 * @param array $query
+	 * @return Location[]
+	 * @throws Exception
+	 */
+	public function getLocations(array $query = []): array {
 		$nvp = array_merge(["customer_id" => $this->id], $query);
 		return Location::query($nvp, ["sort" => "name"]);
 	}
 
 	/**
 	 * @return Project[]
+	 * @throws Exception
 	 */
 	public function getProjects() {
 		return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
@@ -78,11 +88,15 @@ class Customer extends Model {
 
 	/**
 	 * @return Template[]
+	 * @throws Exception
 	 */
 	public function getTemplates() {
 		return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
 	}
 
+	/**
+	 * @return string
+	 */
 	public function getTestResultsUrl(): string {
 		$slug = Util::slugWithId($this->name, $this->id);
 		$url = "/account/test-results/{$slug}";

+ 122 - 42
engine/db.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnused */
 
 class db {
 
@@ -9,67 +9,103 @@ class db {
 
 	public static $DB = null;
 
-	public static function affected_rows($link = null) {
+	public static function affected_rows(mysqli $link = null): int {
 		if(empty($link)) $link = db::$DB;
 		return mysqli_affected_rows($link);
 	}
 
-	public static function cast_all_rows($result, $class_name) {
-		$objects = [];
-		while($row = db::fetch_object($result)) {
-			$objects[] = new $class_name($row);
-		}
-		return $objects;
-	}
-
-	public static function connect($host, $username, $password, $database = "", $port = 3306) {
-		db::$DB = @new mysqli($host, $username, $password, $database, $port);
-		if(!is_null(db::$DB->connect_error)) {
-			throw new Exception("Connect Error: " . db::$DB->connect_error);
+	/**
+	 * @param string $host
+	 * @param string $username
+	 * @param string $password
+	 * @param string $database
+	 * @param int $port
+	 * @return mysqli
+	 * @throws Exception
+	 */
+	public static function connect(string $host, string $username, string $password, string $database = "", int $port = 3306): mysqli {
+		$link = @new mysqli($host, $username, $password, $database, $port);
+		db::$DB = $link;
+
+		if(!is_null($link->connect_error)) {
+			throw new Exception("Connect Error: " . $link->connect_error);
 		}
 		db::query("SET NAMES utf8mb4");
-		return db::$DB;
+		return $link;
 	}
 
-	public static function data_seek($result, $row_number) {
-		mysqli_data_seek($result, $row_number);
+	/**
+	 * @param mysqli_result $result
+	 * @param int $row_number
+	 * @return bool
+	 */
+	public static function data_seek(mysqli_result $result, int $row_number): bool {
+		return mysqli_data_seek($result, $row_number);
 	}
 
-	public static function escape($string) {
+	/**
+	 * @param string $string
+	 * @return string
+	 */
+	public static function escape(string $string): string {
 		return mysqli_real_escape_string(db::$DB, $string);
 	}
 
-	public static function error() {
+	/**
+	 * @return string
+	 */
+	public static function error(): string {
 		return mysqli_error(db::$DB);
 	}
 
-	public static function fetch_array($result) {
+	/**
+	 * @param mysqli_result $result
+	 * @return array|null
+	 */
+	public static function fetch_array(mysqli_result $result): ?array {
 		return mysqli_fetch_array($result);
 	}
 
-	public static function fetch_assoc($result) {
+	/**
+	 * @param mysqli_result $result
+	 * @return array|null
+	 */
+	public static function fetch_assoc(mysqli_result $result): ?array {
 		return mysqli_fetch_assoc($result);
 	}
 
-	public static function fetch_object($result) {
+	/**
+	 * @param mysqli_result $result
+	 * @return object|null
+	 */
+	public static function fetch_object(mysqli_result $result): ?object {
 		return mysqli_fetch_object($result);
 	}
 
-	public static function fetch_row($result) {
+	/**
+	 * @param mysqli_result $result
+	 * @return array|null
+	 */
+	public static function fetch_row(mysqli_result $result): ?array {
 		return mysqli_fetch_row($result);
 	}
 
-	public static function free_result($result) {
+	public static function free_result(mysqli_result $result): void {
 		mysqli_free_result($result);
 	}
 
-	public static function get_value_string($pairs, $escape=false) {
+	/**
+	 * @param array $pairs
+	 * @param bool $escape
+	 * @return string
+	 */
+	public static function get_value_string(array $pairs, bool $escape = false): string {
 
 		if($escape) {
 			$pairs = array_map(array("db", "escape"), $pairs);
 		}
 
-		$string_array = array();
+		$string_array = [];
 
 		foreach($pairs as $field => $value) {
 			if(is_null($value)) {
@@ -85,7 +121,12 @@ class db {
 		return implode(", ", $string_array);
 	}
 
-	public static function get_where_string($pairs, $options = []) {
+	/**
+	 * @param array $pairs
+	 * @param array|bool $options
+	 * @return string
+	 */
+	public static function get_where_string(array $pairs, $options = []): string {
 
 		$escape = false;
 		$fuzzy = false;
@@ -104,7 +145,7 @@ class db {
 			$pairs = array_map(array("db", "escape"), $pairs);
 		}
 
-		$string_array = array();
+		$string_array = [];
 
 		foreach($pairs as $field => $value) {
 			if(is_null($value)) {
@@ -128,17 +169,30 @@ class db {
 		return implode(" AND ", $string_array);
 	}
 
-	public static function has_index($table, $index_name) {
+	/**
+	 * @param string $table
+	 * @param string $index_name
+	 * @return bool
+	 * @throws Exception
+	 */
+	public static function has_index(string $table, string $index_name): bool {
 		$sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'";
 		$result = db::query($sql);
 		return db::num_rows($result)==1;
 	}
 
-	public static function insert_id() {
+	/**
+	 * @return int
+	 */
+	public static function insert_id(): int {
 		return mysqli_insert_id(db::$DB);
 	}
 
-	public static function num_rows($result) {
+	/**
+	 * @param mysqli_result $result
+	 * @return int
+	 */
+	public static function num_rows(mysqli_result $result): int {
 
 		if(is_bool($result)) {
 			return 0;
@@ -147,21 +201,40 @@ class db {
 		return mysqli_num_rows($result);
 	}
 
-	public static function object_or_false($result) {
-		return mysqli_num_rows($result)>0 ? db::fetch_object($result) : false;
+	/**
+	 * @param mysqli_result $result
+	 * @return object|null
+	 */
+	public static function object_or_null(mysqli_result $result): ?object {
+		return mysqli_num_rows($result)>0 ? db::fetch_object($result) : null;
 	}
 
-	public static function object_with_id($table, $field, $id) {
+	/**
+	 * @param string $table
+	 * @param string $field
+	 * @param int $id
+	 * @return object|null
+	 * @throws Exception
+	 */
+	public static function object_with_id(string $table, string $field, int $id): ?object {
 		$sql = "SELECT * FROM `$table` WHERE `$field`='$id'";
 		$result = db::query($sql);
-		return db::object_or_false($result);
+		return db::object_or_null($result);
 	}
 
-	public static function ping() {
+	/**
+	 * @return bool
+	 */
+	public static function ping(): bool {
 		return mysqli_ping(db::$DB);
 	}
 
-	public static function query($sql) {
+	/**
+	 * @param string $sql
+	 * @return bool|mysqli_result
+	 * @throws Exception
+	 */
+	public static function query(string $sql) {
 		if(!is_string($sql)) return false;
 
 		self::$last_query = $sql;
@@ -182,18 +255,25 @@ class db {
 		return $result;
 	}
 
-	public static function select_db($name, $link_identifier=NULL) {
+	/**
+	 * @param string $name
+	 * @param mysqli|null $link_identifier
+	 * @return bool
+	 */
+	public static function select_db(string $name, mysqli $link_identifier = null): bool {
 		if(is_null($link_identifier)) {
-			mysqli_select_db(db::$DB, $name);
+			return mysqli_select_db(db::$DB, $name);
 		}
 		else {
-			mysqli_select_db($link_identifier, $name);
+			return mysqli_select_db($link_identifier, $name);
 		}
 	}
 
-	public static function thread_id() {
+	/**
+	 * @return int
+	 */
+	public static function thread_id(): int {
 		return mysqli_thread_id(db::$DB);
 	}
 }
 
-?>

+ 57 - 12
engine/email.php

@@ -1,9 +1,10 @@
-<?php
+<?php /** @noinspection PhpUnused */
 
 use PHPMailer\PHPMailer\PHPMailer;
 
 class Email {
 
+    /** @var PHPMailer $instance */
 	private $instance;
 
 	private $htmlContent = "";
@@ -41,7 +42,11 @@ class Email {
 		$this->instance = Email::getPHPMailerInstance();
 	}
 
-	public function loadHtmlTemplate($template_name) {
+	/**
+	 * @param string $template_name
+	 * @throws Exception
+	 */
+	public function loadHtmlTemplate(string $template_name) {
 
 		$this->instance->IsHTML(true);
 
@@ -53,41 +58,81 @@ class Email {
 		$this->htmlContent = $content;
 	}
 
-	public function setValues($values = []) {
+	/**
+	 * @param array $values
+	 * @return bool
+	 */
+	public function setValues(array $values = []) {
 		foreach($values as $key => $value) {
 			$this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
 		}
 		return true;
 	}
 
-	public function addAttachment($path, $name = "", $encoding = "base64", $type = "", $disposition = "attachment") {
+	/**
+	 * @param string $path
+	 * @param string $name
+	 * @param string $encoding
+	 * @param string $type
+	 * @param string $disposition
+	 * @return bool
+	 * @throws \PHPMailer\PHPMailer\Exception
+	 */
+	public function addAttachment(string $path, string $name = "", string $encoding = "base64", string $type = "", string $disposition = "attachment") {
 		return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
 	}
 
-	public function setFrom($email, $name="") {
+	/**
+	 * @param string $email
+	 * @param string $name
+	 * @return bool
+	 * @throws \PHPMailer\PHPMailer\Exception
+	 */
+	public function setFrom(string $email, string $name = "") {
 		return $this->instance->setFrom($email, $name);
 	}
 
-	public function addAddress($email, $name="") {
+	/**
+	 * @param string $email
+	 * @param string $name
+	 * @return bool
+	 */
+	public function addAddress(string $email, string $name = "") {
 		return $this->instance->addAddress($email, $name);
 	}
 
-	public function addReplyTo($email, $name="") {
+	/**
+	 * @param string $email
+	 * @param string $name
+	 * @return bool
+	 */
+	public function addReplyTo(string $email, string $name = "") {
 		return $this->instance->addReplyTo($email, $name);
 	}
 
-	public function setSubject($subject) {
+	/**
+	 * @param string $subject
+	 * @return bool
+	 */
+	public function setSubject(string $subject) {
 		$this->instance->Subject = $subject;
 		return true;
 	}
 
-	public function setHtmlContent($string) {
+	/**
+	 * @param string $string
+	 */
+	public function setHtmlContent(string $string) {
 		$this->instance->IsHTML(true);
 
 		$this->htmlContent = $string;
 	}
 
-	public function send() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function send(): bool {
 		// Does our sanity checks and then send email
 		$instance = $this->instance;
 
@@ -97,7 +142,7 @@ class Email {
 		if(empty($this->htmlContent)) throw new Exception("No content is set.");
 
 		// Embed images
-		$match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
+		$match_count = preg_match_all("/src=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
 		if($match_count > 0) {
 
 			$paths = $all_matches[1];
@@ -114,7 +159,7 @@ class Email {
 			}
 		}
 
-		$count = preg_match_all("/\{([a-z\-]+)\}/", $this->htmlContent, $matches);
+		$count = preg_match_all("/{([a-z\-]+)}/", $this->htmlContent, $matches);
 		if($count > 0) {
 			$missing_keys = [];
 			foreach($matches[1] as $key) {

+ 4 - 2
engine/email_builder.php

@@ -1,4 +1,6 @@
-<?php
+<?php /** @noinspection HtmlRequiredLangAttribute */
+/** @noinspection HtmlDeprecatedTag */
+/** @noinspection HtmlDeprecatedAttribute */
 
 class EmailBuilder {
 
@@ -135,4 +137,4 @@ EOD;
 
 	}
 
-}
+}

+ 43 - 12
engine/form.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection DuplicatedCode */
 
 class Form {
 
@@ -7,11 +7,20 @@ class Form {
 		1 => YES,
 	];
 
-	public static function checked($value) {
+	/**
+	 * @param $value
+	 * @return string
+	 */
+	public static function checked($value): string {
 		return !empty($value) ? "checked" : "";
 	}
 
-	public static function checkRecaptcha($required_score = 0.5) {
+	/**
+	 * @param float $required_score
+	 * @return bool
+	 * @throws Exception
+	 */
+	public static function checkRecaptcha(float $required_score = 0.5) {
 
 		if(!defined("GOOGLE_RECAPTCHA_SECRET")) return true;
 
@@ -124,7 +133,7 @@ class Form {
 			}
 		}
 
-		if(!$countries) return false;
+		if(!$countries) return;
 
 		$country_pairs = get_object_vars($countries);
 		sort($country_pairs);
@@ -142,7 +151,11 @@ class Form {
 		}
 	}
 
-	public static function renderField($options) {
+	/**
+	 * @param object $options
+	 * @throws Exception
+	 */
+	public static function renderField(object $options) {
 
 		switch($options->type) {
 
@@ -180,7 +193,11 @@ class Form {
 		<?php
 	}
 
-	private static function renderTextField($options) {
+	/**
+	 * @param object $options
+	 * @throws Exception
+	 */
+	private static function renderTextField(object $options) {
 		$id = str_replace("_", "-", $options->name);
 		$placeholder = "Enter " . strtolower($options->label);
 		$value = $options->value ?? http::value($options->name);
@@ -204,7 +221,11 @@ class Form {
 		<?php
 	}
 
-	private static function renderNumberField($options) {
+	/**
+	 * @param object $options
+	 * @throws Exception
+	 */
+	private static function renderNumberField(object $options) {
 		$id = str_replace("_", "-", $options->name);
 		$placeholder = "Enter " . strtolower($options->label);
 		$value = $options->value ?? http::value($options->name);
@@ -244,7 +265,11 @@ class Form {
 		<?php
 	}
 
-	private static function renderSelectField($options) {
+	/**
+	 * @param object $options
+	 * @throws Exception
+	 */
+	private static function renderSelectField(object $options) {
 		$id = str_replace("_", "-", $options->name);
 		$selected_value = $options->value ?? http::value($options->name);
 		$required = ($options->required ?? false) ? "required" : "";
@@ -277,9 +302,11 @@ class Form {
 		<?php
 	}
 
-	private static function renderFileField($options) {
+	/**
+	 * @param object $options
+	 */
+	private static function renderFileField(object $options) {
 		$id = str_replace("_", "-", $options->name);
-		$placeholder = "Enter " . strtolower($options->label);
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		?>
@@ -299,7 +326,11 @@ class Form {
 		<?php
 	}
 
-	private static function renderTextareaField($options) {
+	/**
+	 * @param object $options
+	 * @throws Exception
+	 */
+	private static function renderTextareaField(object $options) {
 		$id = str_replace("_", "-", $options->name);
 		$placeholder = "Enter " . strtolower($options->label);
 		$value = $options->value ?? http::value($options->name);
@@ -325,4 +356,4 @@ class Form {
 
 }
 
-?>
+?>

+ 1 - 1
engine/form_exception.php

@@ -30,4 +30,4 @@ class FormException extends Exception {
 		]);
 	}
 
-}
+}

+ 6 - 1
engine/geolocation.php

@@ -8,7 +8,6 @@ class Geolocation {
 	const MILES_IN_KM = 0.621371;
 	const KMS_IN_MILE = 1.60934;
 
-
 	public static function getBounds($lat_lng, $distance, $units = Geolocation::UNIT_MILES) {
 
 		list($latitude, $longitude) = $lat_lng;
@@ -65,6 +64,12 @@ class Geolocation {
 		}
 	}
 
+	/**
+	 * @param $address
+	 * @param string $region
+	 * @return array
+	 * @throws Exception
+	 */
 	public static function geocode($address, $region = "uk") {
 		$query = [
 			"address" =>  $address,

+ 0 - 2
engine/geolocation_bounds.php

@@ -40,5 +40,3 @@ class GeolocationBounds {
 	}
 
 }
-
-?>

+ 1 - 3
engine/html.php

@@ -15,9 +15,7 @@ class HTML {
 		$modified_time = filemtime($path);
 		$src = preg_replace("/^public\//", "/", $path);
 		?>
-		<link rel="stylesheet" type="text/css" href="<?=htmlspecialchars($src)?>?<?=$modified_time?>"></link>
+        <link rel="stylesheet" type="text/css" href="<?=htmlspecialchars($src)?>?<?=$modified_time?>" />
 		<?php
 	}
 }
-
-?>

+ 34 - 24
engine/http.php

@@ -8,7 +8,14 @@ class HTTP {
 		return (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest");
 	}
 
-	public static function post($url, $fields_or_data, $curl_options = []) {
+	/**
+	 * @param string $url
+	 * @param $fields_or_data
+	 * @param array $curl_options
+	 * @return bool|string
+	 * @throws Exception
+	 */
+	public static function post(string $url, $fields_or_data, $curl_options = []) {
 
 		$curl_handle = curl_init($url);
 
@@ -38,7 +45,12 @@ class HTTP {
 		header("Pragma: public");
 	}
 
-	public static function getDateFromFields($prefix = "") {
+	/**
+	 * @param string $prefix
+	 * @return string
+	 * @throws Exception
+	 */
+	public static function getDateFromFields(string $prefix = ""): string {
 		$date = false;
 
 		if(empty($prefix)) {
@@ -66,7 +78,12 @@ class HTTP {
 		return $date;
 	}
 
-	public static function getDateTimeFromFields($prefix = "") {
+	/**
+	 * @param string $prefix
+	 * @return bool|string
+	 * @throws Exception
+	 */
+	public static function getDateTimeFromFields(string $prefix = "") {
 		$date = http::getDateFromFields($prefix);
 		if($date === false) return false;
 
@@ -96,7 +113,14 @@ class HTTP {
 		return "$date $hour:$minute:$second";
 	}
 
-	public static function value($key, $validation = "none", $request_type = "request") {
+	/**
+	 * @param string $key
+	 * @param string $validation
+	 * @param string $request_type
+	 * @return mixed
+	 * @throws Exception
+	 */
+	public static function value(string $key, string $validation = "none", string $request_type = "request") {
 
 		$request_type = strtolower($request_type);
 
@@ -209,7 +233,12 @@ class HTTP {
 		header("HTTP/1.1 $string");
 	}
 
-	public static function validateUploadedFile($file) {
+	/**
+	 * @param array $file
+	 * @return bool
+	 * @throws Exception
+	 */
+	public static function validateUploadedFile(array $file) {
 		$file = (object) $file;
 		if($file->error > 0) {
 			switch($file->error) {
@@ -221,24 +250,5 @@ class HTTP {
 		return true;
 	}
 
-	public static function sendFile($path) {
-		$modified_time = filemtime($path);
-		$mime_type = Picture::getFileMIMEType($path);
-
-		$max_age = 7 * 86400;
-
-		header("Cache-Control: public, max-age=$max_age");
-
-		header("Expires: " . date("r", time() + $max_age));
-		header("Pragma: cache");
-
-		header("Last-Modified: " . date("r", $modified_time));
-
-		header("X-Sendfile: " . $path);
-		header("Content-Type: " . $mime_type);
-	}
-
-
 }
 
-?>

+ 8 - 9
engine/image.php

@@ -7,19 +7,18 @@ class Image {
 	public static function isJPEG($path) {
 		$size = @getimagesize($path);
 		if(!$size) return false;
-		list($width, $height, $type) = $size;
+		list(, , $type) = $size;
 		if($type != IMAGETYPE_JPEG) return false;
 		return true;
 	}
 
-	public static function normalise($path, $size = 1920) {
+	public static function normalise($path, $max_width = 1920) {
 
 		$size = getimagesize($path);
 
-		list($width, $height, $type) = $size;
+		list($width, $height) = $size;
 
 		$exif = exif_read_data($path);
-		$orientation = 0;
 		$rotate_angle = 0;
 		$is_flipped = false;
 		if($exif !== false) {
@@ -33,13 +32,13 @@ class Image {
 		$original_image = imagecreatefromjpeg($path);
 
 		if($width > $height) {
-			if($width > 1920) {
-				$width = 1920;
+			if($width > $max_width) {
+				$width = $max_width;
 			}
 		}
 		else {
-			if($height > 1920) {
-				$ratio = $height / 1920;
+			if($height > $max_width) {
+				$ratio = $height / $max_width;
 				$width = floor($width / $ratio);
 			}
 		}
@@ -67,4 +66,4 @@ class Image {
 
 	}
 
-}
+}

+ 0 - 2
engine/json.php

@@ -41,5 +41,3 @@ class JSON {
 	}
 
 }
-
-?>

+ 62 - 7
engine/location.php

@@ -1,7 +1,34 @@
 <?php
 
 /**
- * @method Location get(int $id)
+ * @property int $id
+ * @property int $customer_id
+ * @property string $name
+ * @property string $legal_entity_name
+ * @property string $address_1
+ * @property string $address_2
+ * @property string $city
+ * @property string $county
+ * @property string $postal_code
+ * @property string $state
+ * @property string $country
+ * @property string $email
+ * @property string $phone
+ * @property string $opening_hours
+ * @property string $shop_left
+ * @property string $shop_right
+ * @property string $brand
+ * @property string $region
+ * @property string $region_name
+ * @property string $internal_id
+ * @property string $area_manager
+ * @property string $area_manager_name
+ * @property string $area_manager_email
+ * @property string $operations_manager_name
+ * @property bool $closed
+ * @property double $latitude
+ * @property double $longitude
+ * @property string $dts_verified
  */
 
 class Location extends Model {
@@ -11,7 +38,13 @@ class Location extends Model {
 	protected static $fillable_fields = ["customer_id", "name", "legal_entity_name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone", "opening_hours", "shop_left", "shop_right", "brand", "region", "region_name", "internal_id", "area_manager", "area_manager_name", "area_manager_email", "operations_manager_name", "closed", "latitude", "longitude", "dts_verified"];
 
 	// Object methods
-	public function assignVenueTypes($types) {
+
+	/**
+	 * @param array $types
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function assignVenueTypes(array $types): bool {
 		$links = LocationVenueType::query(["location_id" => $this->id]);
 		foreach($links as $link) {
 			$link->delete();
@@ -27,14 +60,22 @@ class Location extends Model {
 		return true;
 	}
 
-	public function getVenueTypes() {
+	/**
+	 * @return VenueType[]
+	 * @throws Exception
+	 */
+	public function getVenueTypes(): array {
 		$links = LocationVenueType::query(["location_id" => $this->id]);
 		$ids = array_column($links, "venue_type_id");
 
 		return VenueType::query(["id" => $ids]);
 	}
 
-	public function getCustomer() {
+	/**
+	 * @return Customer|null
+	 * @throws Exception
+	 */
+	public function getCustomer(): ?Customer {
 		return Customer::get($this->customer_id);
 	}
 
@@ -63,6 +104,7 @@ class Location extends Model {
 
 	/**
 	 * @return LocationFile[]
+	 * @throws Exception
 	 */
 	public function getPhotos() {
 		return LocationFile::query([
@@ -73,6 +115,7 @@ class Location extends Model {
 
 	/**
 	 * @return Test[]
+	 * @throws Exception
 	 */
 	public function getTests() {
 		return Test::query([
@@ -86,6 +129,7 @@ class Location extends Model {
 
 	/**
 	 * @return Test[]
+	 * @throws Exception
 	 */
 	public function getCompletedTests() {
 		return Test::query([
@@ -97,8 +141,13 @@ class Location extends Model {
 		]);
 	}
 
-
-    public function logUpdate($old_data, $modified_fields) {
+	/**
+	 * @param object $old_data
+	 * @param array $modified_fields
+	 * @return bool
+	 * @throws Exception
+	 */
+    public function logUpdate(object $old_data, array $modified_fields): bool {
 
 		if(empty($modified_fields)) return true;
 
@@ -112,6 +161,7 @@ class Location extends Model {
 
 	/**
 	 * @return LocationUpdateLogEntry[]
+	 * @throws Exception
 	 */
 	public function getUpdateLogEntries() {
 
@@ -124,7 +174,12 @@ class Location extends Model {
 
 	}
 
-    private function getModifiedArray($old_data, $modified_fields): array {
+	/**
+	 * @param object $old_data
+	 * @param array $modified_fields
+	 * @return array
+	 */
+    private function getModifiedArray(object $old_data, array $modified_fields): array {
 
 		$new_data = $this->getModelData();
 

+ 16 - 4
engine/location_file.php

@@ -1,5 +1,14 @@
 <?php
 
+/**
+ * Class LocationFile
+ *
+ * @property int $id
+ * @property int $location_id
+ * @property int $type
+ * @property string $dts_uploaded
+ */
+
 class LocationFile extends Model {
 
 	protected static $table = "location_file";
@@ -9,9 +18,12 @@ class LocationFile extends Model {
 	const TYPE_PHOTO = 1;
 
 	// Object methods
-	public function getLocation() {
-		return Location::get($location_id);
+
+	/**
+	 * @return Location|null
+	 * @throws Exception
+	 */
+	public function getLocation(): ?Location {
+		return Location::get($this->location_id);
 	}
 }
-
-?>

+ 8 - 4
engine/location_update_log_entry.php

@@ -6,8 +6,6 @@
  * @property int $user_id
  * @property string $modifications_json
  * @property string $dts
- *
- * @method LocationUpdateLogEntry get(int $id)
  */
 
 class LocationUpdateLogEntry extends Model {
@@ -17,20 +15,26 @@ class LocationUpdateLogEntry extends Model {
 	protected static $fillable_fields = ["location_id", "user_id", "modifications_json", "dts"];
 
 	// Object methods
+
 	/**
 	 * @return Location
+	 * @throws Exception
 	 */
-	public function getLocation() {
+	public function getLocation(): ?Location {
 		return Location::get($this->location_id);
 	}
 
 	/**
 	 * @return User
+	 * @throws Exception
 	 */
-	public function getUser() {
+	public function getUser(): ?User {
 		return User::get($this->user_id);
 	}
 
+	/**
+	 * @return array
+	 */
 	public function getModifications(): array {
 		return json_decode($this->modifications_json) ?: [];
 	}

+ 7 - 0
engine/location_venue_type.php

@@ -1,5 +1,12 @@
 <?php
 
+/**
+ * Class LocationVenueType
+ *
+ * @property int $location_id
+ * @property int $venue_type_id
+ */
+
 class LocationVenueType extends Model {
 
 	protected static $table = "location_venue_type";

+ 128 - 29
engine/model.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnused */
 
 abstract class Model {
 
@@ -17,7 +17,12 @@ abstract class Model {
 	protected $in_db;
 
 	// Magic methods
-	public function __get($name) {
+
+	/**
+	 * @param string $name
+	 * @return mixed
+	 */
+	public function __get(string $name) {
 
 		if(in_array($name, static::$object_fields)) {
 			return $this->$name ?? null;
@@ -30,7 +35,12 @@ abstract class Model {
 		return isset($this->model_data->$name) ? $this->model_data->$name : null;
 	}
 
-	public function __set($name, $value) {
+	/**
+	 * @param string $name
+	 * @param string $value
+	 * @throws Exception
+	 */
+	public function __set(string $name, string $value): void {
 
 		if(in_array($name, static::$object_fields)) {
 			$this->$name = $value;
@@ -68,14 +78,23 @@ abstract class Model {
 		}
 	}
 
-	public function __isset($name) {
+	/**
+	 * @param string $name
+	 * @return bool
+	 */
+	public function __isset(string $name) {
 		$value = $this->$name;
 		return !is_null($value);
 	}
 
 	// Static method
 
-	public static function get($id) {
+	/**
+	 * @param $id
+	 * @return static|null
+	 * @throws Exception
+	 */
+	public static function get($id): ?object {
 
 		$table = static::$table;
 		$is_compound_key = is_array(static::$id_field);
@@ -86,13 +105,13 @@ abstract class Model {
 		}
 
 		$id_field = static::$id_field;
-		$class = get_called_class();
+		$nvp = [];
 
-		if($is_compound_key) { // Compound key
+		if($is_compound_key && is_array($id_field)) { // Compound key
 			$args = func_get_args();
 			$fields = $id_field;
 
-			if(count($args) != count($fields)) return false;
+			if(count($args) != count($fields)) return null;
 
 			foreach($fields as $index => $field) {
 				$nvp[$field] = $args[$index];
@@ -106,10 +125,10 @@ abstract class Model {
 
 		$sql = "SELECT * FROM `$table` WHERE $where";
 		$result = db::query($sql);
-		$object = db::object_or_false($result);
+		$object = db::object_or_null($result);
 
 		if($object) {
-			$object = new $class($object);
+			$object = new static($object);
 			if(!$is_compound_key) {
 				static::$repositories[$table][$id] = $object;
 			}
@@ -118,7 +137,13 @@ abstract class Model {
 		return $object;
 	}
 
-	public static function getOrException($id, $exception_message = false) {
+	/**
+	 * @param int $id
+	 * @param bool $exception_message
+	 * @return static|null
+	 * @throws Exception
+	 */
+	public static function getOrException(int $id, bool $exception_message = false): ?object {
 
 		if(!$exception_message) $exception_message = get_called_class() . " not found.";
 
@@ -127,7 +152,11 @@ abstract class Model {
 		return $object;
 	}
 
-	public static function getAll() {
+	/**
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function getAll(): array {
 		$objects = [];
 
 		$table = static::$table;
@@ -141,7 +170,12 @@ abstract class Model {
 		return $objects;
 	}
 
-	public static function load(...$id_sets) {
+	/**
+	 * @param array ...$id_sets
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function load(...$id_sets): array {
 
 		$table = static::$table;
 
@@ -162,7 +196,11 @@ abstract class Model {
 		return $items;
 	}
 
-	public static function loadAll() {
+	/**
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function loadAll(): array {
 		$table = static::$table;
 
 		$items = static::query([], ["id_as_key" => true]);
@@ -173,7 +211,12 @@ abstract class Model {
 		return $items;
 	}
 
-	public static function deleteMultiple($nvp = []) {
+	/**
+	 * @param array $nvp
+	 * @return int
+	 * @throws Exception
+	 */
+	public static function deleteMultiple($nvp = []): int {
 		$table = static::$table;
 
 		$vs = "1";
@@ -182,8 +225,6 @@ abstract class Model {
 			$vs = db::get_where_string($nvp);
 		}
 
-		$objects = [];
-
 		$sql = "DELETE FROM `$table` WHERE $vs";
 		$result = db::query($sql);
 		if(!$result) return false;
@@ -191,7 +232,13 @@ abstract class Model {
 		return db::affected_rows();
 	}
 
-	public static function query($nvp = [], $options = []) {
+	/**
+	 * @param string|array $nvp
+	 * @param array $options
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function query($nvp = [], $options = []): array {
 		if(is_array($nvp)) {
 			return static::findByNVP($nvp, $options);
 		}
@@ -200,17 +247,33 @@ abstract class Model {
 		}
 	}
 
-	public static function queryFuzzy($nvp = [], $options = []) {
+	/**
+	 * @param array $nvp
+	 * @param array $options
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function queryFuzzy($nvp = [], $options = []): array {
 		$options["fuzzy"] = true;
 		return static::findByNVP($nvp, $options);
 	}
 
-	public static function queryOne($nvp = [], $options = []) {
+	/**
+	 * @param array $nvp
+	 * @param array $options
+	 * @return static|null
+	 * @throws Exception
+	 */
+	public static function queryOne($nvp = [], $options = []): ?object {
 		$rows = static::findByNVP($nvp, $options);
-		return $rows ? $rows[0] : false;
+		return $rows ? $rows[0] : null;
 	}
 
-	private static function getRemappedNVP($nvp = []) {
+	/**
+	 * @param array $nvp
+	 * @return array
+	 */
+	private static function getRemappedNVP($nvp = []): array {
 		$mapped_nvp = [];
 		foreach($nvp as $field => $value) {
 			$field = static::$field_map[$field] ?? $field;
@@ -219,7 +282,12 @@ abstract class Model {
 		return $mapped_nvp;
 	}
 
-	public static function count($nvp = []) {
+	/**
+	 * @param array $nvp
+	 * @return int
+	 * @throws Exception
+	 */
+	public static function count($nvp = []): int {
 		$table = static::$table;
 
 		$vs = "1";
@@ -228,8 +296,6 @@ abstract class Model {
 			$vs = db::get_where_string($nvp);
 		}
 
-		$objects = [];
-
 		$sql = "SELECT COUNT(*) AS `count` FROM `$table` WHERE $vs";
 		$result = db::query($sql);
 		$row = db::fetch_object($result);
@@ -237,7 +303,13 @@ abstract class Model {
 		return $row->count;
 	}
 
-	public static function findByNVP($nvp, $options = []) {
+	/**
+	 * @param array $nvp
+	 * @param array $options
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function findByNVP(array $nvp, array $options = []): array {
 
 		if(empty($nvp)) {
 			$vs = "1";
@@ -250,7 +322,13 @@ abstract class Model {
 		return static::findByValueString($vs, $options);
 	}
 
-	private static function findByValueString($vs, $options = []) {
+	/**
+	 * @param string $vs
+	 * @param array $options
+	 * @return static[]
+	 * @throws Exception
+	 */
+	private static function findByValueString(string $vs, array $options = []): array {
 		$table = static::$table;
 
 		$options = (object) $options;
@@ -334,6 +412,11 @@ abstract class Model {
 		return $where;
 	}
 
+	/**
+	 * @param bool $testing
+	 * @return bool
+	 * @throws Exception
+	 */
 	public final function commit($testing = false) {
 
 		$this->preCommit();
@@ -401,16 +484,29 @@ abstract class Model {
 		return !empty($result);
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function testCommit() {
 		return $this->commit(true);
 	}
 
+	/**
+	 * @param bool $testing
+	 * @return bool
+	 * @throws Exception
+	 */
 	public final function commitOrException($testing = false) {
 		$success = $this->commit($testing);
 		if(!$success) throw new Exception("Database error.");
 		return $success;
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function delete() {
 		$table = static::$table;
 
@@ -423,9 +519,13 @@ abstract class Model {
 		$sql = "DELETE FROM `$table` WHERE $where";
 		$result = db::query($sql);
 
-		return empty($result) ? false : db::affected_rows() ? true : false;
+		return empty($result) ? false : (db::affected_rows() ? true : false);
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public final function deleteOrException() {
 		$success = $this->delete();
 		if(!$success) throw new Exception("Database error.");
@@ -455,4 +555,3 @@ abstract class Model {
 
 }
 
-?>

+ 16 - 5
engine/practice_test.php

@@ -5,20 +5,31 @@ class PracticeTest extends Test {
 	protected $values = [];
 
 	// Constants
-	public function getValues($as_assoc_array = false) {
+
+	/**
+	 * @param bool $as_assoc_array
+	 * @return array
+	 */
+	public function getValues(bool $as_assoc_array = false): array {
 		return $this->values;
 	}
 
-	public function setValue($key, $value) {
+	/**
+	 * @param string $key
+	 * @param string $value
+	 * @return bool
+	 */
+	public function setValue(string $key, string $value): bool {
 		$this->values[$key] = $value;
 		return true;
 	}
 
-	public function getDirectory() {
+	/**
+	 * @return string
+	 */
+	public function getDirectory(): string {
 		$directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
 		if(!file_exists($directory)) mkdir($directory, 0777, true);
 		return $directory;
 	}
 }
-
-?>

+ 147 - 34
engine/project.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpIncludeInspection */
 
 /**
  * @property int $id
@@ -20,8 +20,6 @@
  * @property string $dts_report
  * @property string $dts_finished
  * @property int $user_id_finished
- *
- * @method Project get(int $id)
  */
 
 class Project extends Model {
@@ -40,7 +38,13 @@ class Project extends Model {
 	protected $tests = null;
 
 	// Static methods
-	public static function exportCSV($processing_file, $projects = []) {
+
+	/**
+	 * @param string $processing_file
+	 * @param array $projects
+	 * @throws Exception
+	 */
+	public static function exportCSV(string $processing_file, /** @noinspection PhpUnusedParameterInspection */ $projects = []) {
 
 		// Get processing file
 		$processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
@@ -55,17 +59,20 @@ class Project extends Model {
 	}
 
 	// Object methods
+
 	/**
 	 * @return Customer
+	 * @throws Exception
 	 */
-	public function getCustomer() {
+	public function getCustomer(): ?Customer {
 		return Customer::get($this->customer_id);
 	}
 
 	/**
 	 * @return Test[]
+	 * @throws Exception
 	 */
-	public function getTests() {
+	public function getTests(): array {
 
 		if(!is_null($this->tests)) return $this->tests;
 
@@ -78,12 +85,20 @@ class Project extends Model {
 		return $this->tests;
 	}
 
-	public function getTestCount() {
+	/**
+	 * @return int
+	 * @throws Exception
+	 */
+	public function getTestCount(): int {
 		if(is_null($this->tests)) $this->getTests();
 		return count($this->tests);
 	}
 
-	public function getPassCount() {
+	/**
+	 * @return int
+	 * @throws Exception
+	 */
+	public function getPassCount(): int {
 		$count = 0;
 		$tests = $this->getTests();
 		foreach($tests as $test) {
@@ -93,7 +108,11 @@ class Project extends Model {
 		return $count;
 	}
 
-	public function getFailCount() {
+	/**
+	 * @return int
+	 * @throws Exception
+	 */
+	public function getFailCount(): int {
 		$count = 0;
 		$tests = $this->getTests();
 		foreach($tests as $test) {
@@ -103,7 +122,11 @@ class Project extends Model {
 		return $count;
 	}
 
-	public function getPendingCount() {
+	/**
+	 * @return int
+	 * @throws Exception
+	 */
+	public function getPendingCount(): int {
 		$count = 0;
 		$tests = $this->getTests();
 		foreach($tests as $test) {
@@ -113,6 +136,10 @@ class Project extends Model {
 		return $count;
 	}
 
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
 	public function getPassRate(): string {
 		$pass_count = $this->getPassCount();
 		$fail_count = $this->getFailCount();
@@ -122,12 +149,19 @@ class Project extends Model {
 		return (number_format($pass_count / $pass_fail_count, 4) * 100) . "%";
 	}
 
-	public function getLocations() {
+	/**
+	 * @return Location[]
+	 * @throws Exception
+	 */
+	public function getLocations(): array {
 		$tests = $this->getTests();
 		return Location::query(["id" => array_column($tests, "location_id")]);
 	}
 
-	public function getDisplayAgeRange() {
+	/**
+	 * @return string
+	 */
+	public function getDisplayAgeRange(): string {
 
 		if($this->min_age && $this->max_age) {
 			return $this->min_age . " to " . $this->max_age;
@@ -143,6 +177,9 @@ class Project extends Model {
 
 	}
 
+	/**
+	 * @return string
+	 */
 	public function getNameHtml(): string {
 
 		if($this->isAnytime()) {
@@ -155,7 +192,11 @@ class Project extends Model {
 		return htmlspecialchars($this->name) . " <i class=\"fa fa-fw fa-{$icon}\" data-toggle=\"tooltip\" title=\"{$time_of_day}\"></i>";
 	}
 
-	public function getCompletedPercentage() {
+	/**
+	 * @return float
+	 * @throws Exception
+	 */
+	public function getCompletedPercentage(): float {
 
 		$test_count = $this->test_count;
 
@@ -171,69 +212,109 @@ class Project extends Model {
 
 	/**
 	 * @return Template
+	 * @throws Exception
 	 */
-	public function getTemplate() {
+	public function getTemplate(): ?Template {
 		return Template::get($this->template_id);
 	}
 
-	public function getTemplateOverrides() {
+	/**
+	 * @return array
+	 */
+	public function getTemplateOverrides(): array {
 		$overrides = json_decode($this->template_overrides_json) ?: [];
 		return $overrides;
 	}
 
-	public function getTimeOfDayText() {
+	/**
+	 * @return string
+	 */
+	public function getTimeOfDayText(): string {
 		switch($this->time_of_day) {
+			default:
 			case self::TOD_ANYTIME: return "Any";
 			case self::TOD_DAYTIME: return "Day";
 			case self::TOD_NIGHTTIME: return "Night";
 		}
 	}
 
-	public function getTimeOfDayIcon() {
+	/**
+	 * @return string
+	 */
+	public function getTimeOfDayIcon(): string {
 		switch($this->time_of_day) {
+			default:
 			case self::TOD_ANYTIME: return "adjust";
 			case self::TOD_DAYTIME: return "sun";
 			case self::TOD_NIGHTTIME: return "moon";
 		}
 	}
 
-	public function getURL() {
+	/**
+	 * @return string
+	 */
+	public function getURL(): string {
 		return "/account/projects/{$this->id}";
 	}
 
-	public function getDirectory() {
+	/**
+	 * @return string
+	 */
+	public function getDirectory(): string {
 		$directory = ROOT_DIR . "/files/projects/" . $this->id;
 		if(!file_exists($directory)) mkdir($directory, 0777, true);
 		return $directory;
 	}
 
-	public function hasInvoicePDF() {
+	/**
+	 * @return bool
+	 */
+	public function hasInvoicePDF(): bool {
 		$directory = $this->getDirectory();
 		return file_exists($directory . "/invoice.pdf");
 	}
 
-	public function isAnytime() {
+	/**
+	 * @return bool
+	 */
+	public function isAnytime(): bool {
 		return $this->time_of_day == self::TOD_ANYTIME;
 	}
 
-	public function isDaytime() {
+	/**
+	 * @return bool
+	 */
+	public function isDaytime(): bool {
 		return $this->time_of_day == self::TOD_DAYTIME;
 	}
 
-	public function isNighttime() {
+	/**
+	 * @return bool
+	 */
+	public function isNighttime(): bool {
 		return $this->time_of_day == self::TOD_NIGHTTIME;
 	}
 
-	public function isPastDeadline() {
+	/**
+	 * @return bool
+	 */
+	public function isPastDeadline(): bool {
 		$deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
 		return time() > $deadline_unix;
 	}
 
-	public function isFinished() {
+	/**
+	 * @return bool
+	 */
+	public function isFinished(): bool {
 		return $this->dts_finished === null;
 	}
 
-	public function markFinished() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function markFinished(): bool {
 
 		if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
 		if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
@@ -243,13 +324,21 @@ class Project extends Model {
 		return $this->commit();
 	}
 
-	public function markUnfinished() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function markUnfinished(): bool {
 		$this->dts_finished = null;
 		$this->user_id_finished = null;
 		return $this->commit();
 	}
 
-	public function getDisplayReportMonth() {
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
+	public function getDisplayReportMonth(): string {
 		$report_month = "-";
 		if($this->dts_report) {
 			$dt_report = new DateTime($this->dts_report . " UTC");
@@ -258,7 +347,11 @@ class Project extends Model {
 		return $report_month;
 	}
 
-	public function getDataTableRow() {
+	/**
+	 * @return array
+	 * @throws Exception
+	 */
+	public function getDataTableRow(): array {
 
 		$tests = $this->getTests();
 
@@ -269,13 +362,17 @@ class Project extends Model {
 			"testCount" => count($tests),
 			"timeOfDay" => $this->getTimeOfDayText(),
 			"unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
-			"unixEnd" => Util::getUnixTimeFromUTC($this->dts_end),
+			"unixEnd" => Util::getUnixTimeFromUTC($this->dts_deadline),
 
 			"reportMonth" => $this->getDisplayReportMonth(),
 			"unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
 		];
 	}
 
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
 	public function getTestResultsUrl(): string {
 		$customer = $this->getCustomer();
 		$customer_slug = Util::slugWithId($customer->name, $customer->id);
@@ -283,6 +380,10 @@ class Project extends Model {
 		return "/account/test-results/{$customer_slug}/{$project_slug}";
 	}
 
+	/**
+	 * @return Invoice
+	 * @throws Exception
+	 */
 	public function getCustomerInvoice(): Invoice {
 
 		$invoice = new Invoice;
@@ -291,7 +392,6 @@ class Project extends Model {
 		$test_line_items = $this->getTestInvoiceLineItems();
 		$field_line_items = $this->getFieldInvoiceLineItems();
 
-
 		$invoice->addLineItems($test_line_items);
 		$invoice->addLineItems($field_line_items);
 
@@ -299,6 +399,10 @@ class Project extends Model {
 
 	}
 
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
 	private function getInvoiceReference(): string {
 
 		$customer = $this->getCustomer();
@@ -308,7 +412,10 @@ class Project extends Model {
 			"/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
 	}
 
-
+	/**
+	 * @return InvoiceLineItem[]
+	 * @throws Exception
+	 */
 	private function getTestInvoiceLineItems(): array {
 
 		/**
@@ -326,7 +433,9 @@ class Project extends Model {
 
 			$fee = $test->customer_fee ?? $default_customer_fee;
 
-			$item = $items[$fee] ?? null;
+			$key = "fee_{$fee}";
+
+			$item = $items[$key] ?? null;
 
 			if(!$item) {
 				$item = new InvoiceLineItem(__("%s Testing", $template->name));
@@ -335,13 +444,17 @@ class Project extends Model {
 
 			$item->incrementQuantity();
 
-			$items[$fee] = $item;
+			$items[$key] = $item;
 
 		}
 
 		return array_values($items);
 	}
 
+	/**
+	 * @return InvoiceLineItem[]
+	 * @throws Exception
+	 */
 	private function getFieldInvoiceLineItems(): array {
 
 		$template = $this->getTemplate();

+ 18 - 4
engine/project_location.php

@@ -1,5 +1,12 @@
 <?php
 
+/**
+ * Class ProjectLocation
+ *
+ * @property int $project_id
+ * @property int $location_id
+ */
+
 class ProjectLocation extends Model {
 
 	protected static $table = "project_location";
@@ -7,14 +14,21 @@ class ProjectLocation extends Model {
 	protected static $fillable_fields = ["project_id", "location_id"];
 
 	// Object methods
-	public function getProject() {
+
+	/**
+	 * @return Project|null
+	 * @throws Exception
+	 */
+	public function getProject(): ?Project {
 		return Project::get($this->project_id);
 	}
 
-	public function getLocation() {
+	/**
+	 * @return Location|null
+	 * @throws Exception
+	 */
+	public function getLocation(): ?Location {
 		return Location::get($this->location_id);
 	}
 
 }
-
-?>

+ 18 - 5
engine/session.php

@@ -1,12 +1,24 @@
 <?php
 
+/**
+ * Class Session
+ *
+ * @property int $id
+ * @property string $data
+ * @property string $dts_last_access
+ */
+
 class Session extends Model {
 
 	protected static $table = "session";
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["id", "data", "dts_last_access"];
 
-	public static function start($name="", $timeout=3600) {
+	/**
+	 * @param string $name
+	 * @param int $timeout
+	 */
+	public static function start(string $name = "", int $timeout = 3600) {
 
 		$handler = new SiteSessionHandler();
 		session_set_save_handler($handler, true);
@@ -19,12 +31,13 @@ class Session extends Model {
 		session_start();
 	}
 
-	public static function extend($timeout) {
+	/**
+	 * @param int $timeout
+	 */
+	public static function extend(int $timeout) {
 		$id = session_id();
 		$name = session_name();
-		setcookie($name, $id, time()+$timeout, "/", "");
+		setcookie($name, $id, time() + $timeout, "/", "");
 	}
 
 }
-
-?>

+ 22 - 3
engine/site_session_handler.php

@@ -6,11 +6,22 @@ class SiteSessionHandler implements SessionHandlerInterface {
 		return true;
 	}
 
+	/**
+	 * @param string $id
+	 * @return string
+	 * @throws Exception
+	 */
 	public function read($id) {
 		$session = Session::get($id);
 		return $session ? $session->data : "";
 	}
 
+	/**
+	 * @param string $id
+	 * @param string $data
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function write($id, $data) {
 
 		$session = Session::get($id);
@@ -28,6 +39,11 @@ class SiteSessionHandler implements SessionHandlerInterface {
 		return true;
 	}
 
+	/**
+	 * @param string $id
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function destroy($id) {
 
 		$session = Session::get($id);
@@ -35,15 +51,18 @@ class SiteSessionHandler implements SessionHandlerInterface {
 		return true;
 	}
 
+	/**
+	 * @param int $lifetime
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function gc($lifetime) {
 		$threshold = time() - $lifetime;
 		$date_time = date("Y-m-d H:i:s", $threshold);
 
 		$sql = "DELETE FROM `session` WHERE `dts_last_access` < '$date_time'";
 		$result = db::query($sql);
-		return $result;
+		return !empty($result);
 	}
 
 }
-
-?>

+ 71 - 16
engine/template.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpIncludeInspection */
 
 /**
  * @property int $id
@@ -7,21 +7,26 @@
  * @property string $json
  * @property string $json_file
  * @property bool $practice
+ * @property string $folder
  * @property int $pass_stage
- *
- * @method Template get(int $id)
  */
 
 class Template extends Model {
 
 	protected static $table = "template";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "pass_stage"];
+	protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
 
 	private $overrides = [];
 
 	// Object methods
-	public function exportTestsCSV($processing_file, $tests = []) {
+
+	/**
+	 * @param string $processing_file
+	 * @param Test[] $tests
+	 * @throws Exception
+	 */
+	public function exportTestsCSV(string $processing_file, array $tests = []) {
 
 		// Preload
 		Project::load(array_column($tests, "project_id"));
@@ -49,7 +54,10 @@ class Template extends Model {
 		fclose($handle);
 	}
 
-	public function getJSON() {
+	/**
+	 * @return string
+	 */
+	public function getJSON(): string {
 		$json_path = ROOT_DIR . "/json/" . $this->json_file;
 
 		if(!empty($this->json_file) && file_exists($json_path)) {
@@ -59,11 +67,20 @@ class Template extends Model {
 		return $this->json;
 	}
 
-	public function getCustomer() {
+	/**
+	 * @return Customer|null
+	 * @throws Exception
+	 */
+	public function getCustomer(): ?Customer {
 		return Customer::get($this->customer_id);
 	}
 
-	public function getProcessingFilePath($file) {
+	/**
+	 * @param $file
+	 * @return string
+	 * @throws Exception
+	 */
+	public function getProcessingFilePath($file): string {
 		if(!$this->folder) throw new Exception("Template processing folder not set.");
 
 		$folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
@@ -78,11 +95,17 @@ class Template extends Model {
 		return $file_path;
 	}
 
-	public function getURL() {
+	/**
+	 * @return string
+	 */
+	public function getURL(): string {
 		return "/account/templates/" . $this->id;
 	}
 
-	public function getFields() {
+	/**
+	 * @return array
+	 */
+	public function getFields(): array {
 		$fields = [];
 
 		$json = json_decode($this->getJSON());
@@ -98,21 +121,34 @@ class Template extends Model {
 		return $fields;
 	}
 
-	public function getTesterInvoiceFields() {
+	/**
+	 * @return array
+	 * @throws Exception
+	 */
+	public function getTesterInvoiceFields(): array {
 		$json = json_decode($this->getJSON());
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 
 		return $json->testerInvoiceFields ?? [];
 	}
 
-	public function getCustomerInvoiceFields() {
+	/**
+	 * @return array
+	 * @throws Exception
+	 */
+	public function getCustomerInvoiceFields(): array {
 		$json = json_decode($this->getJSON());
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 
 		return $json->customerInvoiceFields ?? [];
 	}
 
-	public function getTesterInvoiceCosts($values = []) {
+	/**
+	 * @param array $values
+	 * @return float
+	 * @throws Exception
+	 */
+	public function getTesterInvoiceCosts(array $values = []): float {
 		$costs = 0;
 
 		$invoice_fields = $this->getTesterInvoiceFields();
@@ -123,11 +159,18 @@ class Template extends Model {
 		return $costs;
 	}
 
-	public function setOverrides($overrides) {
+	/**
+	 * @param array $overrides
+	 */
+	public function setOverrides(array $overrides) {
 		$this->overrides = $overrides;
 	}
 
-	public function applyOverridesToField($field) {
+	/**
+	 * @param object $field
+	 * @return object
+	 */
+	public function applyOverridesToField(object $field): object {
 		foreach($this->overrides as $override) {
 			if(empty($field->name)) continue;
 			if($override->field == $field->name) {
@@ -141,6 +184,9 @@ class Template extends Model {
 		return $field;
 	}
 
+	/**
+	 * @throws Exception
+	 */
 	public function renderBeforeTestForm() {
 
 		$json = json_decode($this->getJSON());
@@ -173,6 +219,9 @@ class Template extends Model {
 		<?php
 	}
 
+	/**
+	 * @throws Exception
+	 */
 	public function renderDuringTestForm() {
 
 		$json = json_decode($this->getJSON());
@@ -312,11 +361,17 @@ class Template extends Model {
 		<?php
 	}
 
+	/**
+	 * @return int
+	 */
 	public function getPassStage(): int {
 		return (int) $this->pass_stage;
 	}
 
-	public function hasPassFail() {
+	/**
+	 * @return bool
+	 */
+	public function hasPassFail(): bool {
 		return !is_null($this->pass_stage);
 	}
 

+ 59 - 13
engine/test.php

@@ -463,13 +463,19 @@ class Test extends Model {
 	}
 
 	public function getMPDF() {
+		/** @var Mpdf $mpdf */
 		$id = $this->id;
 		$output = false;
 		require ROOT_DIR . "/controllers/test_pdf.php";
 		return $mpdf;
 	}
 
-	public function assignTester($tester) {
+	/**
+	 * @param User $tester
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function assignTester(User $tester) {
 		$this->tester_id = $tester->id;
 		$this->dts_assigned = Util::getDateTime();
 		$success = $this->commit();
@@ -480,7 +486,11 @@ class Test extends Model {
 
 	}
 
-	public function getPreviousTest() {
+	/**
+	 * @return Test|null
+	 * @throws Exception
+	 */
+	public function getPreviousTest(): ?Test {
 
 		$nvp = [
 			"dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
@@ -493,57 +503,87 @@ class Test extends Model {
 			"order" => "desc",
 		]);
 
-		return $tests ? $tests[0] : false;
+		return $tests ? $tests[0] : null;
 
 	}
 
-	public function unassignTester() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function unassignTester(): bool {
 		$this->status = Test::STATUS_UNASSIGNED;
 		$this->tester_id = null;
 		$this->dts_due = null;
 		$this->dts_assigned = null;
-		$success = $this->commit();
-
-		return $success;
+		return $this->commit();
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function isPass(): bool {
 		if(is_null($this->pass)) return false;
 		return !empty($this->pass);
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function isPending(): bool {
 		return is_null($this->pass);
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function isFail(): bool {
 		if(is_null($this->pass)) return false;
 		return empty($this->pass);
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function isCompleted() {
 		return $this->status == Test::STATUS_COMPLETED;
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function isPendingVerification() {
 		return $this->status == Test::STATUS_PENDING_VERIFICATION;
 	}
 
-	public function hasPDF() {
+	/**
+	 * @return bool
+	 */
+	public function hasPDF(): bool {
 		$directory = $this->getDirectory();
 		return file_exists($directory . "/pdf.pdf");
 	}
 
-	public function getPDFUrl() {
+	/**
+	 * @return string
+	 */
+	public function getPDFUrl(): string {
 		return "/account/tests/{$this->id}/files/pdf";
 	}
 
-	public function hasTesterInvoicePDF() {
+	/**
+	 * @return bool
+	 */
+	public function hasTesterInvoicePDF(): bool {
 		$directory = $this->getDirectory();
 		return file_exists($directory . "/tester_invoice.pdf");
 	}
 
-	public function markTesterInvoicePaid() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function markTesterInvoicePaid(): bool {
 
 		if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
 			$this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
@@ -554,7 +594,11 @@ class Test extends Model {
 		return false;
 	}
 
-	public function rejectTesterInvoice() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function rejectTesterInvoice(): bool {
 		if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
 			$this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
 			$this->dts_tester_invoice_generated = null;
@@ -568,6 +612,9 @@ class Test extends Model {
 		return false;
 	}
 
+	/**
+	 * @return string
+	 */
 	public function getTesterInvoiceReference(): string {
 
 		$location = $this->getLocation();
@@ -580,4 +627,3 @@ class Test extends Model {
 	}
 
 }
-?>

+ 5 - 1
engine/test_pdf.php

@@ -2,7 +2,11 @@
 
 class TestPDF {
 
-	public static function getValueHtml($value) {
+	/**
+	 * @param string $value
+	 * @return string
+	 */
+	public static function getValueHtml(string $value): string {
 		if(empty($value)) {
 			return "<span class=\"text-muted\">N/A</span>";
 		}

+ 0 - 7
engine/test_value.php

@@ -6,11 +6,4 @@ class TestValue extends Model {
 	protected static $id_field = ["test_id", "name"];
 	protected static $fillable_fields = ["test_id", "name", "value"];
 
-	protected static $field_map = [
-	];
-
-	// Object methods
-
 }
-
-?>

+ 152 - 22
engine/user.php

@@ -1,5 +1,43 @@
 <?php
 
+/**
+ * Class User
+ *
+ * @property int $id
+ * @property string $email
+ * @property int $old_id
+ * @property int $referrer_id
+ * @property string $password
+ * @property bool $activated
+ * @property int $level
+ * @property int $score
+ * @property string $title
+ * @property string $first_name
+ * @property string $last_name
+ * @property string $gender
+ * @property string $date_of_birth
+ * @property string $locale
+ * @property bool $reported
+ * @property string $reported_comment
+ * @property bool $can_claim
+ * @property string $phone
+ * @property string $verify_token
+ * @property string $reset_token
+ * @property bool $terms_accepted
+ * @property bool $newsletter_consent
+ * @property int $preferred_payment_method
+ * @property string $bank_name
+ * @property string $bank_sort_code
+ * @property string $bank_account_number
+ * @property string $paypal_email
+ * @property string $dts_registered
+ * @property string $dts_last_seen
+ * @property string $dts_verified
+ * @property string $dts_deleted
+ * @property int $verifier_id
+ * @property string $verifier_notes
+ */
+
 class User extends Model {
 
 	protected static $table = "user";
@@ -40,7 +78,11 @@ class User extends Model {
 	];
 
 	// Static methods
-	public static function titles() {
+
+	/**
+	 * @return array
+	 */
+	public static function titles(): array {
 		return [
 			"Miss" => MISS,
 			"Mr" => MR,
@@ -49,7 +91,10 @@ class User extends Model {
 		];
 	}
 
-	public static function genders() {
+	/**
+	 * @return array
+	 */
+	public static function genders(): array {
 		return [
 			"Female" => FEMALE,
 			"Male" => MALE,
@@ -57,7 +102,11 @@ class User extends Model {
 		];
 	}
 
-	public static function validatePassword($password) {
+	/**
+	 * @param string $password
+	 * @return bool
+	 */
+	public static function validatePassword(string $password): bool {
 		$lowercase = preg_match("/[a-z]/", $password);
 		$uppercase = preg_match("/[A-Z]/", $password);
 		$number = preg_match("/[0-9]/", $password);
@@ -68,7 +117,11 @@ class User extends Model {
 	}
 
 	// Object methods
-	public function hasValidPaymentMethod() {
+
+	/**
+	 * @return bool
+	 */
+	public function hasValidPaymentMethod(): bool {
 		switch($this->preferred_payment_method) {
 			case User::PAYMENT_METHOD_BANK_TRANSFER: {
 				if(empty($this->bank_name)) return false;
@@ -84,6 +137,10 @@ class User extends Model {
 		return true;
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function sendActivationEmail() {
 
 		$this->verify_token = bin2hex(random_bytes(16));
@@ -114,6 +171,10 @@ class User extends Model {
 		return true;
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function sendWelcomeEmail() {
 
 		// Content
@@ -140,6 +201,10 @@ class User extends Model {
 
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function sendIDPassEmail() {
 
 		// Content
@@ -175,6 +240,11 @@ class User extends Model {
 
 	}
 
+	/**
+	 * @param string $reason
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function sendIDFailEmail($reason = "No reason given.") {
 
 		// Content
@@ -202,6 +272,10 @@ class User extends Model {
 		return true;
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function sendPasswordResetEmail() {
 
 		$this->reset_token = bin2hex(random_bytes(16));
@@ -235,12 +309,19 @@ class User extends Model {
 
 	}
 
+	/**
+	 * @return bool
+	 */
 	public function canAdmin() {
 		return $this->level >= User::LEVEL_ADMIN;
 	}
 
-	public function getReferrer() {
-		return is_null($this->referrer_id) ? false : User::get($this->referrer_id);
+	/**
+	 * @return User|null
+	 * @throws Exception
+	 */
+	public function getReferrer(): ?User {
+		return is_null($this->referrer_id) ? null : User::get($this->referrer_id);
 	}
 
 	public function getDirectory() {
@@ -295,6 +376,10 @@ class User extends Model {
 		return "Unspecified";
 	}
 
+	/**
+	 * @return int|string
+	 * @throws Exception
+	 */
 	public function getAge() {
 
 		if(is_null($this->date_of_birth)) return __("N/A");
@@ -312,7 +397,12 @@ class User extends Model {
 		return implode(" ", $parts);
 	}
 
-	public function hasPermission($object) {
+	/**
+	 * @param $object
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function hasPermission($object): bool {
 
 		if($this->isAdmin()) return true;
 
@@ -338,6 +428,10 @@ class User extends Model {
 		return false;
 	}
 
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
 	public function deactivate() {
 		// Decline all pending tests
 		$pending_tests = $this->getPendingTests();
@@ -350,7 +444,11 @@ class User extends Model {
 		return $this->commit();
 	}
 
-	public function softDelete() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function softDelete(): bool {
 
 		// Delete identification documents
 		$this->deleteIDFiles();
@@ -378,12 +476,14 @@ class User extends Model {
 		// Set deleted time
 		$this->dts_deleted = Util::getDateTime();
 
-		$success = $this->commit();
-
-		return true;
+		return $this->commit();
 	}
 
-	public function deleteIDFiles() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function deleteIDFiles(): bool {
 
 		$files = UserFile::query([
 			"user_id" => $this->id,
@@ -400,8 +500,11 @@ class User extends Model {
 		return true;
 	}
 
-
-	public function deleteAddresses() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function deleteAddresses(): bool {
 		$addresses = $this->getAddresses();
 		foreach($addresses as $address) {
 			$address->delete();
@@ -410,7 +513,11 @@ class User extends Model {
 		return true;
 	}
 
-	public function getAddresses() {
+	/**
+	 * @return UserAddress[]
+	 * @throws Exception
+	 */
+	public function getAddresses(): array {
 
 		$addresses = UserAddress::query([
 			"user_id" => $this->id,
@@ -421,7 +528,11 @@ class User extends Model {
 		return $addresses;
 	}
 
-	public function getInvoiceAddress() {
+	/**
+	 * @return UserAddress|null
+	 * @throws Exception
+	 */
+	public function getInvoiceAddress(): ?UserAddress {
 		$addresses = UserAddress::query([
 			"user_id" => $this->id,
 		]);
@@ -433,11 +544,12 @@ class User extends Model {
 		// If only one address in account, just return it
 		if(count($addresses) == 1) return array_shift($addresses);
 
-		return false;
+		return null;
 	}
 
 	/**
 	 * @return Customer[]
+	 * @throws Exception
 	 */
 	public function getCustomers() {
 
@@ -455,7 +567,11 @@ class User extends Model {
 		return [];
 	}
 
-	public function getPendingTests() {
+	/**
+	 * @return Test[]
+	 * @throws Exception
+	 */
+	public function getPendingTests(): array {
 		return Test::query([
 			"tester_id" => $this->id,
 			"status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
@@ -465,7 +581,11 @@ class User extends Model {
 		]);
 	}
 
-	public function getCompletedTests() {
+	/**
+	 * @return Test[]
+	 * @throws Exception
+	 */
+	public function getCompletedTests(): array {
 		return Test::query([
 			"tester_id" => $this->id,
 			"status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
@@ -475,17 +595,27 @@ class User extends Model {
 		]);
 	}
 
-	public function getReferrals() {
+	/**
+	 * @return User[]
+	 * @throws Exception
+	 */
+	public function getReferrals(): array {
 		return User::query([
 			"referrer_id" => $this->id
 		]);
 	}
 
-	public function getPreferredPaymentMethodText() {
+	/**
+	 * @return string
+	 */
+	public function getPreferredPaymentMethodText(): string {
 		return User::PAYMENT_METHODS[$this->preferred_payment_method];
 	}
 
-	public function getURL() {
+	/**
+	 * @return string
+	 */
+	public function getURL(): string {
 		return "/account/users/{$this->id}";
 	}
 

+ 33 - 8
engine/user_address.php

@@ -1,24 +1,48 @@
 <?php
 
+/**
+ * Class UserAddress
+ *
+ * @property int $id
+ * @property int $user_id
+ * @property string $name
+ * @property string $address_1
+ * @property string $address_2
+ * @property string $city
+ * @property string $county
+ * @property string $postal_code
+ * @property string $state
+ * @property string $country
+ * @property double $latitude
+ * @property double $longitude
+ * @property float $max_radius_miles
+ * @property bool $invoice
+ */
+
 class UserAddress extends Model {
 
 	protected static $table = "user_address";
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["user_id", "name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "latitude", "longitude", "max_radius_miles", "invoice"];
 
-	protected static $field_map = [
-	];
-
 	const RADIUSES = [
 		"0.25", "0.50", "1.00", "1.50", "2.00", "2.50", "3.00", "4.00", "5.00", "7.50", "10.00", "12.50", "15.00", "20.00", "25.00",
 	];
 
 	// Object methods
-	public function getUser() {
+
+	/**
+	 * @return User|null
+	 * @throws Exception
+	 */
+	public function getUser(): ?User {
 		return User::get($this->user_id);
 	}
 
-	public function getDisplayAddress() {
+	/**
+	 * @return string
+	 */
+	public function getDisplayAddress(): string {
 		$parts = [];
 		$fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"];
 
@@ -29,10 +53,11 @@ class UserAddress extends Model {
 		return implode(", ", array_filter($parts));
 	}
 
-	public function getGeolocation() {
+	/**
+	 * @return array
+	 */
+	public function getGeolocation(): array {
 		return [$this->latitude, $this->longitude];
 	}
 
 }
-
-?>

+ 18 - 4
engine/user_customer.php

@@ -1,5 +1,12 @@
 <?php
 
+/**
+ * Class UserCustomer
+ *
+ * @property int $user_id
+ * @property int $customer_id
+ */
+
 class UserCustomer extends Model {
 
 	protected static $table = "user_customer";
@@ -7,14 +14,21 @@ class UserCustomer extends Model {
 	protected static $fillable_fields = ["user_id", "customer_id"];
 
 	// Object methods
-	public function getUser() {
+
+	/**
+	 * @return User|null
+	 * @throws Exception
+	 */
+	public function getUser(): ?User {
 		return User::get($this->user_id);
 	}
 
-	public function getCustomer() {
+	/**
+	 * @return Customer|null
+	 * @throws Exception
+	 */
+	public function getCustomer(): ?Customer {
 		return Customer::get($this->customer_id);
 	}
 
 }
-
-?>

+ 9 - 4
engine/user_file.php

@@ -1,5 +1,14 @@
 <?php
 
+/**
+ * Class UserFile
+ *
+ * @property int $id
+ * @property int $user_id
+ * @property int $type
+ * @property string $dts_uploaded
+ */
+
 class UserFile extends Model {
 
 	protected static $table = "user_file";
@@ -9,8 +18,4 @@ class UserFile extends Model {
 	const TYPE_ID = 1;
 	const TYPE_PHOTO = 2;
 
-	// Object methods
-
 }
-
-?>

+ 118 - 160
engine/util.php

@@ -1,7 +1,12 @@
 <?php
+
 class Util {
 
-	public static function getGenderText($gender) {
+	/**
+	 * @param string $gender
+	 * @return string
+	 */
+	public static function getGenderText(string $gender): string {
 		switch($gender) {
 			case "M": return MALE;
 			case "F": return FEMALE;
@@ -9,7 +14,11 @@ class Util {
 		}
 	}
 
-	public static function getBooleanText($boolean) {
+	/**
+	 * @param string $boolean
+	 * @return string
+	 */
+	public static function getBooleanText(string $boolean): string {
 		switch($boolean) {
 			case "Y": return YES;
 			case "N": return NO;
@@ -17,12 +26,23 @@ class Util {
 		}
 	}
 
-	public static function convertUTF8($string, $target_encoding = "CP1252") {
+	/**
+	 * @param string $string
+	 * @param string $target_encoding
+	 * @return string
+	 */
+	public static function convertUTF8(string $string, string $target_encoding = "CP1252"): string {
 		return mb_convert_encoding($string, $target_encoding, "UTF-8");
 	}
 
-	public static function getPaginationObject($array, $page_size = 25) {
-		$item_count = count($array);
+	/**
+	 * @param array $items
+	 * @param int $page_size
+	 * @return object
+	 * @throws Exception
+	 */
+	public static function getPaginationObject(array $items, int $page_size = 25) {
+		$item_count = count($items);
 
 		$page = http::value("page");
 		if($page < 1) $page = 1;
@@ -56,12 +76,22 @@ class Util {
 		];
 	}
 
-	public static function getItemDirectory($folder, $id) {
+	/**
+	 * @param string $folder
+	 * @param int $id
+	 * @return string
+	 */
+	public static function getItemDirectory(string $folder, int $id): string {
 		$shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT);
 		return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id;
 	}
 
-	public static function createItemDirectory($folder, $id) {
+	/**
+	 * @param string $folder
+	 * @param int $id
+	 * @return string
+	 */
+	public static function createItemDirectory(string $folder, int $id): string {
 
 		$directory = Util::getItemDirectory($folder, $id);
 
@@ -72,19 +102,32 @@ class Util {
 		return $directory;
 	}
 
-	public static function slug($string) {
+	/**
+	 * @param string $string
+	 * @return string
+	 */
+	public static function slug(string $string): string {
 		$slug = strtolower($string);
 		$slug = preg_replace("/[^0-9a-z]/i", "-", $slug); // Replace anything not 0-9 a-z with dash
-		$slug = preg_replace("/\-+/", "-", $slug); // Replace consequential dashes with dash
+		$slug = preg_replace("/-+/", "-", $slug); // Replace consequential dashes with dash
 		$slug = trim($slug, "-");
 		return $slug;
 	}
 
+	/**
+	 * @param string $string
+	 * @param int $id
+	 * @return string
+	 */
 	public static function slugWithId(string $string, int $id): string {
 		return Util::slug($string) . "-" . $id;
 	}
 
-	public static function getDisplayFileSize($size) {
+	/**
+	 * @param int $size
+	 * @return string
+	 */
+	public static function getDisplayFileSize(int $size): string {
 		if($size < pow(2, 10)) return number_format($size) . " bytes";
 		if($size < pow(2, 20)) return number_format($size / pow(2, 10), 2) . " KB";
 		if($size < pow(2, 30)) return number_format($size / pow(2, 20), 2) . " MB";
@@ -92,7 +135,12 @@ class Util {
 		return number_format($size / pow(2, 40), 2) . " TB";
 	}
 
-	public static function timeDifference($unix_time, $options = []) {
+	/**
+	 * @param int $unix_time
+	 * @param array $options
+	 * @return string
+	 */
+	public static function timeDifference(int $unix_time, $options = []): string {
 
 		$plain = $options["plain"] ?? false;
 
@@ -145,7 +193,11 @@ class Util {
 
 	}
 
-	public static function getTimeZones() {
+	/**
+	 * @return array
+	 * @throws Exception
+	 */
+	public static function getTimeZones(): array {
 
 		// Taken and modified from: https://stackoverflow.com/a/17355238
 
@@ -206,25 +258,46 @@ class Util {
 		return $time_zone_list;
 	}
 
-	public static function getDate($time = false) {
+	/**
+	 * @param bool $time
+	 * @return string
+	 */
+	public static function getDate($time = false): string {
 		if(!$time) $time = time();
 		return gmdate("Y-m-d", $time);
 	}
 
-	public static function getUTCDateTime($time = false) {
+	/**
+	 * @param bool $time
+	 * @return string
+	 */
+	public static function getUTCDateTime($time = false): string {
 		return Util::getDateTime($time);
 	}
 
-	public static function getDateTime($time = false) {
+	/**
+	 * @param bool $time
+	 * @return string
+	 */
+	public static function getDateTime($time = false): string {
 		if(!$time) $time = time();
 		return gmdate("Y-m-d H:i:s", $time);
 	}
 
-	public static function getUnixTimeFromUTC($dts) {
+	/**
+	 * @param string $dts
+	 * @return false|int
+	 */
+	public static function getUnixTimeFromUTC(string $dts) {
 		return strtotime($dts . " UTC");
 	}
 
-	public static function getDurationFromDTS($dts1, $dts2) {
+	/**
+	 * @param string $dts1
+	 * @param string $dts2
+	 * @return float|int
+	 */
+	public static function getDurationFromDTS(string $dts1, string $dts2) {
 		$tz = date_default_timezone_get();
 		date_default_timezone_set("UTC");
 
@@ -236,7 +309,11 @@ class Util {
 		return abs($t2 - $t1);
 	}
 
-	public static function getDisplayDateTime($time = false) {
+	/**
+	 * @param int|bool $time
+	 * @return string
+	 */
+	public static function getDisplayDateTime($time = false): string {
 		if($time === false) $time = time();
 
 		$current_year = date("Y");
@@ -247,7 +324,11 @@ class Util {
 		return date($format, $time);
 	}
 
-	public static function getDisplayDate($time = false) {
+	/**
+	 * @param int|bool $time
+	 * @return string
+	 */
+	public static function getDisplayDate($time = false): string {
 		if($time === false) $time = time();
 
 		$current_year = date("Y");
@@ -258,17 +339,27 @@ class Util {
 		return date($format, $time);
 	}
 
-	public static function getDisplayDateTimeFromUTC($dts) {
+	/**
+	 * @param string $dts
+	 * @return string
+	 */
+	public static function getDisplayDateTimeFromUTC(string $dts): string {
 		$unix = Util::getUnixTimeFromUTC($dts);
 		return Util::getDisplayDateTime($unix);
 	}
 
-	public static function sortObjectArray($array, $field, $compare_function = "") {
+	/**
+	 * @param array $array
+	 * @param string $field
+	 * @param string $compare_function
+	 * @return array
+	 */
+	public static function sortObjectArray(array $array, string $field, string $compare_function = ""): array {
 
 		usort($array, function($a, $b) use ($compare_function, $field) {
 
 			switch($compare_function) {
-				case "":
+				default:
 					if($a->$field == $b->$field) return 0;
 					return ($a->$field < $b->$field) ? -1 : 1;
 				case "strnatcasecmp":
@@ -279,7 +370,11 @@ class Util {
 		return $array;
 	}
 
-	public static function arrayFlipByID($input = []) {
+	/**
+	 * @param array $input
+	 * @return array
+	 */
+	public static function arrayFlipByID(array $input = []): array {
 		$output = [];
 
 		foreach($input as $object) {
@@ -289,141 +384,4 @@ class Util {
 		return $output;
 	}
 
-	public static function formatText($string, $options = []) {
-
-		$bb_code = $options["bb_code"] ?? true;
-		$emoticon = $options["emoticon"] ?? true;
-
-		$string = htmlspecialchars($string);
-		$string = nl2br($string);
-
-		/*
-		if($emoticon && $emoticonresult) {
-			db::data_seek($emoticonresult, 0);
-			while($myrow = db::fetch_array($emoticonresult)) {
-				$replacetext = $myrow["replacetext"];
-				$url = $myrow["url"];
-				$description = $myrow["description"];
-				$string = str_replace("$replacetext", "<img src=\"$dirPrefix$url\" alt=\"$description\" />", $string);
-			}
-		}
-		*/
-
-		if($emoticon) {
-			$string = Emoticon::apply($string);
-		}
-
-		if($bb_code) {
-			$string = Util::BBCode($string);
-		}
-
-		$pattern = "[0-9a-zA-Z\&\/\-\#\%\.\?\=\;\_\:\,\+\(\)\!]";
-
-		$string = preg_replace("/^http(s?):\/\/($pattern*)/", "<a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a> ", $string); // Case for beginning
-		$string = preg_replace("/\shttp(s?):\/\/($pattern*)/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for middle
-		$string = preg_replace("/http(s?):\/\/($pattern*)$/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for end
-
-		return $string;
-	}
-
-	public static function BBCode($string) {
-
-		preg_match_all("/\[quote/i", $string, $matches);
-		$quote_begin_count = count($matches["0"]);
-
-		preg_match_all("/\[\/quote\]/i", $string, $matches);
-		$quote_end_count = count($matches["0"]);
-
-		$quotes = min($quote_begin_count, $quote_end_count);
-
-		$valid_quotes = ($quote_begin_count == $quote_end_count);
-
-		if(!$valid_quotes) {
-		}
-
-		$search = [
-			"/\[b\](.*?)\[\/b\]/is",
-			"/\[i\](.*?)\[\/i\]/is",
-			"/\[u\](.*?)\[\/u\]/is",
-			"/\[s\](.*?)\[\/s\]/is",
-			"/\[spoiler\](.*?)\[\/spoiler\]/is",
-			"/\[img\=(.*?)\]/is",
-			"/\[img\](.*?)\[\/img\]/is",
-			"/\[img width\=(.*?)\](.*?)\[\/img\]/is",
-			"/\[url\](.*?)\[\/url\]/is",
-			"/\[url\=(.*?)\](.*?)\[\/url\]/is",
-			"/\[color\=(.*?)\](.*?)\[\/color\]/is",
-			"/\[colour\=(.*?)\](.*?)\[\/colour\]/is",
-			"/\[size\=(.*?)\](.*?)\[\/size\]/is",
-			"/\[font\=(.*?)\](.*?)\[\/font\]/is",
-			"/\[center\](.*?)\[\/center\]<br \/>/is",
-			"/\[center\](.*?)\[\/center\]/is",
-			"/\[code\](.*?)\[\/code\]/is",
-			"/\[background\=(.*?)\](.*?)\[\/background\]/is",
-			"/\[youtube\][^\s]+v\=([A-Za-z0-9\-_]+)[^\s]*\[\/youtube\]/is", // YouTube URL
-			"/\[youtube\]([A-Za-z0-9\-_]+?)\[\/youtube\]/is", // YouTube ID
-			"/\[twitch\]([A-Za-z0-9\-_]+?)\[\/twitch\]/is", // Twitch ID
-			"/\[hrd?\]\<br \/\>/is",
-			"/\[nico\](.*?)\[\/nico\]/is",
-			"/\[leftfloat\](.*?)\[\/leftfloat\]/is",
-			"/\[rightfloat\](.*?)\[\/rightfloat\]/is",
-			"/\[leftfloat width\=(.*?)\](.*?)\[\/leftfloat\]/is",
-			"/\[clear\]/is",
-		];
-
-		$replace = [
-			'<strong>$1</strong>',
-			'<em>$1</em>',
-			'<u>$1</u>',
-			'<del>$1</del>',
-			'<span class="spoiler">$1</span>',
-			'<img class="img-fluid" src="$1" alt="$1" />',
-			'<img class="img-fluid" src="$1" alt="$1" />',
-			'<img class="img-fluid" width="$1" src="$2" alt="$2" />',
-			'<a href="$1" target="_blank">$1</a>',
-			'<a href="$1" target="_blank">$2</a>',
-			'<span style="color: $1;">$2</span>',
-			'<span style="color: $1;">$2</span>',
-			'<span style="font-size: $1'.'px;">$2</span>',
-			'<span style="font-family: $1;">$2</span>',
-			'<div class="text-center">$1</div>',
-			'<div class="text-center">$1</div>',
-			'<div class="code">$1</div>',
-			'<span style="background: $1;">$2</span>',
-			'<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
-			'<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
-			'<object type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="https://www.twitch.tv/widgets/live_embed_player.swf?channel=$1" bgcolor="#000000">
-<param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=www.twitch.tv&channel=$1&auto_play=false&start_volume=25" /></object>',
-			'<hr />',
-			'<script type="text/javascript" src="https://ext.nicovideo.jp/thumb_watch/$1"></script>',
-			'<div class="float-left">$1</div>',
-			'<div class="float-right">$1</div>',
-			'<div class="float-left" style="width: $1%;">$2</div>',
-			'<div class="float-none"></div>',
-		];
-
-		$string = preg_replace($search, $replace, $string);
-
-		if($valid_quotes) {
-			$search = [
-				"/\[quote\]/is",
-				"/\[quote\=(.*?)\]([^\]])/is",
-				"/\[quote\=(.*?)\]/is",
-				"/\[\/quote\]<br \/>/is",
-				"/\[\/quote\]/is",
-			];
-
-			$replace = [
-				'<div class="card quote mb-3"><div class="card-header">Quote</div><div class="card-body">',
-				'<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">$2',
-				'<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">',
-				'</div></div>',
-				'</div></div>',
-			];
-
-			$string = preg_replace($search, $replace, $string, $quotes);
-		}
-
-		return $string;
-	}
 }

+ 13 - 1
engine/venue_type.php

@@ -1,5 +1,12 @@
 <?php
 
+/**
+ * Class VenueType
+ *
+ * @property int $id
+ * @property string $name
+ */
+
 class VenueType extends Model {
 
 	protected static $table = "venue_type";
@@ -7,7 +14,12 @@ class VenueType extends Model {
 	protected static $fillable_fields = ["name"];
 
 	// Static methods
-	public static function getAll() {
+
+	/**
+	 * @return VenueType[]
+	 * @throws Exception
+	 */
+	public static function getAll(): array {
 		return self::query([], ["sort" => "name"]);
 	}
 

+ 14 - 3
engine/view.php

@@ -4,11 +4,17 @@ class View {
 
 	protected static $included = [];
 
-	public static function include($view, $params = []) {
+	/**
+	 * @param string $view
+	 * @param array $params
+	 * @throws Exception
+	 */
+	public static function include(string $view, array $params = []) {
 		$view_path = ROOT_DIR . "/views/" . $view . ".php";
 		if(file_exists($view_path)) {
 			self::$included[] = $view;
 			extract($params);
+			/** @noinspection PhpIncludeInspection */
 			require $view_path;
 		}
 		else {
@@ -16,8 +22,13 @@ class View {
 		}
 	}
 
-	public static function includeOnce($view, $params = []) {
+	/**
+	 * @param string $view
+	 * @param array $params
+	 * @throws Exception
+	 */
+	public static function includeOnce(string $view, array $params = []) {
 		if(in_array($view, self::$included)) return;
 		self::include($view, $params);
 	}
-}
+}