Skip to content

Commit

Permalink
(PhpStan) fix analyzer level = 1 issues (#14)
Browse files Browse the repository at this point in the history
* (PhpStan) fix analyzer level = 1 issues

* (Readability) rename classes according to what they are doing

* (PhpStan) phpstan adjustments

* (CodeQuality) imrpove quality by return types and improve readability.

* (CodeQuality) php code sniffer not allowed to fail
  • Loading branch information
alchalade authored Dec 12, 2023
1 parent 5507467 commit 3354585
Show file tree
Hide file tree
Showing 121 changed files with 601 additions and 929 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/php_linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ jobs:
working-directory: ./Website/htdocs/mpmanager

- name: Run Code sniffer
run: ./vendor/bin/phpcbf --standard=PSR12 --tab-width=4 app/
run: ./vendor/bin/phpcs --standard=PSR12 --tab-width=4 --exclude=Generic.Files.LineLength app/
working-directory: ./Website/htdocs/mpmanager
continue-on-error: true

- name: Run PHPStan
run: ./vendor/bin/phpstan analyze app/Http app/Services --memory-limit=4G --level=1
run: ./vendor/bin/phpstan analyze --error-format=table app/Http app/Services app/Models/ app/Events app/Listeners app/Misc app/modules --memory-limit=2G --level=1
working-directory: ./Website/htdocs/mpmanager
continue-on-error: true
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ private function runForAllShards(
$input,
$output
) {
$this->runForCompany($databaseProxyManagerService, $companyDatabase->getCompanyId(), $input,
$output);
$this->runForCompany(
$databaseProxyManagerService,
$companyDatabase->getCompanyId(),
$input,
$output
);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@

class CreateDummyCompanyWithData extends Command
{
const SQL_DUMMY_DATA_FILE_NAME = 'dummy_data.sql';
const DUMMY_COMPANY_DATA = [
public const SQL_DUMMY_DATA_FILE_NAME = 'dummy_data.sql';
public const DUMMY_COMPANY_DATA = [
'name' => 'Dummy Company',
'address' => 'Dummy Address',
'phone' => '+255123456789',
'country_id' => -1,
'email' => '[email protected]',
'protected_page_password' => '123123'
];
const DUMMY_COMPANY_USER = [
public const DUMMY_COMPANY_USER = [
'password' => '123123',
'email' => '[email protected]',
'name' => 'Dummy User',
];
const DUMMY_DATABASE_NAME = 'DummyCompany_1';
public const DUMMY_DATABASE_NAME = 'DummyCompany_1';

protected $signature = 'dummy:create-company-with-dummy-data';
protected $description = 'Create a dummy company with dummy data for development environment';
Expand Down Expand Up @@ -68,8 +68,10 @@ public function handle()
];

DatabaseProxy::query()->firstOrCreate($databaseProxyData, $databaseProxyData);
$this->databaseProxyManagerService->runForCompany($company->getId(),
fn() => $this->importSqlDump($path, $databaseName));
$this->databaseProxyManagerService->runForCompany(
$company->getId(),
fn() => $this->importSqlDump($path, $databaseName)
);

return 0;
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
use App\Models\Transaction\AirtelTransaction;
use App\Models\Transaction\VodacomTransaction;


class DummyDataCreator extends AbstractSharedCommand
{
const DEMO_COMPANY_ID = 11;
public const DEMO_COMPANY_ID = 11;

protected $signature = 'dummy:create-data {amount} {--company-id=} {--type=}';
protected $description = 'creates dummy data for demo company';
Expand Down Expand Up @@ -60,7 +59,6 @@ public function __construct(
private Ticket $ticket,
private MaintenanceUsers $maintenanceUsers,
private TicketOutsource $ticketOutsource

) {
parent::__construct();
}
Expand Down Expand Up @@ -92,7 +90,6 @@ public function handle()
}
DB::connection('shard')->commit();
} catch (\Exception $e) {

DB::connection('shard')->rollBack();
echo $e->getMessage();
}
Expand Down Expand Up @@ -270,9 +267,11 @@ private function generateTransaction(): void
if ($transactionData->transaction->amount > 0) {
$token = $this->token->newQuery()->create([
'token' => Str::random(30),
'energy' => round($transactionData->transaction->amount /
'energy' => round(
$transactionData->transaction->amount /
($randomMeter['meterParameter']['tariff']['price']),
2),
2
),
'created_at' => $dummyDate,
'updated_at' => $dummyDate,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(private CustomerDatabaseParser $customerDatabasePars
public function handle(): void
{
$this->info('Command started');
Log::info('command running w '. $this->option('company-id'));
Log::info('command running w ' . $this->option('company-id'));
$result = $this->customerDatabaseParser->insertFromCsv();
foreach ($result as $key => $value) {
$this->info($key . ' : ' . $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function handle()
try {
$seederName = $this->argument('seederName');
Artisan::call('db:seed', ['--force' => true, '--class' => $seederName]);

} catch (\Throwable $t) {
$this->info("failed seeding " . $t->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Models\SolarHomeSystem;
use Illuminate\Support\Facades\DB;


class ShiftEcosysSHSdataForDeviceFeature extends AbstractSharedCommand
{
//--company-id=36
Expand All @@ -30,8 +29,8 @@ class ShiftEcosysSHSdataForDeviceFeature extends AbstractSharedCommand
3 => 2
];

public function __construct(
) {
public function __construct()
{
parent::__construct();
}

Expand Down Expand Up @@ -69,15 +68,12 @@ public function handle()
DB::connection('shard')->table('meters')->truncate();
DB::connection('shard')->table('meter_tariffs')->truncate();

$connectionGroups = ConnectionGroup::query()->whereIn('id',[2,3])->get()->map(function($q){
ConnectionGroup::query()->whereIn('id', [2,3])->get()->map(function ($q) {
$q->delete();
$q->save();
});

} catch (\Exception $e) {
$message = $e->getMessage();
$this->info("Unexpected error occurred. Message: {$message}");

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public function __construct(
private MenuItemsService $menuItemsService,
private ManufacturerService $manufacturerService,
private TokenService $tokenService,

) {
parent::__construct();
}
Expand All @@ -80,7 +79,6 @@ public function handle()
$this->info('Meter parameter values are being shifted to meters, devices and addresses.');
$meterParameters->each(fn($meterParameter) => $this->setMeterDevices($meterParameter));
DB::connection('shard')->commit();

} catch (\Exception $e) {
$message = $e->getMessage();
$this->info("Unexpected error occurred. Message: {$message}");
Expand Down
13 changes: 2 additions & 11 deletions Website/htdocs/mpmanager/app/Events/HistoryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,21 @@ class HistoryEvent implements ShouldBroadcast
use InteractsWithSockets;
use SerializesModels;

/**
* @var History
*/
public $historyModel;

/**
* Create a new event instance.
*
* @param History $historyModel
*/
public function __construct(History $historyModel)
public function __construct(private readonly History $historyModel)
{
$this->broadcastQueue = config('services.queues.energy');

//initialize the history model
$this->historyModel = $historyModel;
}

/**
* Get the channels the event should broadcast on.
*
* @return PrivateChannel
*/
public function broadcastOn()
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel('histories');
}
Expand Down
8 changes: 1 addition & 7 deletions Website/htdocs/mpmanager/app/Events/NewTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@ class NewTicket implements ShouldBroadcast
use InteractsWithSockets;
use SerializesModels;

/**
* @var Ticket
*/
public $ticket;

/**
* Create a new event instance.
*
* @param Ticket $ticket
*/
public function __construct(Ticket $ticket)
public function __construct(private Ticket $ticket)
{
//
$this->ticket = $ticket;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function store($meterSerial, $amount)
'message' => 'transaction process started',
'transactionId' => $transaction->id,
];

} catch (\Exception $exception) {
$jsonData = [
'message' => 'transaction process can not be started',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(private AppliancePaymentService $appliancePaymentSer
{
}

public function store(AssetPerson $appliancePerson, Request $request):ApiResource
public function store(AssetPerson $appliancePerson, Request $request): ApiResource
{
try {
DB::connection('shard')->beginTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function update(UpdateAssetRequest $request, Asset $asset): ApiResource

public function destroy(Asset $asset): ApiResource
{
return ApiResource::make($this->assetService->deleteAsset($asset)
);
return ApiResource::make($this->assetService->deleteAsset($asset));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use MPM\Device\DeviceAddressService;
use MPM\Device\DeviceService;


class AssetPersonController extends Controller
{
public function __construct(
Expand All @@ -38,7 +37,6 @@ public function __construct(
private AssetService $applianceService,
private ApplianceRateService $applianceRateService
) {

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __construct(
private ClusterMeterService $clusterMetersService,
private ClusterTransactionService $clusterTransactionsService,
private ClusterPopulationService $clusterPopulationService,

) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,26 @@
use App\Models\Country;
use App\Http\Requests\CountryRequest;
use App\Http\Resources\ApiResource;
use Illuminate\Support\Facades\Config;

class CountryController extends Controller
{
public function index(): ApiResource
{
return new ApiResource(
Country::paginate(
config()->get('services.pagination')
Country::query()->paginate(
Config::get('services.pagination')
)
);
}

public function show(Country $country): ApiResource
{
return new ApiResource(
$country
);
return ApiResource::make($country);
}

public function store(CountryRequest $request): ApiResource
{
return
new ApiResource(
Country::create(
request()->only(
[
'country_name',
'country_code',
]
)
)
);
return ApiResource::make(Country::query()->create(request()->only(['country_name', 'country_code',])));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class CustomerRegistrationAppController extends Controller
{

public function __construct(private CustomerRegistrationAppService $customerRegistrationAppService)
{
}
Expand All @@ -23,10 +22,10 @@ public function store(AndroidAppRequest $request)
DB::connection('shard')->commit();

return ApiResource::make($person)->response()->setStatusCode(201);
}catch (\Exception $e){
} catch (\Exception $e) {
DB::connection('shard')->rollBack();
Log::critical('Error while adding new Customer', ['message' => $e->getMessage()]);
throw new \Exception($e->getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function update(Request $request)
$creatorId = auth('api')->user()->id;
$devices = $request->all();
foreach ($devices as $deviceData) {

$serialNumber = $deviceData['serial_number'];
$device = $this->deviceService->getBySerialNumber($serialNumber);
$previousDataOfDevice = json_encode($device->toArray());
Expand All @@ -36,8 +35,7 @@ public function update(Request $request)
'affected' => $device,
'action' => "Device infos updated from: $previousDataOfDevice to $updatedDataOfDevice"
]
]
);
]);
}
return ApiResource::make($devices);
}
Expand Down
20 changes: 2 additions & 18 deletions Website/htdocs/mpmanager/app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@ public function index()
return ApiResource::make($this->deviceService->getAll());
}

public function store(StoreDeviceRequest $request)
{
//
}

public function show(Device $device)
{
//
}

public function update(Device $device, UpdateDeviceRequest $request)
public function update(Device $device, UpdateDeviceRequest $request): ApiResource
{
$creatorId = auth('api')->user()->id;
$previousOwner = $device->person_id;
Expand All @@ -42,13 +32,7 @@ public function update(Device $device, UpdateDeviceRequest $request)
'affected' => $device,
'action' => "Device owner changed from personId: $previousOwner to personId: $newOwner"
]
]
);
]);
return ApiResource::make($updatedDevice);
}

public function destroy(Device $device)
{
//
}
}
Loading

0 comments on commit 3354585

Please sign in to comment.