Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply PHP 8.0 Syntax and constructor promotion #119

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CallbackStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Laminas\Diactoros;

use Psr\Http\Message\StreamInterface;
use Stringable;

use function array_key_exists;

Expand All @@ -13,7 +14,7 @@
/**
* Implementation of PSR HTTP streams
*/
class CallbackStream implements StreamInterface
class CallbackStream implements StreamInterface, Stringable
{
/** @var callable|null */
protected $callback;
Expand Down
3 changes: 1 addition & 2 deletions src/Exception/InvalidForwardedHeaderNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

class InvalidForwardedHeaderNameException extends RuntimeException implements ExceptionInterface
{
/** @param mixed $name */
public static function forHeader($name): self
public static function forHeader(mixed $name): self
{
if (! is_string($name)) {
$name = sprintf('(value of type %s)', is_object($name) ? $name::class : gettype($name));
Expand Down
3 changes: 1 addition & 2 deletions src/Exception/InvalidProxyAddressException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class InvalidProxyAddressException extends RuntimeException implements ExceptionInterface
{
/** @param mixed $proxy */
public static function forInvalidProxyArgument($proxy): self
public static function forInvalidProxyArgument(mixed $proxy): self
{
$type = is_object($proxy) ? $proxy::class : gettype($proxy);
return new self(sprintf(
Expand Down
5 changes: 2 additions & 3 deletions src/HeaderSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function isValid($value): bool
* @param mixed $value Value to be tested. This method asserts it is a string or number.
* @throws Exception\InvalidArgumentException For invalid values.
*/
public static function assertValid($value): void
public static function assertValid(mixed $value): void
{
if (! is_string($value) && ! is_numeric($value)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -144,10 +144,9 @@ public static function assertValid($value): void
*
* @see http://tools.ietf.org/html/rfc7230#section-3.2
*
* @param mixed $name
* @throws Exception\InvalidArgumentException
*/
public static function assertValidName($name): void
public static function assertValidName(mixed $name): void
{
if (! is_string($name)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand Down
3 changes: 1 addition & 2 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,9 @@ private function validateProtocolVersion($version): void
}

/**
* @param mixed $values
* @return string[]
*/
private function filterHeaderValue($values): array
private function filterHeaderValue(mixed $values): array
{
if (! is_array($values)) {
$values = [$values];
Expand Down
4 changes: 3 additions & 1 deletion src/PhpInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Laminas\Diactoros;

use Stringable;

use function stream_get_contents;

/**
* Caching version of php://input
*/
class PhpInputStream extends Stream
class PhpInputStream extends Stream implements Stringable
{
private string $cache = '';

Expand Down
10 changes: 4 additions & 6 deletions src/RelativeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Laminas\Diactoros;

use Psr\Http\Message\StreamInterface;
use Stringable;

use const SEEK_SET;

Expand All @@ -14,16 +15,13 @@
*
* @see AbstractSerializer::splitStream()
*/
final class RelativeStream implements StreamInterface
final class RelativeStream implements StreamInterface, Stringable
{
private StreamInterface $decoratedStream;

private int $offset;

public function __construct(StreamInterface $decoratedStream, ?int $offset)
public function __construct(private StreamInterface $decoratedStream, ?int $offset)
{
$this->decoratedStream = $decoratedStream;
$this->offset = (int) $offset;
$this->offset = (int) $offset;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Response/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public static function fromArray(array $serializedResponse): Response
}

/**
* @param array $data
* @return mixed
* @throws Exception\DeserializationException
*/
Expand Down
18 changes: 4 additions & 14 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class JsonResponse extends Response
/** @var mixed */
private $payload;

private int $encodingOptions;

/**
* Create a JSON response with the given data.
*
Expand All @@ -71,10 +69,9 @@ public function __construct(
$data,
int $status = 200,
array $headers = [],
int $encodingOptions = self::DEFAULT_JSON_FLAGS
private int $encodingOptions = self::DEFAULT_JSON_FLAGS
) {
$this->setPayload($data);
$this->encodingOptions = $encodingOptions;

$json = $this->jsonEncode($data, $this->encodingOptions);
$body = $this->createBodyFromJson($json);
Expand All @@ -92,10 +89,7 @@ public function getPayload()
return $this->payload;
}

/**
* @param mixed $data
*/
public function withPayload($data): JsonResponse
public function withPayload(mixed $data): JsonResponse
{
$new = clone $this;
$new->setPayload($data);
Expand Down Expand Up @@ -126,10 +120,9 @@ private function createBodyFromJson(string $json): Stream
/**
* Encode the provided data to JSON.
*
* @param mixed $data
* @throws Exception\InvalidArgumentException If unable to encode the $data to JSON.
*/
private function jsonEncode($data, int $encodingOptions): string
private function jsonEncode(mixed $data, int $encodingOptions): string
{
if (is_resource($data)) {
throw new Exception\InvalidArgumentException('Cannot JSON encode resources');
Expand All @@ -151,10 +144,7 @@ private function jsonEncode($data, int $encodingOptions): string
return $json;
}

/**
* @param mixed $data
*/
private function setPayload($data): void
private function setPayload(mixed $data): void
{
if (is_object($data)) {
$data = clone $data;
Expand Down
23 changes: 5 additions & 18 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ class ServerRequest implements ServerRequestInterface

private array $attributes = [];

private array $cookieParams = [];

/** @var null|array|object */
private $parsedBody;

private array $queryParams = [];

private array $serverParams;

private array $uploadedFiles;

/**
Expand All @@ -53,22 +44,22 @@ class ServerRequest implements ServerRequestInterface
* @param null|string $method HTTP method for the request, if any.
* @param string|resource|StreamInterface $body Message body, if any.
* @param array $headers Headers for the message, if any.
* @param array $cookies Cookies for the message, if any.
* @param array $cookieParams Cookies for the message, if any.
* @param array $queryParams Query params for the message, if any.
* @param null|array|object $parsedBody The deserialized body parameters, if any.
* @param string $protocol HTTP protocol version.
* @throws Exception\InvalidArgumentException For any invalid value.
*/
public function __construct(
array $serverParams = [],
private array $serverParams = [],
array $uploadedFiles = [],
$uri = null,
?string $method = null,
$body = 'php://input',
array $headers = [],
array $cookies = [],
array $queryParams = [],
$parsedBody = null,
private array $cookieParams = [],
private array $queryParams = [],
private $parsedBody = null,
string $protocol = '1.1'
) {
$this->validateUploadedFiles($uploadedFiles);
Expand All @@ -78,11 +69,7 @@ public function __construct(
}

$this->initialize($uri, $method, $body, $headers);
$this->serverParams = $serverParams;
$this->uploadedFiles = $uploadedFiles;
$this->cookieParams = $cookies;
$this->queryParams = $queryParams;
$this->parsedBody = $parsedBody;
$this->protocol = $protocol;
}

Expand Down
9 changes: 3 additions & 6 deletions src/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
use function preg_match;
use function preg_replace;
use function sprintf;
use function str_contains;
use function strlen;
use function strpos;
use function strrpos;
use function strtolower;
use function substr;
Expand Down Expand Up @@ -158,7 +158,7 @@ private static function marshalUriFromSapi(array $server, array $headers): Uri

// URI fragment
$fragment = '';
if (strpos($path, '#') !== false) {
if (str_contains($path, '#')) {
[$path, $fragment] = explode('#', $path, 2);
}

Expand Down Expand Up @@ -267,10 +267,7 @@ private static function marshalRequestPath(array $server): string
return $origPathInfo;
}

/**
* @param mixed $https
*/
private static function marshalHttpsValue($https): bool
private static function marshalHttpsValue(mixed $https): bool
{
if (is_bool($https)) {
return $https;
Expand Down
27 changes: 7 additions & 20 deletions src/ServerRequestFilter/FilterUsingXForwardedHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use function filter_var;
use function in_array;
use function is_string;
use function strpos;
use function str_contains;
use function strtolower;

use const FILTER_FLAG_IPV4;
Expand Down Expand Up @@ -41,24 +41,14 @@ final class FilterUsingXForwardedHeaders implements FilterServerRequestInterface
self::HEADER_PROTO,
];

/** @var list<FilterUsingXForwardedHeaders::HEADER_*> */
private array $trustedHeaders;

/** @var list<non-empty-string> */
private array $trustedProxies;

/**
* Only allow construction via named constructors
*
* @param list<non-empty-string> $trustedProxies
* @param list<FilterUsingXForwardedHeaders::HEADER_*> $trustedHeaders
*/
private function __construct(
array $trustedProxies = [],
array $trustedHeaders = []
) {
$this->trustedProxies = $trustedProxies;
$this->trustedHeaders = $trustedHeaders;
private function __construct(private array $trustedProxies = [], private array $trustedHeaders = [])
{
}

public function __invoke(ServerRequestInterface $request): ServerRequestInterface
Expand All @@ -79,7 +69,7 @@ public function __invoke(ServerRequestInterface $request): ServerRequestInterfac
$uri = $originalUri = $request->getUri();
foreach ($this->trustedHeaders as $headerName) {
$header = $request->getHeaderLine($headerName);
if ('' === $header || false !== strpos($header, ',')) {
if ('' === $header || str_contains($header, ',')) {
// Reject empty headers and/or headers with multiple values
continue;
}
Expand Down Expand Up @@ -225,23 +215,20 @@ private static function normalizeProxiesList(array $proxyCIDRList): array
return $proxyCIDRList;
}

/**
* @param mixed $cidr
*/
private static function validateProxyCIDR($cidr): bool
private static function validateProxyCIDR(mixed $cidr): bool
{
if (! is_string($cidr) || '' === $cidr) {
return false;
}

$address = $cidr;
$mask = null;
if (false !== strpos($cidr, '/')) {
if (str_contains($cidr, '/')) {
[$address, $mask] = explode('/', $cidr, 2);
$mask = (int) $mask;
}

if (false !== strpos($address, ':')) {
if (str_contains($address, ':')) {
// is IPV6
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
&& (
Expand Down
8 changes: 4 additions & 4 deletions src/ServerRequestFilter/IPRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use function ip2long;
use function pack;
use function sprintf;
use function str_contains;
use function str_pad;
use function str_repeat;
use function strpos;
use function substr_compare;
use function unpack;

Expand All @@ -29,7 +29,7 @@ private function __construct()
/** @psalm-pure */
public static function matches(string $ip, string $cidr): bool
{
if (false !== strpos($ip, ':')) {
if (str_contains($ip, ':')) {
return self::matchesIPv6($ip, $cidr);
}

Expand All @@ -42,7 +42,7 @@ public static function matchesIPv4(string $ip, string $cidr): bool
$mask = 32;
$subnet = $cidr;

if (false !== strpos($cidr, '/')) {
if (str_contains($cidr, '/')) {
[$subnet, $mask] = explode('/', $cidr, 2);
$mask = (int) $mask;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public static function matchesIPv6(string $ip, string $cidr): bool
$mask = 128;
$subnet = $cidr;

if (false !== strpos($cidr, '/')) {
if (str_contains($cidr, '/')) {
[$subnet, $mask] = explode('/', $cidr, 2);
$mask = (int) $mask;
}
Expand Down
Loading