Skip to content

Commit 9cb87c9

Browse files
committed
Fixed Psalm errors and added discovery process for http related implementations
1 parent 2644864 commit 9cb87c9

File tree

5 files changed

+26
-52
lines changed

5 files changed

+26
-52
lines changed

composer.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
1212
"require": {
1313
"php": ">=8.1",
1414
"ext-json": "*",
15-
"composer-runtime-api": "^2.0",
1615
"facebook/php-business-sdk": "^22.0",
16+
"php-http/discovery": "^1.20",
1717
"psr/http-client": "^1.0",
18+
"psr/http-client-implementation": "*",
1819
"psr/http-factory": "^1.0",
20+
"psr/http-factory-implementation": "*",
1921
"psr/http-message": "^1.0 || ^2.0",
2022
"psr/log": "^1.1 || ^2.0 || ^3.0",
2123
"webmozart/assert": "^1.11"
2224
},
2325
"require-dev": {
2426
"infection/infection": "^0.26.21",
25-
"kriswallsmith/buzz": "^1.3",
2627
"nyholm/psr7": "^1.8",
2728
"phpunit/phpunit": "^9.6",
2829
"psalm/plugin-phpunit": "^0.19",
2930
"setono/code-quality-pack": "^2.9",
30-
"shipmonk/composer-dependency-analyser": "^1.8.2"
31+
"shipmonk/composer-dependency-analyser": "^1.8.2",
32+
"symfony/http-client": "^6.4 || ^7.0"
3133
},
3234
"prefer-stable": true,
3335
"autoload": {
@@ -44,7 +46,8 @@
4446
"allow-plugins": {
4547
"dealerdirect/phpcodesniffer-composer-installer": false,
4648
"ergebnis/composer-normalize": true,
47-
"infection/extension-installer": true
49+
"infection/extension-installer": true,
50+
"php-http/discovery": false
4851
},
4952
"sort-packages": true
5053
},

src/Client/Client.php

+6-34
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Setono\MetaConversionsApi\Client;
66

7-
use Buzz\Client\Curl;
87
use FacebookAds\ApiConfig;
9-
use Nyholm\Psr7\Factory\Psr17Factory;
8+
use Http\Discovery\Psr17FactoryDiscovery;
9+
use Http\Discovery\Psr18ClientDiscovery;
1010
use Psr\Http\Client\ClientInterface as HttpClientInterface;
1111
use Psr\Http\Message\RequestFactoryInterface;
1212
use Psr\Http\Message\ResponseFactoryInterface;
@@ -76,14 +76,7 @@ public function sendEvent(Event $event): void
7676
private function getHttpClient(): HttpClientInterface
7777
{
7878
if (null === $this->httpClient) {
79-
if (!class_exists(Curl::class)) {
80-
throw ClientException::missingDependency(
81-
Curl::class,
82-
sprintf('Either set the http client with %s or run composer require kriswallsmith/buzz', self::class . '::setHttpClient()'),
83-
);
84-
}
85-
86-
$this->httpClient = new Curl($this->getResponseFactory());
79+
$this->httpClient = Psr18ClientDiscovery::find();
8780
}
8881

8982
return $this->httpClient;
@@ -97,14 +90,7 @@ public function setHttpClient(HttpClientInterface $httpClient): void
9790
private function getRequestFactory(): RequestFactoryInterface
9891
{
9992
if (null === $this->requestFactory) {
100-
if (!class_exists(Psr17Factory::class)) {
101-
throw ClientException::missingDependency(
102-
Psr17Factory::class,
103-
sprintf('Either set the request factory with %s or run composer require nyholm/psr7', self::class . '::setRequestFactory()'),
104-
);
105-
}
106-
107-
$this->requestFactory = new Psr17Factory();
93+
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
10894
}
10995

11096
return $this->requestFactory;
@@ -118,14 +104,7 @@ public function setRequestFactory(RequestFactoryInterface $requestFactory): void
118104
private function getResponseFactory(): ResponseFactoryInterface
119105
{
120106
if (null === $this->responseFactory) {
121-
if (!class_exists(Psr17Factory::class)) {
122-
throw ClientException::missingDependency(
123-
Psr17Factory::class,
124-
sprintf('Either set the response factory with %s or run composer require nyholm/psr7', self::class . '::setResponseFactory()'),
125-
);
126-
}
127-
128-
$this->responseFactory = new Psr17Factory();
107+
$this->responseFactory = Psr17FactoryDiscovery::findResponseFactory();
129108
}
130109

131110
return $this->responseFactory;
@@ -139,14 +118,7 @@ public function setResponseFactory(ResponseFactoryInterface $responseFactory): v
139118
private function getStreamFactory(): StreamFactoryInterface
140119
{
141120
if (null === $this->streamFactory) {
142-
if (!class_exists(Psr17Factory::class)) {
143-
throw ClientException::missingDependency(
144-
Psr17Factory::class,
145-
sprintf('Either set the stream factory with %s or run composer require nyholm/psr7', self::class . '::setStreamFactory()'),
146-
);
147-
}
148-
149-
$this->streamFactory = new Psr17Factory();
121+
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
150122
}
151123

152124
return $this->streamFactory;

tests/Client/ClientTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function log($level, string|\Stringable $message, array $context = []): v
9797
$this->messages[] = $message;
9898
}
9999

100+
/**
101+
* @param non-empty-string $regexp
102+
*/
100103
public function hasMessageMatching(string $regexp): bool
101104
{
102105
foreach ($this->messages as $message) {

tests/ValueObject/FbcTest.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ public function it_handles_wrong_input(string $input): void
6262
}
6363

6464
/**
65-
* @return list<list<string>>
65+
* @return \Generator<array-key, array{string}>
6666
*/
67-
public function wrongInputs(): array
67+
public function wrongInputs(): \Generator
6868
{
69-
return [
70-
['wrong input'],
71-
['afb.1.1657051589577.IwAR0rmfgHgxjdKoEopat9y2SPzyjGgfHm9AhdqygToWvarP59nPq15T07MiA'],
72-
['fb.1.1657051589577.IwAR0rmfgHgxjdKoEopat9y2SPzyjGgfHm9AhdqygToWvarP59nPq15T07MiA_'],
73-
];
69+
yield ['wrong input'];
70+
yield ['afb.1.1657051589577.IwAR0rmfgHgxjdKoEopat9y2SPzyjGgfHm9AhdqygToWvarP59nPq15T07MiA'];
71+
yield ['fb.1.1657051589577.IwAR0rmfgHgxjdKoEopat9y2SPzyjGgfHm9AhdqygToWvarP59nPq15T07MiA_'];
7472
}
7573
}

tests/ValueObject/FbpTest.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ public function it_handles_wrong_input(string $input): void
5555
}
5656

5757
/**
58-
* @return list<list<string>>
58+
* @return \Generator<array-key, array{string}>
5959
*/
60-
public function wrongInputs(): array
60+
public function wrongInputs(): \Generator
6161
{
62-
return [
63-
['wrong input'],
64-
['fb.1.1656874832584.1088522659a'],
65-
['afb.1.1656874832584.1088522659'],
66-
];
62+
yield ['wrong input'];
63+
yield ['fb.1.1656874832584.1088522659a'];
64+
yield ['afb.1.1656874832584.1088522659'];
6765
}
6866
}

0 commit comments

Comments
 (0)