Skip to content

Commit

Permalink
Merge pull request #10 from n4ss1m/refactor_rector
Browse files Browse the repository at this point in the history
Refactor: Enable Rector Rules for Code Quality and Strictness
  • Loading branch information
n4ss1m authored Feb 2, 2025
2 parents 933757f + 241cbbf commit f1521f2
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 87 deletions.
13 changes: 13 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return RectorConfig::configure()
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withSets([
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
SetList::NAMING,
SetList::RECTOR_PRESET,
SetList::STRICT_BOOLEANS,
SetList::EARLY_RETURN,
SetList::INSTANCEOF,
])
->withPhpSets();
2 changes: 2 additions & 0 deletions src/Contracts/ShippingProviderContract.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Contracts;

interface ShippingProviderContract
Expand Down
8 changes: 5 additions & 3 deletions src/CourierDZ.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ;

use CourierDZ\Enum\ShippingProvider;
Expand All @@ -11,14 +13,14 @@ class CourierDZ
/**
* Create a new ShippingService instance for the given provider.
*
* @param ShippingProvider $provider The provider to use
* @param ShippingProvider $shippingProvider The provider to use
* @param array<non-empty-string, non-empty-string> $credentials The credentials to use for the provider
*
* @throws InvalidProviderException
*/
public static function provider(ShippingProvider $provider, array $credentials): ShippingService
public static function provider(ShippingProvider $shippingProvider, array $credentials): ShippingService
{
return new ShippingService($provider->value, $credentials);
return new ShippingService($shippingProvider->value, $credentials);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Enum/ShippingProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Enum;

enum ShippingProvider: string
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/CreateOrderException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/CreateOrderValidationException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/CredentialsException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/FunctionNotSupportedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/InvalidProviderException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/TrackingIdNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\Exceptions;

use Exception;
Expand Down
32 changes: 17 additions & 15 deletions src/ProviderIntegrations/EcotrackProviderIntegration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\ProviderIntegrations;

use CourierDZ\Contracts\ShippingProviderContract;
Expand Down Expand Up @@ -61,7 +63,7 @@ public function __construct(array $credentials)
$provider_name = (static::metadata())['name'];

if (! isset($credentials['token'])) {
throw new CredentialsException("{$provider_name} credentials must include 'token'.");
throw new CredentialsException($provider_name." credentials must include 'token'.");
}

$this->credentials = $credentials;
Expand Down Expand Up @@ -91,7 +93,7 @@ public function testCredentials(): bool

// Define the headers
$headers = [
'Authorization' => "Bearer {$this->credentials['token']}",
'Authorization' => 'Bearer '.$this->credentials['token'],
];

// Make the GET request
Expand All @@ -109,9 +111,9 @@ public function testCredentials(): bool
// If the request returns any other status code, throw an HttpException
default => throw new HttpException('Ecotrack '.static::metadata()['name'].', Unexpected error occurred.'),
};
} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand All @@ -126,7 +128,7 @@ public function getRates(?int $from_wilaya_id, ?int $to_wilaya_id): array

// Define the headers
$headers = [
'Authorization' => "Bearer {$this->credentials['token']}",
'Authorization' => 'Bearer '.$this->credentials['token'],
];

// Make the GET request
Expand All @@ -142,7 +144,7 @@ public function getRates(?int $from_wilaya_id, ?int $to_wilaya_id): array
$result = json_decode($body, true);

// If the to_wilaya_id is specified, filter the result to only include the specified wilaya
if ($to_wilaya_id) {
if ($to_wilaya_id !== null && $to_wilaya_id !== 0) {
foreach ($result['livraison'] as $wilaya) {
if ($wilaya['wilaya_id'] == $to_wilaya_id) {
// Return the first matching wilaya
Expand All @@ -157,9 +159,9 @@ public function getRates(?int $from_wilaya_id, ?int $to_wilaya_id): array
// Return the list of shipping rates
return $result['livraison'];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -191,7 +193,7 @@ public function createOrder(array $orderData): array

// Define the headers
$headers = [
'Authorization' => "Bearer {$this->credentials['token']}",
'Authorization' => 'Bearer '.$this->credentials['token'],
'Content-Type' => 'application/json',
];

Expand All @@ -214,9 +216,9 @@ public function createOrder(array $orderData): array
// Return the order response
return $arrayResponse;

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand All @@ -231,7 +233,7 @@ public function orderLabel(string $orderId): array

// Define the headers
$headers = [
'Authorization' => "Bearer {$this->credentials['token']}",
'Authorization' => 'Bearer '.$this->credentials['token'],
];

// Make the GET request
Expand All @@ -254,7 +256,7 @@ public function orderLabel(string $orderId): array
// Get the response body
$label = $response->getBody()->getContents();

if (empty($label)) {
if ($label === '' || $label === '0') {
throw new HttpException('Failed to retrieve label for order with tracking ID '.$orderId.' - Empty response from Ecotrack');
}

Expand All @@ -270,9 +272,9 @@ public function orderLabel(string $orderId): array
'data' => $base64data,
];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/ProviderIntegrations/ProcolisProviderIntegration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\ProviderIntegrations;

use CourierDZ\Contracts\ShippingProviderContract;
Expand Down Expand Up @@ -108,9 +110,9 @@ public function testCredentials(): bool
// If the request returns any other status code, throw an HttpException
default => throw new HttpException('Procolis, Unexpected error occurred.'),
};
} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}

}
Expand Down Expand Up @@ -142,7 +144,7 @@ public function getRates(?int $from_wilaya_id, ?int $to_wilaya_id): array
$result = json_decode($body, true);

// If the to_wilaya_id is specified, filter the result to only include the specified wilaya
if ($to_wilaya_id) {
if ($to_wilaya_id !== null && $to_wilaya_id !== 0) {
$filteredResult = [];
foreach ($result as $wilaya) {
if ($wilaya['IDWilaya'] == $to_wilaya_id) {
Expand All @@ -163,9 +165,9 @@ public function getRates(?int $from_wilaya_id, ?int $to_wilaya_id): array
// Decode JSON response
return $result;

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -230,9 +232,9 @@ public function createOrder(array $orderData): array
// Return the created order
return $arrayResponse['Colis'][0];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -280,9 +282,9 @@ public function getOrder(string $trackingId): array
// Decode JSON response
return $arrayResponse['Colis'][0];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/ProviderIntegrations/YalidineProviderIntegration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CourierDZ\ProviderIntegrations;

use CourierDZ\Contracts\ShippingProviderContract;
Expand Down Expand Up @@ -122,9 +124,9 @@ public function testCredentials(): bool

// Any other status code is considered an unexpected error
throw new HttpException('Yalidine, Unexpected error occurred.');
} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -156,9 +158,9 @@ public function getRates($from_wilaya_id, $to_wilaya_id): array
// Return the response body as an array
return json_decode($response->getBody()->getContents(), true);

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -210,9 +212,9 @@ public function createOrder(array $orderData): array
// Return the created order
return $arrayResponse[$orderData['id']];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}

Expand Down Expand Up @@ -262,9 +264,9 @@ public function getOrder(string $trackingId): array

return $data['data'][0];

} catch (GuzzleException $e) {
} catch (GuzzleException $guzzleException) {
// Handle exceptions
throw new HttpException($e->getMessage());
throw new HttpException($guzzleException->getMessage());
}
}
}
Loading

0 comments on commit f1521f2

Please sign in to comment.