Skip to content

Commit ed5b52f

Browse files
author
Daniyal Hamid
committed
Updated type hints and tests + Refactoring
1 parent b306b53 commit ed5b52f

File tree

11 files changed

+57
-59
lines changed

11 files changed

+57
-59
lines changed

src/Emitter/AbstractSapiEmitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract public function emit(ResponseInterface $response): void;
3131
*/
3232
public function process(
3333
ServerRequestInterface $request,
34-
RequestHandlerInterface $handler
34+
RequestHandlerInterface $handler,
3535
): ResponseInterface {
3636
$response = $handler->handle($request);
3737
$this->emit($response);

src/Factory/HttpFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function addFactory(object|string $factory): void
6767

6868
public static function createResponse(
6969
int $statusCode = 200,
70-
string $reasonPhrase = ''
70+
string $reasonPhrase = '',
7171
): ResponseInterface {
7272
$factory = self::getFactory();
7373
return $factory->createResponse($statusCode, $reasonPhrase);
@@ -79,7 +79,7 @@ public static function createResponse(
7979
*
8080
* @return RequestInterface
8181
*/
82-
public static function createRequest(string $method, $uri): RequestInterface
82+
public static function createRequest(string $method, UriInterface|string $uri): RequestInterface
8383
{
8484
return self::getFactory()->createRequest($method, $uri);
8585
}
@@ -94,7 +94,7 @@ public static function createRequest(string $method, $uri): RequestInterface
9494
public static function createServerRequest(
9595
string $method,
9696
$uri,
97-
array $serverParams = []
97+
array $serverParams = [],
9898
): ServerRequestInterface {
9999
return self::getFactory()->createServerRequest($method, $uri, $serverParams);
100100
}
@@ -113,7 +113,7 @@ public static function createServerRequestFromGlobals(
113113
array $parsedBody = [],
114114
array $cookies = [],
115115
array $files = [],
116-
$body = ''
116+
$body = '',
117117
): ServerRequestInterface {
118118
$factory = self::getFactory();
119119

@@ -134,7 +134,7 @@ public static function createStream(string $content = ''): StreamInterface
134134

135135
public static function createStreamFromFile(
136136
string $filename,
137-
string $mode = 'r'
137+
string $mode = 'r',
138138
): StreamInterface {
139139
return self::getFactory()->createStreamFromFile($filename, $mode);
140140
}
@@ -159,7 +159,7 @@ public static function createUploadedFile(
159159
?int $size = null,
160160
int $error = UPLOAD_ERR_OK,
161161
?string $clientFilename = null,
162-
?string $clientMediaType = null
162+
?string $clientMediaType = null,
163163
): UploadedFileInterface {
164164
return self::getFactory()->createUploadedFile(
165165
$stream,

src/Http/Message/DownloadResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DownloadResponse extends AbstractFileResponse
3737
*/
3838
public static function fromPath(
3939
string $filePath,
40-
string $serveFilenameAs = ''
40+
string $serveFilenameAs = '',
4141
): self {
4242
return new self($filePath, $serveFilenameAs);
4343
}
@@ -66,7 +66,7 @@ public static function fromResource($resource, string $serveFilenameAs = ''): se
6666
*/
6767
public static function fromStream(
6868
StreamInterface $stream,
69-
string $serveFilenameAs = ''
69+
string $serveFilenameAs = '',
7070
): self {
7171
return new self($stream, $serveFilenameAs);
7272
}

src/Http/Message/JsonResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class JsonResponse extends ResponseDecorator
4343
public static function create(
4444
mixed $data = [],
4545
int $encodingOptions = 0,
46-
int $maxDepth = 512
46+
int $maxDepth = 512,
4747
): self {
4848
return new self($data, $encodingOptions, $maxDepth);
4949
}
@@ -56,7 +56,7 @@ public static function create(
5656
public function __construct(
5757
mixed $data = [],
5858
int $encodingOptions = 0,
59-
int $maxDepth = 512
59+
int $maxDepth = 512,
6060
) {
6161
$encodingOptions |= JSON_THROW_ON_ERROR
6262
| JSON_HEX_QUOT

src/Http/Message/JsonpResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function create(
4949
$data,
5050
string $callback,
5151
int $encodingOptions = 0,
52-
int $maxDepth = 512
52+
int $maxDepth = 512,
5353
): self {
5454
return new self($data, $callback, $encodingOptions, $maxDepth);
5555
}
@@ -67,7 +67,7 @@ public function __construct(
6767
$data,
6868
string $callback,
6969
int $encodingOptions = 0,
70-
int $maxDepth = 512
70+
int $maxDepth = 512,
7171
) {
7272
if (empty($callback)) {
7373
throw new InvalidArgumentException('Callback cannot be empty');

src/Http/MiddlewareDecoratorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct(callable $middleware)
9595

9696
public function process(
9797
ServerRequestInterface $request,
98-
RequestHandlerInterface $handler
98+
RequestHandlerInterface $handler,
9999
): ResponseInterface {
100100
return ($this->middleware)($request, $handler);
101101
}

src/Http/ServerRequestBuilder.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace BitFrame\Http;
1414

15-
use BitFrame\Factory\HttpFactory;
15+
use BitFrame\Factory\{PSR17FactoryInterface, HttpFactory};
1616
use BitFrame\Parser\MediaParserNegotiator;
1717
use Psr\Http\Message\{
1818
ServerRequestFactoryInterface,
@@ -63,11 +63,11 @@ class ServerRequestBuilder
6363

6464
public static function fromSapi(
6565
array $server,
66-
ServerRequestFactoryInterface|StreamFactoryInterface|UploadedFileFactoryInterface|UriFactoryInterface $factory,
66+
object $factory,
6767
?array $parsedBody = null,
6868
array $cookies = [],
6969
array $files = [],
70-
$body = ''
70+
$body = '',
7171
): ServerRequestInterface {
7272
$builder = new self($server, $factory);
7373

@@ -85,10 +85,7 @@ public static function fromSapi(
8585

8686
public function __construct(
8787
private array $server,
88-
private ServerRequestFactoryInterface
89-
|StreamFactoryInterface
90-
|UploadedFileFactoryInterface
91-
|UriFactoryInterface $factory
88+
private object $factory,
9289
) {
9390
$this->request = (HttpFactory::isPsr17Factory($factory))
9491
? $factory->createServerRequest('GET', '/', $server)

src/Parser/MediaParserNegotiator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class MediaParserNegotiator implements MediaParserInterface
4343
private ?MediaParserInterface $activeParser = null;
4444

4545
public function __construct(private ServerRequestInterface $request)
46-
{}
46+
{
47+
}
4748

4849
public function add(string $type, string $parser): void
4950
{

src/Router/AbstractRouter.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ abstract class AbstractRouter
3636
/**
3737
* Add a route to the map.
3838
*
39-
* @param string|string[] $methods
39+
* @param array|string $methods
4040
* @param string $path
4141
* @param callable|string|array|MiddlewareInterface $handler
4242
*/
4343
abstract public function map(
4444
array|string $methods,
4545
string $path,
46-
callable|string|array|MiddlewareInterface $handler
46+
callable|string|array|MiddlewareInterface $handler,
4747
);
4848

4949
/**
5050
* Add a route to the map using $middleware.
5151
*
52-
* @param string|string[] $methods
52+
* @param string|array $methods
5353
* @param array|string|callable|MiddlewareInterface $middleware
5454
* @param string $path
5555
* @param callable|string|array $handler
@@ -58,7 +58,7 @@ public function use(
5858
array|string $methods,
5959
array|string|callable|MiddlewareInterface $middleware,
6060
string $path,
61-
callable|string|array $handler
61+
callable|string|array $handler,
6262
): void {
6363
$middlewares = $this->getUnpackedMiddleware($middleware);
6464
$middlewares[] = $this->getDecoratedMiddleware($handler);
@@ -73,7 +73,7 @@ public function __construct(array $middlewares)
7373

7474
public function process(
7575
ServerRequestInterface $request,
76-
RequestHandlerInterface $handler
76+
RequestHandlerInterface $handler,
7777
): ResponseInterface {
7878
foreach ($this->middlewares as $middleware) {
7979
$middleware->process($request, $handler);
@@ -182,7 +182,7 @@ public function any(string $path, callable|string|array $handler): void
182182
/**
183183
* Add a route that sends a text response.
184184
*
185-
* @param string[]|string $methods
185+
* @param array|string $methods
186186
* @param string $route
187187
* @param string $text
188188
* @param int $statusCode
@@ -199,7 +199,7 @@ public function text(array|string $methods, string $route, string $text, int $st
199199
/**
200200
* Add a route that sends HTML response.
201201
*
202-
* @param string[]|string $methods
202+
* @param array|string $methods
203203
* @param string $route
204204
* @param string $html
205205
* @param int $statusCode
@@ -216,7 +216,7 @@ public function html(array|string $methods, string $route, string $html, int $st
216216
/**
217217
* Add a route that sends a JSON response.
218218
*
219-
* @param string[]|string $methods
219+
* @param array|string $methods
220220
* @param string $route
221221
* @param array $data
222222
* @param int $statusCode
@@ -233,7 +233,7 @@ public function json(array|string $methods, string $route, array $data, int $sta
233233
/**
234234
* Add a route that sends a JSONP response.
235235
*
236-
* @param string[]|string $methods
236+
* @param array|string $methods
237237
* @param string $route
238238
* @param array $data
239239
* @param string $callback
@@ -244,7 +244,7 @@ public function jsonp(
244244
string $route,
245245
array $data,
246246
string $callback,
247-
int $statusCode = 200
247+
int $statusCode = 200,
248248
): void {
249249
$this->map(
250250
(array) $methods,
@@ -256,7 +256,7 @@ public function jsonp(
256256
/**
257257
* Add a route that sends XML response.
258258
*
259-
* @param string[]|string $methods
259+
* @param array|string $methods
260260
* @param string $route
261261
* @param string $xml
262262
* @param int $statusCode
@@ -295,7 +295,7 @@ public function file(string $route, string $filePath): void
295295
public function download(
296296
string $route,
297297
string $downloadUrl,
298-
string $serveFilenameAs = ''
298+
string $serveFilenameAs = '',
299299
): void {
300300
$this->map(
301301
['GET'],

src/Router/RouteGroup.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace BitFrame\Router;
1414

15+
use Psr\Http\Server\MiddlewareInterface;
16+
1517
use function ltrim;
1618
use function substr;
1719

@@ -20,30 +22,28 @@
2022
*/
2123
class RouteGroup extends AbstractRouter
2224
{
23-
protected string $prefix;
24-
25-
/** @var callable */
26-
protected $handler;
27-
28-
protected AbstractRouter $routeMapper;
29-
25+
/**
26+
* @param string $prefix
27+
* @param callable $handler
28+
* @param AbstractRouter $routeMapper
29+
*/
3030
public function __construct(
31-
string $prefix,
32-
callable $handler,
33-
AbstractRouter $routeMapper
31+
protected string $prefix,
32+
protected $handler,
33+
protected AbstractRouter $routeMapper
3434
) {
3535
$this->prefix = '/' . ltrim($prefix, '/');
36-
$this->handler = $handler;
37-
$this->routeMapper = $routeMapper;
38-
3936
($this->handler)($this);
4037
}
4138

4239
/**
4340
* {@inheritdoc}
4441
*/
45-
public function map($methods, string $path, $handler)
46-
{
42+
public function map(
43+
array|string $methods,
44+
string $path,
45+
callable|string|array|MiddlewareInterface $handler,
46+
): void {
4747
if ($path === '' || $path === '/') {
4848
$path = '';
4949
} else {

0 commit comments

Comments
 (0)