Skip to content

Commit

Permalink
Refactor code to resolve phpstan warnings (#398)
Browse files Browse the repository at this point in the history
- Updated existing code using phpstan suggestions
    - added missing code where needed
    - deleted unused code/comments
  • Loading branch information
beesaferoot authored Dec 9, 2024
1 parent 67cfe1f commit 62ad09d
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/backend/app/Console/Commands/AddPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function runInCompanyScope(): void {
$composer_name = $this->argument('composer_name');
$description = $this->argument('description');

$this->pluginService->addPlugin($name, $composer_name, $description);
$this->pluginsService->addPlugin($name, $composer_name, $description);
}
}
5 changes: 3 additions & 2 deletions src/backend/app/Console/Commands/DemoDataCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function generateTransaction(): void {
'created_at' => $demoDate,
'updated_at' => $demoDate,
]);
$subTransaction = null;

$manufacturerTransaction = $this->calinTransaction->newQuery()->create([]);

Expand Down Expand Up @@ -233,7 +234,7 @@ private function generateTransaction(): void {
// create an object for the token job
$transactionData = \App\Misc\TransactionDataContainer::initialize($transaction);
} catch (\Exception $exception) {
event('transaction.failed', [$this->transaction, $e->getMessage()]);
event('transaction.failed', [$this->transaction, $exception->getMessage()]);
throw $exception;
}

Expand All @@ -256,7 +257,7 @@ private function generateTransaction(): void {
'token' => Str::random(30),
'load' => round(
$transactionData->transaction->amount /
$randomMeter['tariff']['price'],
$randomMeter['tariff']['price'],
2
),
];
Expand Down
2 changes: 0 additions & 2 deletions src/backend/app/Console/Commands/ModelShowTenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function __construct(

/**
* Execute the console command.
*
* @return int
*/
public function handle() {
$this->databaseProxyManagerService->buildDatabaseConnectionDummyCompany();
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Console;

use app\Console\Commands\MailApplianceDebtsCommand;
use App\Console\Commands\MailApplianceDebtsCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Inensus\AngazaSHS\Console\Commands\InstallPackage as InstallAngazaSHSPackage;
Expand Down
1 change: 0 additions & 1 deletion src/backend/app/Http/Middleware/JwtMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class JwtMiddleware extends BaseMiddleware {
*/
public function handle(Request $request, \Closure $next, string $type = 'user'): mixed {
try {
// @phpstan-ignore-next-line as no methods are defined on the facade its failing to resolve the function
$id = JWTAuth::parseToken()->getPayload()->get('sub');
if ($type === 'agent') {
$user = Agent::query()->findOrFail($id);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/Jobs/SmsLoadBalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function () {
Log::debug('smsgateway', ['data' => $this->smsBody, 'firebase' => $fireBaseResult]);
},
function () {
return $this->release(1);
$this->release(1);
}
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace App\Models;

use App\Models\Base\BaseModel;

class Admin extends BaseModel {}
8 changes: 4 additions & 4 deletions src/backend/app/Observers/AgentReceiptObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
public function created(AgentReceipt $receipt): void {
$agentId = $receipt->agent_id;
$agent = $this->agentService->getById($agentId);
$due = $agent->due_to_energy_supplier;
$due = $agent->due_to_energy_supplier ?? 0;
$sinceLastVisit = 0;
$lastReceipt = $this->agentReceiptService->getLastReceipt($agentId);

Expand All @@ -41,11 +41,11 @@ public function created(AgentReceipt $receipt): void {
$collected = $receipt->amount;
$agentReceiptDetailData = [
'agent_receipt_id' => $receipt->id,
'due' => $due ?? 0,
'collected' => $collected ?? 0,
'due' => $due,
'collected' => $collected,
'since_last_visit' => $sinceLastVisit ?? 0,
'earlier' => $earlier ?? 0,
'summary' => $summary ?? 0,
'summary' => $summary < 0 ? 0 : $summary,
];
$this->agentReceiptDetailService->create($agentReceiptDetailData);
$agentBalanceHistoryData = [
Expand Down
22 changes: 9 additions & 13 deletions src/backend/app/Services/AgentBalanceHistoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function save($agentBalanceHistory): bool {
}

public function getLastAgentBalanceHistory($agentId) {
return $this->agentBalanceHistory->newQuery()->where('agent_id', $agentId)->get()->last();
return $this->agentBalanceHistory->newQuery()->where('agent_id', $agentId)->latest('id')->first();
}

public function getTotalAmountSinceLastVisit($agentBalanceHistoryId, $agentId) {
Expand All @@ -59,21 +59,17 @@ public function getTotalAmountSinceLastVisit($agentBalanceHistoryId, $agentId) {
}

public function getTransactionAverage($agent, $lastReceipt) {
$query = $this->agentBalanceHistory->newQuery()
->where('agent_id', $agent->id)
->where('trigger_type', 'agent_transaction');

if ($lastReceipt) {
$averageTransactionAmounts = $this->agentBalanceHistory->newQuery()
->where('agent_id', $agent->id)
->where('trigger_type', 'agent_transaction')
->where('created_at', '>', $lastReceipt->created_at)
->get()
->avg('amount');
} else {
$averageTransactionAmounts = $this->agentBalanceHistory->newQuery()
->where('agent_id', $agent->id)
->where('trigger_type', 'agent_transaction')
->get()
->avg('amount');
$query->where('created_at', '>', $lastReceipt->created_at);
}

// Use avg directly on the query
$averageTransactionAmounts = $query->avg('amount');

return -1 * $averageTransactionAmounts;
}

Expand Down
4 changes: 3 additions & 1 deletion src/backend/app/Services/AgentReceiptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function getLastReceipt($agentId) {
}

public function getLastReceiptDate($agent) {
$lastReceiptDate = $this->agentReceipt->newQuery()->where('agent_id', $agent->id)->get()->last();
$lastReceiptDate = $this->agentReceipt->newQuery()->where('agent_id', $agent->id)
->lastest('created_at')
->first();

if ($lastReceiptDate) {
return $lastReceiptDate->created_at;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/Services/AgentTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function ($q) use ($agentId) {

public function getById(int $agentId, ?int $customerId = null): AgentTransaction {
$customerDeviceSerials = $this->device->newQuery()->where('person_id', $customerId)
->get()->pluck('device_serial');
->pluck('device_serial');

if (!$customerDeviceSerials->count()) {
return null;
Expand Down
10 changes: 10 additions & 0 deletions src/backend/app/Services/PluginsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function isPluginActive(int $pluginId): bool {
->where('mpm_plugin_id', '=', $pluginId)
->exists();
}

public function addPlugin(string $name, string $composerName, string $description): Plugins {
$pluginData = [
'name' => $name,
'composer_name' => $composerName,
'description' => $description,
];

return $this->create($pluginData);
}
}

0 comments on commit 62ad09d

Please sign in to comment.