Skip to content

Commit 2a858d0

Browse files
committed
Fix qlty review findings
1 parent 08b9912 commit 2a858d0

6 files changed

Lines changed: 92 additions & 32 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trim_trailing_whitespace = true
1313
charset = utf-8
1414

1515
[*.md]
16-
indent_size = 4
16+
indent_size = 1
1717

1818
[*.{yml,yaml,json,neon}]
1919
indent_size = 2

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ jobs:
2121

2222
steps:
2323
- name: Checkout
24-
uses: actions/checkout@v4
24+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
25+
with:
26+
persist-credentials: false
2527

2628
- name: Setup PHP
27-
uses: shivammathur/setup-php@v2
29+
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f
2830
with:
2931
php-version: ${{ matrix.php }}
3032
extensions: json, mbstring
3133
coverage: ${{ matrix.php == '8.3' && 'xdebug' || 'none' }}
3234
tools: composer:v2
3335

3436
- name: Cache Composer dependencies
35-
uses: actions/cache@v4
37+
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830
3638
with:
3739
path: ~/.composer/cache
3840
key: composer-${{ matrix.php }}-${{ hashFiles('composer.json') }}

phpcs.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="php-currency-api">
3+
<description>PSR-12 checks for qlty/php-codesniffer.</description>
4+
5+
<file>src</file>
6+
<file>examples</file>
7+
<file>rector.php</file>
8+
9+
<exclude-pattern>build/*</exclude-pattern>
10+
<exclude-pattern>tests/*</exclude-pattern>
11+
<exclude-pattern>vendor/*</exclude-pattern>
12+
13+
<rule ref="PSR12">
14+
<exclude name="Generic.Files.LineLength"/>
15+
</rule>
16+
</ruleset>

rector.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use Rector\Set\ValueObject\SetList;
99

1010
return RectorConfig::configure()
11-
->withPaths([
12-
__DIR__ . '/src',
13-
__DIR__ . '/tests',
14-
])
15-
->withSets([
16-
LevelSetList::UP_TO_PHP_83,
17-
SetList::CODE_QUALITY,
18-
SetList::TYPE_DECLARATION,
19-
PHPUnitSetList::PHPUNIT_100,
20-
])
21-
->withImportNames(removeUnusedImports: true);
11+
->withPaths([
12+
__DIR__ . '/src',
13+
__DIR__ . '/tests',
14+
])
15+
->withSets([
16+
LevelSetList::UP_TO_PHP_83,
17+
SetList::CODE_QUALITY,
18+
SetList::TYPE_DECLARATION,
19+
PHPUnitSetList::PHPUNIT_100,
20+
])
21+
->withImportNames(removeUnusedImports: true);

src/DriverFactory.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
use Otherguy\Currency\Drivers\MockCurrencyDriver;
1717
use Otherguy\Currency\Drivers\OpenExchangeRates;
1818
use Otherguy\Currency\Exceptions\DriverNotFoundException;
19+
use Otherguy\Currency\Exceptions\MissingDependencyException;
1920
use Psr\Http\Client\ClientInterface;
2021
use Psr\Http\Message\RequestFactoryInterface;
21-
use RuntimeException;
2222

2323
class DriverFactory
2424
{
@@ -35,14 +35,14 @@ class DriverFactory
3535
public function __construct(?array $drivers = null)
3636
{
3737
$this->drivers = $drivers ?? [
38-
'mock' => MockCurrencyDriver::class,
39-
'fixerio' => FixerIo::class,
40-
'currencylayer' => CurrencyLayer::class,
41-
'openexchangerates' => OpenExchangeRates::class,
42-
'exchangeratesapi' => ExchangeRatesApi::class,
43-
'frankfurter' => Frankfurter::class,
44-
'currencyapi' => CurrencyApi::class,
45-
'fastforex' => FastForex::class,
38+
'mock' => MockCurrencyDriver::class,
39+
'fixerio' => FixerIo::class,
40+
'currencylayer' => CurrencyLayer::class,
41+
'openexchangerates' => OpenExchangeRates::class,
42+
'exchangeratesapi' => ExchangeRatesApi::class,
43+
'frankfurter' => Frankfurter::class,
44+
'currencyapi' => CurrencyApi::class,
45+
'fastforex' => FastForex::class,
4646
];
4747
}
4848

@@ -124,24 +124,57 @@ public static function setDefault(?self $instance): void
124124
private function defaultClient(): ClientInterface
125125
{
126126
if (!class_exists(GuzzleClient::class)) {
127-
throw new RuntimeException(
128-
'No PSR-18 HTTP client supplied and guzzlehttp/guzzle is not installed. '
129-
. 'Either install guzzlehttp/guzzle, or pass a ClientInterface to DriverFactory::make().',
127+
throw new MissingDependencyException(
128+
'No PSR-18 HTTP client supplied and guzzlehttp/guzzle is '
129+
. 'not installed. Either install guzzlehttp/guzzle, or pass '
130+
. 'a ClientInterface to DriverFactory::make().',
130131
);
131132
}
132133

133-
return new GuzzleClient();
134+
$client = $this->buildDefaultClient();
135+
if (!$client instanceof ClientInterface) {
136+
throw new MissingDependencyException(
137+
'The installed guzzlehttp/guzzle package does not provide a PSR-18 '
138+
. 'ClientInterface implementation.',
139+
);
140+
}
141+
142+
return $client;
134143
}
135144

136145
private function defaultRequestFactory(): RequestFactoryInterface
137146
{
138147
if (!class_exists(GuzzleRequestFactory::class)) {
139-
throw new RuntimeException(
140-
'No PSR-17 RequestFactory supplied and http-interop/http-factory-guzzle is not installed. '
141-
. 'Either install http-interop/http-factory-guzzle, or pass a RequestFactoryInterface to DriverFactory::make().',
148+
throw new MissingDependencyException(
149+
'No PSR-17 RequestFactory supplied and '
150+
. 'http-interop/http-factory-guzzle is not installed. '
151+
. 'Either install http-interop/http-factory-guzzle, or pass '
152+
. 'a RequestFactoryInterface to DriverFactory::make().',
142153
);
143154
}
144155

145-
return new GuzzleRequestFactory();
156+
$requestFactory = $this->buildDefaultRequestFactory();
157+
if (!$requestFactory instanceof RequestFactoryInterface) {
158+
throw new MissingDependencyException(
159+
'The installed http-interop/http-factory-guzzle package does not '
160+
. 'provide a PSR-17 RequestFactoryInterface implementation.',
161+
);
162+
}
163+
164+
return $requestFactory;
165+
}
166+
167+
private function buildDefaultClient(): object
168+
{
169+
$class = GuzzleClient::class;
170+
171+
return new $class();
172+
}
173+
174+
private function buildDefaultRequestFactory(): object
175+
{
176+
$class = GuzzleRequestFactory::class;
177+
178+
return new $class();
146179
}
147180
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Otherguy\Currency\Exceptions;
6+
7+
class MissingDependencyException extends CurrencyException
8+
{
9+
}

0 commit comments

Comments
 (0)