فهرست منبع

Cleanup files and add PHPDocs

Alan Lam 5 سال پیش
والد
کامیت
dea7436aff
3فایلهای تغییر یافته به همراه34 افزوده شده و 31 حذف شده
  1. 5 1
      controllers/project_invoice.php
  2. 3 7
      engine/invoice.php
  3. 26 23
      engine/project.php

+ 5 - 1
controllers/project_invoice.php

@@ -1,4 +1,8 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $id
+ */
 
 use \Mpdf\Mpdf;
 

+ 3 - 7
engine/invoice.php

@@ -1,20 +1,16 @@
 <?php
 
-/**
- * @property InvoiceLineItem[] $lineItems
- */
-
 class Invoice {
 
     private $lineItems = [];
     private $taxRate = 0.2;
     private $reference = "";
 
-    public function addLineItem(InvoiceLineItem $item) {
+    public function addLineItem(InvoiceLineItem $item): void {
         $this->lineItems[] = $item;
     }
 
-    public function addLineItems(array $items) {
+    public function addLineItems(array $items): void {
         foreach($items as $item) {
             $this->addLineItem($item);
         }
@@ -47,7 +43,7 @@ class Invoice {
         return round($this->getTotal() * $this->taxRate, 2);
     }
 
-    public function getTotalIncTax() {
+    public function getTotalIncTax(): float {
         return $this->getTotal() + $this->getTax();
     }
 

+ 26 - 23
engine/project.php

@@ -11,7 +11,7 @@
  * @property int $max_age
  * @property float $default_customer_fee
  * @property float $default_tester_fee
- * @property int $time_of_day
+ * @property string $time_of_day
  * @property bool $tester_can_claim
  * @property string $template_overrides_json
  * @property string $dts_start
@@ -25,9 +25,9 @@
 
 class Project extends Model {
 
-	const TOD_ANYTIME = "A";
-	const TOD_DAYTIME = "D";
-	const TOD_NIGHTTIME = "N";
+	public const TOD_ANYTIME = "A";
+	public const TOD_DAYTIME = "D";
+	public const TOD_NIGHTTIME = "N";
 
 	protected static $table = "project";
 	protected static $id_field = "id";
@@ -36,16 +36,16 @@ class Project extends Model {
 	/**
 	 * @var Test[] $tests
 	 */
-	protected $tests = null;
+	protected $tests;
 
 	// Static methods
 
 	/**
 	 * @param string $processing_file
-	 * @param array $projects
+	 * @param Project[] $projects
 	 * @throws Exception
 	 */
-	public static function exportCSV(string $processing_file, /** @noinspection PhpUnusedParameterInspection */ $projects = []) {
+	public static function exportCSV(string $processing_file, /** @noinspection PhpUnusedParameterInspection */ array $projects = []): void {
 
 		// Get processing file
 		$processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
@@ -54,7 +54,7 @@ class Project extends Model {
 		// Output
 		HTTP::setDownloadHeaders("export.csv");
 
-		$handle = fopen("php://output", "w");
+		$handle = fopen("php://output", "wb");
 		require $processing_file_path;
 		fclose($handle);
 	}
@@ -88,7 +88,7 @@ class Project extends Model {
 	 * @return Customer
 	 * @throws Exception
 	 */
-	public function getCustomer(): ?Customer {
+	public function getCustomer(): Customer {
 		return Customer::get($this->customer_id);
 	}
 
@@ -168,7 +168,7 @@ class Project extends Model {
 		$pass_count = $this->getPassCount();
 		$fail_count = $this->getFailCount();
 		$pass_fail_count = $pass_count + $fail_count;
-		if($pass_fail_count == 0) return "0%";
+		if($pass_fail_count === 0) return "0%";
 
 		return (number_format($pass_count / $pass_fail_count, 4) * 100) . "%";
 	}
@@ -190,10 +190,12 @@ class Project extends Model {
 		if($this->min_age && $this->max_age) {
 			return $this->min_age . " to " . $this->max_age;
 		}
-		else if($this->min_age) {
+
+		if($this->min_age) {
 			return "At least " . $this->min_age;
 		}
-		else if($this->max_age) {
+
+		if($this->max_age) {
 			return "Up to " . $this->max_age;
 		}
 
@@ -225,8 +227,8 @@ class Project extends Model {
 		$test_count = $this->test_count;
 
 		$tests = $this->getTests();
-		$completed_tests = array_filter($tests, function($test) {
-			return $test->status == Test::STATUS_COMPLETED;
+		$completed_tests = array_filter($tests, static function($test) {
+			return (int) $test->status === Test::STATUS_COMPLETED;
 		});
 		$completed_test_count = count($completed_tests);
 
@@ -238,7 +240,7 @@ class Project extends Model {
 	 * @return Template
 	 * @throws Exception
 	 */
-	public function getTemplate(): ?Template {
+	public function getTemplate(): Template {
 		return Template::get($this->template_id);
 	}
 
@@ -246,8 +248,7 @@ class Project extends Model {
 	 * @return array
 	 */
 	public function getTemplateOverrides(): array {
-		$overrides = json_decode($this->template_overrides_json) ?: [];
-		return $overrides;
+		return json_decode($this->template_overrides_json) ?: [];
 	}
 
 	/**
@@ -286,7 +287,9 @@ class Project extends Model {
 	 */
 	public function getDirectory(): string {
 		$directory = ROOT_DIR . "/files/projects/" . $this->id;
-		if(!file_exists($directory)) mkdir($directory, 0777, true);
+		if(!file_exists($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) {
+			throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));
+		}
 		return $directory;
 	}
 
@@ -302,21 +305,21 @@ class Project extends Model {
 	 * @return bool
 	 */
 	public function isAnytime(): bool {
-		return $this->time_of_day == self::TOD_ANYTIME;
+		return $this->time_of_day === self::TOD_ANYTIME;
 	}
 
 	/**
 	 * @return bool
 	 */
 	public function isDaytime(): bool {
-		return $this->time_of_day == self::TOD_DAYTIME;
+		return $this->time_of_day === self::TOD_DAYTIME;
 	}
 
 	/**
 	 * @return bool
 	 */
 	public function isNighttime(): bool {
-		return $this->time_of_day == self::TOD_NIGHTTIME;
+		return $this->time_of_day === self::TOD_NIGHTTIME;
 	}
 
 	/**
@@ -493,7 +496,7 @@ class Project extends Model {
 
 			foreach($invoice_fields as $field) {
 				$item = $items[$field->name] ?? (new InvoiceLineItem($field->label));
-				$item->addPrice((float) $values[$field->name] ?? 0);
+				$item->addPrice((float) ($values[$field->name] ?? 0));
 				$items[$field->name] = $item;
 			}
 		}
@@ -501,7 +504,7 @@ class Project extends Model {
 		return $items;
 	}
 
-	public function setTemplateKeys($keys) {
+	public function setTemplateKeys($keys): void {
 		if(!is_array($keys)) $keys = [];
 		$this->template_keys = json_encode($keys);
 	}