Skip to content

Commit ff84d8b

Browse files
committed
Added check for closed call
1 parent 8336a00 commit ff84d8b

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
help: ## List available targets (this page)
33
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%-45s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
44

5+
composer-install: ## Check code style
6+
docker exec http-client /bin/sh -c 'composer install'
7+
58
cs: ## Check code style
69
docker exec http-client /bin/sh -c 'bin/cs'
710

@@ -12,4 +15,4 @@ stan: ## Fix code style
1215
docker exec http-client /bin/sh -c 'bin/stan'
1316

1417
test: ## Run tests
15-
docker exec http-client /bin/sh -c 'bin/tests'
18+
docker exec http-client /bin/sh -c 'bin/tests'

src/Fapi/HttpClient/CapturingHttpClient.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Fapi\HttpClient;
44

5+
use LogicException;
56
use Psr\Http\Message\RequestInterface;
67
use Psr\Http\Message\ResponseInterface;
78
use Tester\Dumper;
@@ -21,6 +22,8 @@ class CapturingHttpClient implements IHttpClient
2122
/** @var array<ResponseInterface> */
2223
private array $httpResponses = [];
2324

25+
private int $closed = 0;
26+
2427
public function __construct(private IHttpClient $httpClient, private string $file, private string $className)
2528
{
2629
if (!class_exists('Tester\Dumper')) {
@@ -56,6 +59,8 @@ private function capture(RequestInterface $httpRequest, ResponseInterface $httpR
5659

5760
public function close(): void
5861
{
62+
$this->closed++;
63+
5964
if ($this->httpClient instanceof $this->className) {
6065
return;
6166
}
@@ -66,7 +71,8 @@ public function close(): void
6671
private function writeToPhpFile(string $fileName, string $className): void
6772
{
6873
preg_match('#^(?:(.*)\\\\)?([^\\\\]+)\z#', $className, $match);
69-
[, $namespace, $className] = $match;
74+
$namespace = $match[1] ?? null;
75+
$className = $match[2] ?? null;
7076

7177
$code = '<?php declare(strict_types = 1);' . "\n";
7278
$code .= "\n";
@@ -120,4 +126,13 @@ private function exportValue(mixed $value, string $indent = ''): string
120126
return $s;
121127
}
122128

129+
public function __destruct()
130+
{
131+
if ($this->closed !== 1) {
132+
throw new LogicException(
133+
'Method: ' . self::class . '::closed Must be called exactly once. Called ' . $this->closed . ' times.',
134+
);
135+
}
136+
}
137+
123138
}

0 commit comments

Comments
 (0)