Skip to content

Commit

Permalink
Apply PHP 7.4 syntax and typed property
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 28, 2022
1 parent de9cd9c commit 584a620
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 124 deletions.
6 changes: 3 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@
<code>$https</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="2">
<code>function ($host) {</code>
<code>function (string $name, array $headers, $default = null) {</code>
<code>static function ($host) {</code>
<code>static function (string $name, array $headers, $default = null) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="8">
<code>$getHeaderFromArray('x-forwarded-proto', $headers, '')</code>
Expand Down Expand Up @@ -493,7 +493,7 @@
</file>
<file src="test/Request/SerializerTest.php">
<MissingClosureReturnType occurrences="1">
<code>function () use ($payload) {</code>
<code>static function () use ($payload) {</code>
</MissingClosureReturnType>
<MixedArrayOffset occurrences="1">
<code>$payload[$i++]</code>
Expand Down
2 changes: 1 addition & 1 deletion src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private function filterHeaderValue($values): array
);
}

return array_map(function ($value) {
return array_map(static function ($value): string {
HeaderSecurity::assertValid($value);

$value = (string) $value;
Expand Down
6 changes: 2 additions & 4 deletions src/PhpInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
*/
class PhpInputStream extends Stream
{
/** @var string */
private $cache = '';
private string $cache = '';

/** @var bool */
private $reachedEof = false;
private bool $reachedEof = false;

/**
* @param string|resource $stream
Expand Down
6 changes: 2 additions & 4 deletions src/RelativeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
final class RelativeStream implements StreamInterface
{
/** @var StreamInterface */
private $decoratedStream;
private StreamInterface $decoratedStream;

/** @var int */
private $offset;
private int $offset;

public function __construct(StreamInterface $decoratedStream, ?int $offset)
{
Expand Down
9 changes: 3 additions & 6 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class Response implements ResponseInterface
/**
* Map of standard HTTP status code/reason phrases
*
* @var array
* @psalm-var array<positive-int, non-empty-string>
*/
private $phrases = [
private array $phrases = [
// INFORMATIONAL CODES
100 => 'Continue',
101 => 'Switching Protocols',
Expand Down Expand Up @@ -110,11 +109,9 @@ class Response implements ResponseInterface
599 => 'Network Connect Timeout Error',
];

/** @var string */
private $reasonPhrase;
private string $reasonPhrase;

/** @var int */
private $statusCode;
private int $statusCode;

/**
* @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
Expand Down
8 changes: 5 additions & 3 deletions src/Response/InjectContentTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ trait InjectContentTypeTrait
*/
private function injectContentType(string $contentType, array $headers): array
{
$hasContentType = array_reduce(array_keys($headers), function ($carry, $item) {
return $carry ?: strtolower($item) === 'content-type';
}, false);
$hasContentType = array_reduce(
array_keys($headers),
static fn($carry, $item) => $carry ?: strtolower($item) === 'content-type',
false
);

if (! $hasContentType) {
$headers['content-type'] = [$contentType];
Expand Down
3 changes: 1 addition & 2 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class JsonResponse extends Response
/** @var mixed */
private $payload;

/** @var int */
private $encodingOptions;
private int $encodingOptions;

/**
* Create a JSON response with the given data.
Expand Down
15 changes: 5 additions & 10 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,18 @@ class ServerRequest implements ServerRequestInterface
{
use RequestTrait;

/** @var array */
private $attributes = [];
private array $attributes = [];

/** @var array */
private $cookieParams = [];
private array $cookieParams = [];

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

/** @var array */
private $queryParams = [];
private array $queryParams = [];

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

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

/**
* @param array $serverParams Server parameters, typically from $_SERVER
Expand Down
4 changes: 2 additions & 2 deletions src/ServerRequestFilter/FilterUsingXForwardedHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ final class FilterUsingXForwardedHeaders implements FilterServerRequestInterface
];

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

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

/**
* Only allow construction via named constructors
Expand Down
18 changes: 6 additions & 12 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,17 @@ class UploadedFile implements UploadedFileInterface
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
];

/** @var string|null */
private $clientFilename;
private ?string $clientFilename;

/** @var string|null */
private $clientMediaType;
private ?string $clientMediaType;

/** @var int */
private $error;
private int $error;

/** @var null|string */
private $file;
private ?string $file = null;

/** @var bool */
private $moved = false;
private bool $moved = false;

/** @var int */
private $size;
private int $size;

/** @var null|StreamInterface */
private $stream;
Expand Down
22 changes: 7 additions & 15 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,25 @@ class Uri implements UriInterface
'https' => 443,
];

/** @var string */
private $scheme = '';
private string $scheme = '';

/** @var string */
private $userInfo = '';
private string $userInfo = '';

/** @var string */
private $host = '';
private string $host = '';

/** @var int|null */
private $port;

/** @var string */
private $path = '';
private string $path = '';

/** @var string */
private $query = '';
private string $query = '';

/** @var string */
private $fragment = '';
private string $fragment = '';

/**
* generated uri string cache
*
* @var string|null
*/
private $uriString;
private ?string $uriString = null;

public function __construct(string $uri = '')
{
Expand Down
4 changes: 1 addition & 3 deletions src/functions/marshal_headers_from_sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ function marshalHeadersFromSapi(array $server): array
];
return isset($contentHeaders[$key]);
}
: static function (string $key): bool {
return strpos($key, 'CONTENT_') === 0;
};
: static fn(string $key): bool => strpos($key, 'CONTENT_') === 0;

$headers = [];
foreach ($server as $key => $value) {
Expand Down
26 changes: 13 additions & 13 deletions src/functions/marshal_uri_from_sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function marshalUriFromSapi(array $server, array $headers): Uri
* @param mixed $default Default value to return if header not found
* @return mixed
*/
$getHeaderFromArray = function (string $name, array $headers, $default = null) {
$getHeaderFromArray = static function (string $name, array $headers, $default = null) {
$header = strtolower($name);
$headers = array_change_key_case($headers, CASE_LOWER);
if (array_key_exists($header, $headers)) {
Expand All @@ -58,13 +58,13 @@ function marshalUriFromSapi(array $server, array $headers): Uri
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalHostAndPort = function (array $headers, array $server) use ($getHeaderFromArray): array {
$marshalHostAndPort = static function (array $headers, array $server) use ($getHeaderFromArray): array {
/**
* @param string|array $host
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalHostAndPortFromHeader = function ($host) {
* @param string|array $host
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalHostAndPortFromHeader = static function ($host) {
if (is_array($host)) {
$host = implode(', ', $host);
}
Expand All @@ -81,10 +81,10 @@ function marshalUriFromSapi(array $server, array $headers): Uri
};

/**
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalIpv6HostAndPort = function (array $server, ?int $port): array {
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalIpv6HostAndPort = static function (array $server, ?int $port): array {
$host = '[' . $server['SERVER_ADDR'] . ']';
$port = $port ?: 80;
if ($port . ']' === substr($host, strrpos($host, ':') + 1)) {
Expand Down Expand Up @@ -138,7 +138,7 @@ function marshalUriFromSapi(array $server, array $headers): Uri
*
* From Laminas\Http\PhpEnvironment\Request class
*/
$marshalRequestPath = function (array $server): string {
$marshalRequestPath = static function (array $server): string {
// IIS7 with URL Rewrite: make sure we get the unencoded url
// (double slash problem).
$iisUrlRewritten = $server['IIS_WasUrlRewritten'] ?? null;
Expand All @@ -165,7 +165,7 @@ function marshalUriFromSapi(array $server, array $headers): Uri

// URI scheme
$scheme = 'http';
$marshalHttpsValue = function ($https): bool {
$marshalHttpsValue = static function ($https): bool {
if (is_bool($https)) {
return $https;
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/normalize_uploaded_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function normalizeUploadedFiles(array $files): array
* @param string[]|array[]|null $typeTree
* @return UploadedFile[]|array[]
*/
$recursiveNormalize = function (
$recursiveNormalize = static function (
array $tmpNameTree,
array $sizeTree,
array $errorTree,
Expand Down Expand Up @@ -75,7 +75,7 @@ function normalizeUploadedFiles(array $files): array
* @param array $files
* @return UploadedFile[]
*/
$normalizeUploadedFileSpecification = function (array $files = []) use (&$recursiveNormalize): array {
$normalizeUploadedFileSpecification = static function (array $files = []) use (&$recursiveNormalize): array {
if (
! isset($files['tmp_name']) || ! is_array($files['tmp_name'])
|| ! isset($files['size']) || ! is_array($files['size'])
Expand Down
Loading

0 comments on commit 584a620

Please sign in to comment.