Skip to content

Commit

Permalink
add typehints and drop support for php<8.0 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
slepic authored Jan 7, 2022
1 parent 8f400a3 commit eb6e5c9
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 296 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.idea
.php_cs.cache
composer.lock
.php-cs-fixer.cache
.phpunit.result.cache
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php
php:
- '5.6'
- '7.3'
- '8.0'
- '8.1'

before_script:
- composer self-update
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Simple PHP library working with PSR HTTP message transfers.",
"type": "library",
"require": {
"php": ">=5.6",
"php": ">=8.0",
"psr/http-message": "^1.0"
},
"autoload": {
Expand All @@ -28,8 +28,8 @@
},
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "~5.0",
"friendsofphp/php-cs-fixer": "^2.14"
"phpunit/phpunit": "^9",
"friendsofphp/php-cs-fixer": "^3.4"
},
"scripts": {
"check-cs": [
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.5'

services:
php:
image: composer:latest
container_name: php
volumes:
- .:/app:delegated
working_dir: /app
2 changes: 1 addition & 1 deletion php-cs-fixer/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
return new \SplFileInfo($item);
}, $output);

$config = PhpCsFixer\Config::create();
$config = new PhpCsFixer\Config();
$config->setFinder($output);

if (\count($output) > 0) {
Expand Down
23 changes: 2 additions & 21 deletions src/History/HistoryObserver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Slepic\Http\Transfer\History;

use Psr\Http\Message\RequestInterface;
Expand All @@ -9,33 +8,15 @@
use Slepic\Http\Transfer\Observer\ObserverInterface;

/**
* Class HistoryObserver
* @package Slepic\Http\Transfer\History
*
* This observer creates a LogInterface instance and pushes it to a StorageInterface when the transfer is finished.
*/
final class HistoryObserver implements ObserverInterface
{
/**
* @var StorageInterface
*/
private $storage;

/**
* HistoryObserver constructor.
* @param StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
public function __construct(private StorageInterface $storage)
{
$this->storage = $storage;
}

/**
* @param RequestInterface $request
* @param array $context
* @return ObserverDelegateInterface
*/
public function observe(RequestInterface $request, array $context = [])
public function observe(RequestInterface $request, array $context = []): ObserverDelegateInterface
{
return new HistoryObserverDelegate(
$this->storage,
Expand Down
41 changes: 3 additions & 38 deletions src/History/HistoryObserverDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,18 @@
use Slepic\Http\Transfer\Observer\AbstractObserverDelegate;

/**
* Class HistoryObserver
* @package Slepic\Http\Transfer\History
*
* This observer creates a LogInterface instance and pushes it to a StorageInterface when the transfer is finished.
*/
final class HistoryObserverDelegate extends AbstractObserverDelegate
{
/**
* @var StorageInterface
*/
private $storage;
private float $startTime;

/**
* @var RequestInterface
*/
private $request;

/**
* @var float
*/
private $startTime;

/**
* @var array
*/
private $context;

/**
* HistoryObserverDelegate constructor.
* @param StorageInterface $storage
* @param RequestInterface $request
* @param array $context
*/
public function __construct(StorageInterface $storage, RequestInterface $request, array $context = [])
public function __construct(private StorageInterface $storage, private RequestInterface $request, private array $context = [])
{
$this->startTime = \microtime(true);
$this->storage = $storage;
$this->request = $request;
$this->context = $context;
}

/**
* @param ResponseInterface|null $response
* @param \Exception|null $exception
* @throws \Exception
*/
protected function finish(ResponseInterface $response = null, \Exception $exception = null)
protected function finish(?ResponseInterface $response = null, \Throwable $exception = null): void
{
$log = new Log(
$this->request,
Expand Down
36 changes: 10 additions & 26 deletions src/Log/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,31 @@
namespace Slepic\Http\Transfer\Log;

/**
* Class ArrayStorage
* @package Slepic\Http\Transfer\Log
*
* Simple implementation of StorageInterface that allows to read the logs through an IteratorAggregate.
*/
class ArrayStorage implements StorageInterface, \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* @var \ArrayObject
*/
private $logs;
private \ArrayObject $logs;

/**
* ArrayStorage constructor.
* @param array|\ArrayAccess|null $storage
*/
public function __construct($storage = [])
public function __construct(array|\ArrayAccess $storage = [])
{
$this->logs = new \ArrayObject($storage);
}

/**
* @param LogInterface $log
*/
public function store(LogInterface $log)
public function store(LogInterface $log): void
{
$this->logs[] = $log;
}

/**
* @return \Iterator
* @return \Iterator<LogInterface>
*/
public function getIterator()
public function getIterator(): \Iterator
{
return $this->logs->getIterator();
}

/**
* @return int
*/
public function count()
public function count(): int
{
return \count($this->logs);
}
Expand All @@ -52,7 +36,7 @@ public function count()
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->logs[$offset]);
}
Expand All @@ -61,7 +45,7 @@ public function offsetExists($offset)
* @param int $offset
* @return LogInterface|null
*/
public function offsetGet($offset)
public function offsetGet($offset): ?LogInterface
{
return $this->logs[$offset];
}
Expand All @@ -71,15 +55,15 @@ public function offsetGet($offset)
* @param LogInterface $value
* @throws \Exception
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
if ($offset !== null) {
throw new \Exception('Logs can only be appended.');
}
$this->store($value);
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
throw new \BadMethodCallException('Cannot unset logs.');
}
Expand Down
92 changes: 12 additions & 80 deletions src/Log/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,47 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Class Log
* @package Slepic\Http\Transfer\Log
*/
class Log implements LogInterface
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var ResponseInterface|null
*/
private $response;

/**
* @var \Exception|null
*/
private $exception;

/**
* @var float
*/
private $startTime;

/**
* @var float
*/
private $endTime;

/**
* @var array
*/
private $context;

/**
* Log constructor.
* @param RequestInterface $request
* @param float $startTime
* @param float $endTime
* @param ResponseInterface|null $response
* @param \Exception|null $exception
* @param array $context
* @throws \Exception
*/
public function __construct(
RequestInterface $request,
$startTime,
$endTime,
ResponseInterface $response = null,
\Exception $exception = null,
array $context = []
private RequestInterface $request,
private float $startTime,
private float $endTime,
private ?ResponseInterface $response = null,
private ?\Throwable $exception = null,
private array $context = []
) {
if ($response === null && $exception === null) {
throw new \Exception('At least one of response/exception cannot be null.');
}
$this->request = $request;
$this->response = $response;
$this->exception = $exception;
$this->startTime = $startTime;
$this->endTime = $endTime;
$this->context = $context;
}

/**
* @return RequestInterface
*/
public function getRequest()
public function getRequest(): RequestInterface
{
return $this->request;
}

/**
* @return ResponseInterface|null
*/
public function getResponse()
public function getResponse(): ?ResponseInterface
{
return $this->response;
}

/**
* @return \Exception|null
*/
public function getException()
public function getException(): ?\Throwable
{
return $this->exception;
}

/**
* @return float
*/
public function getStartTime()
public function getStartTime(): float
{
return $this->startTime;
}

/**
* @return float
*/
public function getEndTime()
public function getEndTime(): float
{
return $this->endTime;
}

/**
* @return array
*/
public function getContext()
public function getContext(): array
{
return $this->context;
}
Expand Down
Loading

0 comments on commit eb6e5c9

Please sign in to comment.