From df49ab9aec705dcc3f1b87b79db5697043a760f3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 Apr 2023 19:25:33 +1000 Subject: [PATCH] Fixes for static analysis --- app/Helpers/Invoice/InvoiceItemSum.php | 4 +- app/Helpers/Invoice/InvoiceSum.php | 52 ++++++++++++--------- app/Helpers/Invoice/InvoiceSumInclusive.php | 2 +- app/Models/Credit.php | 21 ++++----- app/Models/Invoice.php | 8 ++-- app/Models/PurchaseOrder.php | 11 ++--- app/Models/Quote.php | 33 +++++-------- app/Models/RecurringInvoice.php | 2 +- app/Models/RecurringQuote.php | 2 +- app/Utils/Number.php | 2 +- 10 files changed, 64 insertions(+), 73 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 6c6846470378..3b55c8113817 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -110,7 +110,7 @@ class InvoiceItemSum public function process() { - if (! $this->invoice->line_items || ! isset($this->invoice->line_items) || ! is_array($this->invoice->line_items) || count($this->invoice->line_items) == 0) { + if (! $this->invoice->line_items || ! is_array($this->invoice->line_items) || count($this->invoice->line_items) == 0) { $this->items = []; return $this; @@ -159,7 +159,7 @@ class InvoiceItemSum return $this; } - private function push() + private function push(): self { $this->sub_total += $this->getLineTotal(); diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index 09d181fe198a..9bf0b7e7cb47 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -11,9 +11,14 @@ namespace App\Helpers\Invoice; +use App\Models\Quote; +use App\Utils\Number; +use App\Models\Credit; use App\Models\Invoice; -use App\Utils\Traits\NumberFormatter; +use App\Models\PurchaseOrder; +use App\Models\RecurringInvoice; use Illuminate\Support\Collection; +use App\Utils\Traits\NumberFormatter; class InvoiceSum { @@ -22,7 +27,7 @@ class InvoiceSum use Discounter; use NumberFormatter; - protected $invoice; + protected RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder $invoice; public $tax_map; @@ -78,7 +83,7 @@ class InvoiceSum return $this; } - private function calculateLineItems() + private function calculateLineItems(): self { $this->invoice_items = new InvoiceItemSum($this->invoice); $this->invoice_items->process(); @@ -90,7 +95,7 @@ class InvoiceSum return $this; } - private function calculateDiscount() + private function calculateDiscount(): self { $this->total_discount = $this->discount($this->invoice_items->getSubTotal()); @@ -99,7 +104,7 @@ class InvoiceSum return $this; } - private function calculateCustomValues() + private function calculateCustomValues(): self { $this->total_custom_values += $this->valuer($this->invoice->custom_surcharge1); @@ -114,7 +119,7 @@ class InvoiceSum return $this; } - private function calculateInvoiceTaxes() + private function calculateInvoiceTaxes(): self { if (is_string($this->invoice->tax_name1) && strlen($this->invoice->tax_name1) > 1) { $tax = $this->taxer($this->total, $this->invoice->tax_rate1); @@ -148,23 +153,24 @@ class InvoiceSum * * @return self The balance. */ - private function calculateBalance() + private function calculateBalance(): self { $this->setCalculatedAttributes(); return $this; } - private function calculatePartial() + private function calculatePartial(): self { if (! isset($this->invoice->id) && isset($this->invoice->partial)) { - $this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance)); + $this->invoice->partial = max(0, min(Number::roundValue($this->invoice->partial, 2), $this->invoice->balance)); +// $this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance)); } return $this; } - private function calculateTotals() + private function calculateTotals(): self { $this->total += $this->total_taxes; @@ -230,7 +236,7 @@ class InvoiceSum * Build $this->invoice variables after * calculations have been performed. */ - private function setCalculatedAttributes() + private function setCalculatedAttributes(): self { /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ @@ -238,9 +244,9 @@ class InvoiceSum if ($this->invoice->amount != $this->invoice->balance) { $paid_to_date = $this->invoice->amount - $this->invoice->balance; - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision) - $paid_to_date; + $this->invoice->balance = Number::roundValue($this->getTotal(), $this->precision) - $paid_to_date; } else { - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision); + $this->invoice->balance = Number::roundValue($this->getTotal(), $this->precision); } } /* Set new calculated total */ @@ -268,7 +274,7 @@ class InvoiceSum return $this->gross_sub_total; } - public function setGrossSubTotal($value) + public function setGrossSubTotal($value): self { $this->gross_sub_total = $value; @@ -300,7 +306,7 @@ class InvoiceSum return $this->total_custom_values; } - public function setTaxMap() + public function setTaxMap(): self { if ($this->invoice->is_amount_discount == true) { $this->invoice_items->calcTaxesWithAmountDiscount(); @@ -369,18 +375,18 @@ class InvoiceSum return $this->getTotalTaxes(); } - public function purgeTaxes() + public function purgeTaxes(): self { - $this->tax_rate1 = 0; - $this->tax_name1 = ''; + // $this->tax_rate1 = 0; + // $this->tax_name1 = ''; - $this->tax_rate2 = 0; - $this->tax_name2 = ''; + // $this->tax_rate2 = 0; + // $this->tax_name2 = ''; - $this->tax_rate3 = 0; - $this->tax_name3 = ''; + // $this->tax_rate3 = 0; + // $this->tax_name3 = ''; - $this->discount = 0; + // $this->discount = 0; $line_items = collect($this->invoice->line_items); diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 36a5eefba033..2222d1b1137d 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -42,7 +42,7 @@ class InvoiceSumInclusive private $precision; - private InvoiceItemSumInclusive $invoice_items; + public InvoiceItemSumInclusive $invoice_items; /** * Constructs the object with Invoice and Settings object. * diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 5d48faa809c0..5dd699799426 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -49,7 +49,7 @@ use Laracasts\Presenter\PresentableTrait; * @property string|null $last_sent_date * @property string|null $due_date * @property int $is_deleted - * @property object|null $line_items + * @property array|null $line_items * @property object|null $backup * @property string|null $footer * @property string|null $public_notes @@ -76,10 +76,10 @@ use Laracasts\Presenter\PresentableTrait; * @property int $custom_surcharge_tax2 * @property int $custom_surcharge_tax3 * @property int $custom_surcharge_tax4 - * @property string $exchange_rate - * @property string $amount - * @property string $balance - * @property string|null $partial + * @property float $exchange_rate + * @property float $amount + * @property float $balance + * @property float|null $partial * @property string|null $partial_due_date * @property string|null $last_viewed * @property int|null $created_at @@ -450,7 +450,7 @@ class Credit extends BaseModel /** * Access the invoice calculator object. * - * @return stdClass The invoice calculator object getters + * @return \stdClass The invoice calculator object getters */ public function calc() { @@ -524,6 +524,8 @@ class Credit extends BaseModel return Storage::disk(config('filesystems.default'))->{$type}($file_path); } + $file_exists = false; + try { $file_exists = Storage::disk(config('filesystems.default'))->exists($file_path); } catch (\Exception $e) { @@ -576,19 +578,14 @@ class Credit extends BaseModel switch ($status) { case self::STATUS_DRAFT: return ctrans('texts.draft'); - break; case self::STATUS_SENT: return ctrans('texts.sent'); - break; case self::STATUS_PARTIAL: return ctrans('texts.partial'); - break; case self::STATUS_APPLIED: return ctrans('texts.applied'); - break; default: - return ''; - break; + return ctrans('texts.sent'); } } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d97b29ade7e6..9af728560543 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -78,10 +78,10 @@ use Laracasts\Presenter\PresentableTrait; * @property int $custom_surcharge_tax2 * @property int $custom_surcharge_tax3 * @property int $custom_surcharge_tax4 - * @property string $exchange_rate - * @property string $amount - * @property string $balance - * @property string|null $partial + * @property float $exchange_rate + * @property float $amount + * @property float $balance + * @property float|null $partial * @property string|null $partial_due_date * @property string|null $last_viewed * @property int|null $created_at diff --git a/app/Models/PurchaseOrder.php b/app/Models/PurchaseOrder.php index 17ce85cfeced..11c5b1bce146 100644 --- a/app/Models/PurchaseOrder.php +++ b/app/Models/PurchaseOrder.php @@ -76,7 +76,7 @@ use Illuminate\Support\Facades\Storage; * @property int $custom_surcharge_tax4 * @property string $exchange_rate * @property string $balance - * @property string|null $partial + * @property float|null $partial * @property string $amount * @property string $paid_to_date * @property string|null $partial_due_date @@ -335,18 +335,15 @@ class PurchaseOrder extends BaseModel switch ($status) { case self::STATUS_DRAFT: return ctrans('texts.draft'); - break; case self::STATUS_SENT: return ctrans('texts.sent'); - break; case self::STATUS_ACCEPTED: return ctrans('texts.accepted'); - break; case self::STATUS_CANCELLED: return ctrans('texts.cancelled'); - break; - // code... - break; + default: + return ctrans('texts.sent'); + } } diff --git a/app/Models/Quote.php b/app/Models/Quote.php index b04a848bcaf0..4cb81f8b7e41 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -75,10 +75,10 @@ use Laracasts\Presenter\PresentableTrait; * @property int $custom_surcharge_tax2 * @property int $custom_surcharge_tax3 * @property int $custom_surcharge_tax4 - * @property string $exchange_rate - * @property string $amount - * @property string $balance - * @property string|null $partial + * @property float $exchange_rate + * @property float $amount + * @property float $balance + * @property float|null $partial * @property string|null $partial_due_date * @property string|null $last_viewed * @property int|null $created_at @@ -393,7 +393,7 @@ class Quote extends BaseModel /** * Access the quote calculator object. * - * @return stdClass The quote calculator object getters + * @return \stdClass The quote calculator object getters */ public function calc() { @@ -450,6 +450,8 @@ class Quote extends BaseModel return Storage::disk(config('filesystems.default'))->{$type}($file_path); } + $file_exists = false; + try { $file_exists = Storage::disk(config('filesystems.default'))->exists($file_path); } catch (\Exception $e) { @@ -473,51 +475,40 @@ class Quote extends BaseModel * @param int $status * @return string */ - public static function badgeForStatus(int $status) + public static function badgeForStatus(int $status): string { switch ($status) { case self::STATUS_DRAFT: return '
'.ctrans('texts.draft').'
'; - break; case self::STATUS_SENT: return '
'.ctrans('texts.pending').'
'; - break; case self::STATUS_APPROVED: return '
'.ctrans('texts.approved').'
'; - break; case self::STATUS_EXPIRED: return '
'.ctrans('texts.expired').'
'; - break; case self::STATUS_CONVERTED: return '
'.ctrans('texts.converted').'
'; - break; default: - // code... - break; + return '
'.ctrans('texts.draft').'
'; } } - public static function stringStatus(int $status) + public static function stringStatus(int $status): string { switch ($status) { case self::STATUS_DRAFT: return ctrans('texts.draft'); - break; case self::STATUS_SENT: return ctrans('texts.pending'); - break; case self::STATUS_APPROVED: return ctrans('texts.approved'); - break; case self::STATUS_EXPIRED: return ctrans('texts.expired'); - break; case self::STATUS_CONVERTED: return ctrans('texts.converted'); - break; default: - // code... - break; + return ctrans('texts.draft'); + } } diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 67286724cbdd..0067e1d09c66 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -59,7 +59,7 @@ use Laracasts\Presenter\PresentableTrait; * @property string|null $custom_value4 * @property string $amount * @property string $balance - * @property string|null $partial + * @property float|null $partial * @property string|null $last_viewed * @property int $frequency_id * @property string|null $last_sent_date diff --git a/app/Models/RecurringQuote.php b/app/Models/RecurringQuote.php index dcb68027f0c2..3df3e46d1b86 100644 --- a/app/Models/RecurringQuote.php +++ b/app/Models/RecurringQuote.php @@ -80,7 +80,7 @@ use Laracasts\Presenter\PresentableTrait; * @property int $custom_surcharge_tax4 * @property string|null $due_date_days * @property string $exchange_rate - * @property string|null $partial + * @property float|null $partial * @property string|null $partial_due_date * @property int|null $subscription_id * @property int $uses_inclusive_taxes diff --git a/app/Utils/Number.php b/app/Utils/Number.php index d53e335f98ab..2c6fdf0e7ea7 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -129,7 +129,7 @@ class Number /** * Formats a given value based on the clients currency AND country. * - * @param floatval $value The number to be formatted + * @param $value The number to be formatted * @param $entity * @return string The formatted value */