Skip to content

Commit

Permalink
Min php version 7.1, updated codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
slischka committed May 2, 2021
1 parent 1024818 commit 0609eb5
Show file tree
Hide file tree
Showing 71 changed files with 931 additions and 774 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,9 @@ jobs:

strategy:
matrix:
php-version: [ '7.1', '7.2', '7.3', '7.4' ]
php-version: [ '7.1', '7.2', '7.3', '7.4', '8.0' ]
operating-system: [ 'ubuntu-latest' ]
composer-args: [ '' ]
include:
- php-version: '7.1'
operating-system: 'ubuntu-latest'
composer-args: '--prefer-lowest'
- php-version: '8.0'
operating-system: 'ubuntu-latest'
composer-args: '--ignore-platform-reqs'

fail-fast: false

Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/composer.lock
/tests/locks/*
/tests/php.ini
/vendor/*
.idea
/tests/**/output/
4 changes: 1 addition & 3 deletions bin/tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ declare(strict_types = 1);
\escapeshellarg(
__DIR__ . '/../vendor/bin/tester'
)
. ' -c ' . \escapeshellarg(
'../tests/php.ini'
)
. ' -C '
. ' '
. '../tests',
$return
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": "^7.0",
"php": ">=7.1",
"composer/ca-bundle": "^1.1",
"psr/log": "^1.0",
"psr/http-message": "^1.0",
Expand All @@ -24,16 +24,16 @@
"ext-curl": "*"
},
"require-dev": {
"nette/tester": "^2.1",
"nette/tester": ">=2.3",
"guzzlehttp/guzzle": "^6.0 | ^7.0",
"tracy/tracy": "^2.5",
"nette/di": "^2.4",
"slevomat/coding-standard": "^4.8",
"phpstan/phpstan": "^0.10.5",
"phpstan/phpstan-nette": "^0.10.1",
"tracy/tracy": ">=2.7",
"nette/di": "^3.0",
"phpstan/phpstan": "^0.12.85",
"phpstan/phpstan-nette": "^0.12.17",
"react/http": "^0.8.3",
"react/child-process": "^0.5.2",
"phpstan/phpstan-strict-rules": "^0.10"
"phpstan/phpstan-strict-rules": "^0.12.9",
"orisai/coding-standard": "^1.1"
},
"suggest": {
"ext-curl": "to use class CurlHttpClient",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ This library is here to provide client for making HTTP requests.
It allows mocking of the HTTP client, which is useful for testing.

## Requirements
Library requires PHP 7.0 or higher.
Library requires PHP 7.1 or higher.

## Installation
The best way to install `fapi-cz/http-client` is using [Composer](http://getcomposer.org/).

```
composer require fapi-cz/http-client dev-master
composer require fapi-cz/http-client
```

## Documentation
Expand Down
14 changes: 14 additions & 0 deletions src/Fapi/HttpClient/ArgumentOutOfRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace Fapi\HttpClient;

use InvalidArgumentException;

/**
* The exception that is thrown when the value of an argument is
* outside the allowable range of values as defined by the invoked method.
*/
class ArgumentOutOfRangeException extends InvalidArgumentException
{

}
24 changes: 14 additions & 10 deletions src/Fapi/HttpClient/BaseLoggingFormatter.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php
declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace Fapi\HttpClient;

use Fapi\HttpClient\Utils\Json;
use Fapi\HttpClient\Utils\JsonException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use function base64_encode;
use function get_class;
use function serialize;
use function sprintf;
use const JSON_UNESCAPED_UNICODE;

final class BaseLoggingFormatter implements ILoggingFormatter
{
Expand All @@ -19,7 +24,7 @@ public function formatSuccessful(RequestInterface $request, ResponseInterface $r
. $this->dumpElapsedTime($elapsedTime);
}

public function formatFailed(RequestInterface $request, \Throwable $exception, float $elapsedTime): string
public function formatFailed(RequestInterface $request, Throwable $exception, float $elapsedTime): string
{
return 'Fapi\HttpClient: an HTTP request failed.'
. $this->dumpHttpRequest($request)
Expand All @@ -42,15 +47,15 @@ private function dumpHttpResponse(ResponseInterface $response): string
. ' Response body: ' . $this->dumpValue((string) $response->getBody());
}

private function dumpException(\Throwable $exception): string
private function dumpException(Throwable $exception): string
{
$dump = ' Exception type: ' . $this->dumpValue(\get_class($exception))
$dump = ' Exception type: ' . $this->dumpValue(get_class($exception))
. ' Exception message: ' . $this->dumpValue($exception->getMessage());

if ($exception->getPrevious() !== null) {
$previousException = $exception->getPrevious();

$dump .= ' Previous exception type: ' . $this->dumpValue(\get_class($previousException))
$dump .= ' Previous exception type: ' . $this->dumpValue(get_class($previousException))
. ' Previous exception message: ' . $this->dumpValue($previousException->getMessage());
}

Expand All @@ -61,19 +66,18 @@ private function dumpElapsedTime(float $elapsedTime): string
{
$elapsedTime *= 1000;

return ' Elapsed time: ' . \sprintf('%0.2f', $elapsedTime) . ' ms';
return ' Elapsed time: ' . sprintf('%0.2f', $elapsedTime) . ' ms';
}

/**
* @param mixed $value
* @return string
*/
private function dumpValue($value): string
{
try {
return Json::encode($value, \JSON_UNESCAPED_UNICODE);
return Json::encode($value, JSON_UNESCAPED_UNICODE);
} catch (JsonException $e) {
return '(serialized) ' . \base64_encode(\serialize($value));
return '(serialized) ' . base64_encode(serialize($value));
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Fapi/HttpClient/Bridges/NetteDI/HttpClientExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace Fapi\HttpClient\Bidges\NetteDI;

Expand All @@ -15,20 +14,20 @@
class HttpClientExtension extends CompilerExtension
{

/** @var mixed[] */
/** @var array<mixed> */
public $defaults = [
'type' => 'guzzle',
'logging' => false,
'bar' => false,
];

/** @var string[] */
/** @var array<string> */
private $typeClasses = [
'curl' => CurlHttpClient::class,
'guzzle' => GuzzleHttpClient::class,
];

public function loadConfiguration()
public function loadConfiguration(): void
{
$container = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);
Expand Down
27 changes: 15 additions & 12 deletions src/Fapi/HttpClient/Bridges/Tracy/BarHttpClient.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php
declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace Fapi\HttpClient\Bridges\Tracy;

use Fapi\HttpClient\HttpClientException;
use Fapi\HttpClient\IHttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Tracy\Debugger;
use Tracy\IBarPanel;
use function microtime;
use function ob_get_clean;
use function ob_start;

final class BarHttpClient implements IHttpClient, IBarPanel
{
Expand All @@ -19,7 +22,7 @@ final class BarHttpClient implements IHttpClient, IBarPanel
/** @var IHttpClient */
private $httpClient;

/** @var mixed[] */
/** @var array<mixed> */
private $requests = [];

/** @var int */
Expand All @@ -37,22 +40,22 @@ public function __construct(IHttpClient $httpClient)
public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->count++;
$startedAt = \microtime(true);
$startedAt = microtime(true);

try {
$response = $this->httpClient->sendRequest($request);
} catch (HttpClientException $e) {
$this->captureFailed($request, $e, \microtime(true) - $startedAt);
$this->captureFailed($request, $e, microtime(true) - $startedAt);

throw $e;
}

$this->captureSuccess($request, $response, \microtime(true) - $startedAt);
$this->captureSuccess($request, $response, microtime(true) - $startedAt);

return $response;
}

private function captureFailed(RequestInterface $httpRequest, \Throwable $exception, float $time)
private function captureFailed(RequestInterface $httpRequest, Throwable $exception, float $time): void
{
if ($this->count >= $this->maxRequests) {
return;
Expand All @@ -76,7 +79,7 @@ private function captureFailed(RequestInterface $httpRequest, \Throwable $except
];
}

private function captureSuccess(RequestInterface $httpRequest, ResponseInterface $httpResponse, float $time)
private function captureSuccess(RequestInterface $httpRequest, ResponseInterface $httpResponse, float $time): void
{
if ($this->count >= $this->maxRequests) {
return;
Expand Down Expand Up @@ -113,10 +116,10 @@ public function getTab()
$totalTime = $this->totalTime;
// @codingStandardsIgnoreEnd

\ob_start();
ob_start();
require __DIR__ . '/RequestPanel.tab.phtml';

return \ob_get_clean();
return (string) ob_get_clean();
}

/**
Expand All @@ -130,10 +133,10 @@ public function getPanel()
$requests = $this->requests;
// @codingStandardsIgnoreEnd

\ob_start();
ob_start();
require __DIR__ . '/RequestPanel.panel.phtml';

return \ob_get_clean();
return (string) ob_get_clean();
}

}
11 changes: 5 additions & 6 deletions src/Fapi/HttpClient/Bridges/Tracy/TracyToPsrLogger.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php
declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace Fapi\HttpClient\Bridges\Tracy;

use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
use Throwable;
use Tracy\ILogger;

final class TracyToPsrLogger extends AbstractLogger
{

/** PSR-3 log level to Tracy logger priority mapping */
const PRIORITY_MAP = [
public const PRIORITY_MAP = [
LogLevel::EMERGENCY => ILogger::CRITICAL,
LogLevel::ALERT => ILogger::CRITICAL,
LogLevel::CRITICAL => ILogger::CRITICAL,
Expand All @@ -37,12 +36,12 @@ public function log($level, $message, array $context = [])
{
$priority = self::PRIORITY_MAP[$level] ?? ILogger::ERROR;

if (isset($context['exception']) && $context['exception'] instanceof \Throwable) {
if (isset($context['exception']) && $context['exception'] instanceof Throwable) {
$this->tracyLogger->log($context['exception'], $priority);
unset($context['exception']);
}

if ($context) {
if ($context !== []) {
$message = [
'message' => $message,
'context' => $context,
Expand Down
Loading

0 comments on commit 0609eb5

Please sign in to comment.