Skip to content

Commit 01dbffe

Browse files
committed
remove thecodingmachine/safe
1 parent 6f18305 commit 01dbffe

8 files changed

+87
-17
lines changed

composer.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
"php-http/httplug": ">=1.0",
2929
"php-http/discovery": "^1.0",
3030
"guzzlehttp/psr7": "^1.4.2",
31-
"php-http/message": "^1.0",
32-
"thecodingmachine/safe": "^1.0"
31+
"php-http/message": "^1.0"
3332
},
3433
"require-dev" : {
3534
"phpunit/phpunit": "^7",
3635
"squizlabs/php_codesniffer": "^3.2",
3736
"phpstan/phpstan": "^0.12.7",
38-
"thecodingmachine/phpstan-safe-rule": "^1.0",
3937
"thecodingmachine/phpstan-strict-rules": "^0.12.0",
4038
"php-http/mock-client": "^1.0",
4139
"php-http/guzzle6-adapter": "^1.1",

phpstan.neon

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
includes:
2-
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
3-
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
2+
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

src/Client.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
use Http\Discovery\MessageFactoryDiscovery;
1212
use Psr\Http\Message\RequestInterface;
1313
use Psr\Http\Message\ResponseInterface;
14-
use Safe\Exceptions\FilesystemException;
15-
use function Safe\fclose;
16-
use function Safe\fopen;
17-
use function Safe\fwrite;
14+
use function fclose;
15+
use function fopen;
16+
use function fwrite;
1817

1918
final class Client
2019
{
@@ -57,8 +56,17 @@ public function store(GotenbergRequestInterface $request, string $destination):
5756
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
5857
$fileStream = $response->getBody();
5958
$fp = fopen($destination, 'w');
60-
fwrite($fp, $fileStream->getContents());
61-
fclose($fp);
59+
if ($fp === false) {
60+
throw FilesystemException::createFromPhpError();
61+
}
62+
63+
if (fwrite($fp, $fileStream->getContents()) === false) {
64+
throw FilesystemException::createFromPhpError();
65+
}
66+
67+
if (fclose($fp) === false) {
68+
throw FilesystemException::createFromPhpError();
69+
}
6270
}
6371

6472
private function makeMultipartFormDataRequest(GotenbergRequestInterface $request): RequestInterface

src/DocumentFactory.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
use GuzzleHttp\Psr7\LazyOpenStream;
88
use Psr\Http\Message\StreamInterface;
9-
use Safe\Exceptions\FilesystemException;
9+
use function fopen;
10+
use function fwrite;
1011
use function GuzzleHttp\Psr7\stream_for;
11-
use function Safe\fopen;
12-
use function Safe\fwrite;
1312

1413
final class DocumentFactory
1514
{
@@ -29,7 +28,13 @@ public static function makeFromStream(string $fileName, StreamInterface $fileStr
2928
public static function makeFromString(string $fileName, string $string): Document
3029
{
3130
$fileStream = fopen('php://memory', 'rb+');
32-
fwrite($fileStream, $string);
31+
if ($fileStream === false) {
32+
throw FilesystemException::createFromPhpError();
33+
}
34+
35+
if (fwrite($fileStream, $string) === false) {
36+
throw FilesystemException::createFromPhpError();
37+
}
3338

3439
return new Document($fileName, stream_for($fileStream));
3540
}

src/FilesystemException.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\Gotenberg;
6+
7+
use ErrorException;
8+
use RuntimeException;
9+
use function error_get_last;
10+
11+
class FilesystemException extends ErrorException
12+
{
13+
public static function createFromPhpError(): self
14+
{
15+
$error = error_get_last();
16+
17+
if ($error === null) {
18+
throw new RuntimeException('No error information available');
19+
}
20+
21+
return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
22+
}
23+
}

tests/ClientTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use HTTP\Client\Exception;
88
use PHPUnit\Framework\TestCase;
9-
use Safe\Exceptions\FilesystemException;
109

1110
final class ClientTest extends TestCase
1211
{

tests/DocumentFactoryTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use GuzzleHttp\Psr7\LazyOpenStream;
88
use PHPUnit\Framework\TestCase;
9-
use Safe\Exceptions\FilesystemException;
109

1110
final class DocumentFactoryTest extends TestCase
1211
{

tests/FilesystemExceptionTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\Gotenberg;
6+
7+
use ErrorException;
8+
use PHPUnit\Framework\TestCase;
9+
use RuntimeException;
10+
use function error_clear_last;
11+
use function fclose;
12+
use function fopen;
13+
14+
class FilesystemExceptionTest extends TestCase
15+
{
16+
public function testExceptionCanBeCreated(): void
17+
{
18+
$fp = fopen('php://memory', 'r');
19+
fclose($fp);
20+
@fclose($fp);
21+
22+
$exception = FilesystemException::createFromPhpError();
23+
24+
$this->assertInstanceOf(ErrorException::class, $exception);
25+
$this->assertSame('fclose(): supplied resource is not a valid stream resource', $exception->getMessage());
26+
$this->assertSame(0, $exception->getCode());
27+
$this->assertSame(2, $exception->getSeverity());
28+
}
29+
30+
public function testExceptionThrownWhenNoError(): void
31+
{
32+
error_clear_last();
33+
34+
$this->expectException(RuntimeException::class);
35+
$this->expectExceptionMessage('No error information available');
36+
37+
FilesystemException::createFromPhpError();
38+
}
39+
}

0 commit comments

Comments
 (0)