Skip to content

Commit

Permalink
refactor code for use with PHP 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorijn committed Apr 7, 2023
1 parent 2640800 commit cd88951
Show file tree
Hide file tree
Showing 87 changed files with 552 additions and 630 deletions.
22 changes: 7 additions & 15 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@
* with this source code in the file LICENSE.
*/

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__.'/src',
__DIR__.'/tests',
]);

// Define what rule sets will be applied
$containerConfigurator->import(SetList::DEAD_CODE);
$containerConfigurator->import(SetList::CODE_QUALITY);
$containerConfigurator->import(SetList::PHP_80);
$containerConfigurator->import(SetList::NAMING);
$containerConfigurator->import(SetList::ORDER);
$containerConfigurator->import(SetList::TYPE_DECLARATION_STRICT);
$containerConfigurator->import(SetList::EARLY_RETURN);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
]);
};
13 changes: 6 additions & 7 deletions src/Client/BinanceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@

namespace Jorijn\Bitcoin\Dca\Client;

use InvalidArgumentException;
use Jorijn\Bitcoin\Dca\Exception\BinanceClientException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class BinanceClient implements BinanceClientInterface
{
/** @var string */
public const USER_AGENT = 'Mozilla/4.0 (compatible; Binance PHP client; Jorijn/BitcoinDca; '.PHP_OS.'; PHP/'.PHP_VERSION.')';
final public const USER_AGENT = 'Mozilla/4.0 (compatible; Binance PHP client; Jorijn/BitcoinDca; '.PHP_OS.'; PHP/'.PHP_VERSION.')';

/** @var string */
public const HASH_ALGO = 'sha256';
final public const HASH_ALGO = 'sha256';

public function __construct(
protected HttpClientInterface $httpClient,
Expand Down Expand Up @@ -54,12 +53,12 @@ public function request(string $method, string $url, array $options = []): array
case 'TRADE':
case 'USER_DATA':
[$method, $url, $options] = $this->addSignatureToRequest($method, $url, $options);
// no break
// no break
case 'USER_STREAM':
case 'MARKET_DATA':
// @noinspection SuspiciousAssignmentsInspection
[$method, $url, $options] = $this->addApiKeyToRequest($method, $url, $options);
// no break
// no break
case 'NONE':
default:
return $this->parse($this->httpClient->request($method, $url, $options));
Expand All @@ -74,8 +73,8 @@ protected function addSignatureToRequest(string $method, string $url, array $opt
// check and validate any present body
if (isset($options['body'])) {
if (!\is_array($options['body'])) {
throw new InvalidArgumentException(
'passing any other request body than type `array` on '.__CLASS__.' is not supported'
throw new \InvalidArgumentException(
'passing any other request body than type `array` on '.self::class.' is not supported'
);
}

Expand Down
15 changes: 7 additions & 8 deletions src/Client/Bl3pClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
use Jorijn\Bitcoin\Dca\Exception\Bl3pClientException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;

/**
* @source https://github.com/BitonicNL/bl3p-api/blob/master/examples/php/example.php
*/
class Bl3pClient implements Bl3pClientInterface
{
public const LOG_API_CALL_FAILED = 'API call failed: {url}';
public const API_KEY_RESULT = 'result';
public const API_KEY_DATA = 'data';
public const LOG_CONTEXT_PARAMETERS = 'parameters';
public const LOG_CONTEXT_URL = 'url';
public const API_KEY_MESSAGE = 'message';
final public const LOG_API_CALL_FAILED = 'API call failed: {url}';
final public const API_KEY_RESULT = 'result';
final public const API_KEY_DATA = 'data';
final public const LOG_CONTEXT_PARAMETERS = 'parameters';
final public const LOG_CONTEXT_URL = 'url';
final public const API_KEY_MESSAGE = 'message';

public function __construct(
protected HttpClientInterface $httpClient,
Expand Down Expand Up @@ -69,7 +68,7 @@ public function apiCall($path, $parameters = []): array
try {
// convert json into an array
$result = $serverResponse->toArray(true);
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$this->logger->error(
self::LOG_API_CALL_FAILED,
[self::LOG_CONTEXT_URL => $path, self::LOG_CONTEXT_PARAMETERS => $parameters]
Expand Down
6 changes: 2 additions & 4 deletions src/Client/Bl3pClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Client;

use Exception;

interface Bl3pClientInterface
{
/**
Expand All @@ -23,9 +21,9 @@ interface Bl3pClientInterface
* @param string $path path to call
* @param array $parameters parameters to add to the call
*
* @throws Exception
*
* @return array result of call
*
* @throws \Exception
*/
public function apiCall($path, $parameters = []): array;
}
2 changes: 1 addition & 1 deletion src/Client/KrakenClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class KrakenClient implements KrakenClientInterface
{
public const USER_AGENT = 'Mozilla/4.0 (compatible; Kraken PHP client; Jorijn/BitcoinDca; '.PHP_OS.'; PHP/'.PHP_VERSION.')';
final public const USER_AGENT = 'Mozilla/4.0 (compatible; Kraken PHP client; Jorijn/BitcoinDca; '.PHP_OS.'; PHP/'.PHP_VERSION.')';

public function __construct(
protected HttpClientInterface $httpClient,
Expand Down
3 changes: 1 addition & 2 deletions src/Client/VerboseHttpClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Throwable;

class VerboseHttpClientDecorator implements HttpClientInterface
{
Expand Down Expand Up @@ -50,7 +49,7 @@ public function request(string $method, string $url, array $options = []): Respo

try {
$response = $this->httpClient->request($method, $url, $options);
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$this->logger->debug(
'[API call] exception was raised',
[
Expand Down
3 changes: 1 addition & 2 deletions src/Command/BalanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

class BalanceCommand extends Command
{
Expand Down Expand Up @@ -48,7 +47,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$table->render();

$symfonyStyle->success('Success!');
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$symfonyStyle->error($exception->getMessage());

return 1;
Expand Down
3 changes: 1 addition & 2 deletions src/Command/BuyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;

class BuyCommand extends Command implements MachineReadableOutputCommandInterface
{
Expand Down Expand Up @@ -86,7 +85,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$this->displayFormattedPurchaseOrder($completedBuyOrder, $symfonyStyle, $input->getOption('output'));

return 0;
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$symfonyStyle->error($exception->getMessage());
}

Expand Down
3 changes: 1 addition & 2 deletions src/Component/AddressFromMasterPublicKeyComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer;
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer;
use Jorijn\Bitcoin\Dca\Exception\NoMasterPublicKeyAvailableException;
use const PHP_INT_SIZE;

class AddressFromMasterPublicKeyComponent implements AddressFromMasterPublicKeyComponentInterface
{
Expand Down Expand Up @@ -82,6 +81,6 @@ public function derive(string $masterPublicKey, $path = '0/0'): string
public function supported(): bool
{
// this component only works on PHP 64-bits
return PHP_INT_SIZE === 8;
return \PHP_INT_SIZE === 8;
}
}
5 changes: 2 additions & 3 deletions src/Component/ExternalAddressFromMasterPublicKeyComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Jorijn\Bitcoin\Dca\Component;

use Psr\Log\LoggerInterface;
use Throwable;

class ExternalAddressFromMasterPublicKeyComponent implements AddressFromMasterPublicKeyComponentInterface
{
Expand All @@ -35,7 +34,7 @@ public function derive(string $masterPublicKey, $path = '0/0'): string
return $this->addressCache[$masterPublicKey][$path];
}

[$namespace, $index] = explode('/', $path);
[$namespace, $index] = explode('/', (string) $path);
if ('0' !== $namespace) {
throw new \InvalidArgumentException('no change addresses supported');
}
Expand All @@ -54,7 +53,7 @@ public function derive(string $masterPublicKey, $path = '0/0'): string
try {
// decode the result and add it to the cache in the same go.
$result = $this->addressCache[$masterPublicKey] = json_decode($strResult, true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$this->logger->error(
'failed to decode from external derivation tool: '.($exception->getMessage() ?: $exception::class),
[
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/CheckForUpdatesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class CheckForUpdatesListener
{
public const TEST_VALID_VERSION = '/^v\d+\.\d+\.\d+$/';
final public const TEST_VALID_VERSION = '/^v\d+\.\d+\.\d+$/';
protected RemoteReleaseInformation $remoteReleaseInformation;

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getRandomQuote(): ?Quote
if (empty($quotes)) {
return null;
}
if (2 !== \count($quotes[0])) {
if (2 !== (is_countable($quotes[0]) ? \count($quotes[0]) : 0)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/Notifications/SendEmailOnBuyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class SendEmailOnBuyListener extends AbstractSendEmailListener
{
public const NOTIFICATION_SUBJECT_LINE = 'You bought %s sats on %s';
final public const NOTIFICATION_SUBJECT_LINE = 'You bought %s sats on %s';

public function onBuy(BuySuccessEvent $buySuccessEvent): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class SendEmailOnWithdrawListener extends AbstractSendEmailListener
{
public const NOTIFICATION_SUBJECT_LINE = 'You withdrew %s satoshis from %s';
final public const NOTIFICATION_SUBJECT_LINE = 'You withdrew %s satoshis from %s';

public function onWithdraw(WithdrawSuccessEvent $withdrawSuccessEvent): void
{
Expand Down
3 changes: 1 addition & 2 deletions src/EventListener/WriteOrderToCsvListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\SerializerAwareTrait;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;

class WriteOrderToCsvListener
{
Expand Down Expand Up @@ -60,7 +59,7 @@ public function onSuccessfulBuy(BuySuccessEvent $buySuccessEvent): void
'add_headers' => $addHeaders,
]
);
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$this->logger->error(
'unable to write order to file',
[
Expand Down
3 changes: 1 addition & 2 deletions src/EventListener/XPubAddressUsedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Jorijn\Bitcoin\Dca\Event\WithdrawSuccessEvent;
use Jorijn\Bitcoin\Dca\Repository\TaggedIntegerRepositoryInterface;
use Psr\Log\LoggerInterface;
use Throwable;

class XPubAddressUsedListener
{
Expand Down Expand Up @@ -58,7 +57,7 @@ public function onWithdrawAddressUsed(WithdrawSuccessEvent $withdrawSuccessEvent

// we have a match, increase the index in the database so a new address is returned next time
$this->taggedIntegerRepository->increase($this->configuredXPub);
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$this->logger->error('failed to determine / increase xpub index', [
'xpub' => $this->configuredXPub,
'reason' => $exception->getMessage() ?: $exception::class,
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/BinanceClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class BinanceClientException extends RuntimeException
class BinanceClientException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/BitvavoClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class BitvavoClientException extends RuntimeException
class BitvavoClientException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/Bl3pClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class Bl3pClientException extends RuntimeException
class Bl3pClientException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/BuyTimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class BuyTimeoutException extends RuntimeException
class BuyTimeoutException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/CouldNotGetExternalDerivationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class CouldNotGetExternalDerivationException extends RuntimeException
class CouldNotGetExternalDerivationException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/KrakenClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class KrakenClientException extends RuntimeException
class KrakenClientException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/NoDerivationComponentAvailableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class NoDerivationComponentAvailableException extends RuntimeException
class NoDerivationComponentAvailableException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/NoExchangeAvailableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class NoExchangeAvailableException extends RuntimeException
class NoExchangeAvailableException extends \RuntimeException
{
}
4 changes: 1 addition & 3 deletions src/Exception/NoMasterPublicKeyAvailableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Jorijn\Bitcoin\Dca\Exception;

use RuntimeException;

class NoMasterPublicKeyAvailableException extends RuntimeException
class NoMasterPublicKeyAvailableException extends \RuntimeException
{
}

0 comments on commit cd88951

Please sign in to comment.