Skip to content

Commit 14cddbe

Browse files
committed
(chore) fix CS. Fix failed tests
1 parent b5600b9 commit 14cddbe

File tree

139 files changed

+944
-905
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+944
-905
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
"firebase/php-jwt": "^5.0"
3535
},
3636
"require-dev": {
37-
"phpunit/phpunit": "^8.0",
37+
"cakephp/cakephp-codesniffer": "dev-next",
3838
"league/flysystem-vfs": "^1.0",
39-
"vlucas/phpdotenv": "^3.3",
40-
"cakephp/cakephp-codesniffer": "dev-next"
39+
"phpunit/phpunit": "^8.0",
40+
"vlucas/phpdotenv": "^3.3"
4141
},
4242
"autoload": {
4343
"psr-4": {
@@ -68,6 +68,7 @@
6868
"stan": "phpstan analyse src/ && psalm --show-info=false",
6969
"psalm": "psalm --show-info=false",
7070
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json",
71+
"rector": "rector process src/",
7172
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json",
7273
"coverage-test": "phpunit --stderr --coverage-clover=clover.xml"
7374
}

src/Controller/ApiController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function process()
5757
*
5858
* @return void
5959
*/
60-
public function listing()
60+
public function listing(): void
6161
{
62-
$this->setRequest( $this->getRequest()->withParam('service', 'listing'));
62+
$this->setRequest($this->getRequest()->withParam('service', 'listing'));
6363
$options = [
6464
'className' => 'CakeDC/Api.Listing',
6565
];
@@ -71,9 +71,9 @@ public function listing()
7171
*
7272
* @return void
7373
*/
74-
public function describe()
74+
public function describe(): void
7575
{
76-
$this->setRequest( $this->getRequest()->withParam('service', 'describe'));
76+
$this->setRequest($this->getRequest()->withParam('service', 'describe'));
7777
$options = [
7878
'className' => 'CakeDC/Api.Describe',
7979
];
@@ -86,7 +86,7 @@ public function describe()
8686
* @param array $options Options
8787
* @return \Cake\Http\Client\Response|\Cake\Http\Response|null
8888
*/
89-
protected function _process($options = [])
89+
protected function _process(array $options = []): \Cake\Http\Response
9090
{
9191
$routesInflectorMethod = Configure::read('Api.routesInflectorMethod', 'underscore');
9292

src/Controller/AppController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace CakeDC\Api\Controller;
1515

1616
//use App\Controller\AppController as BaseController;
17+
18+
1719
use Cake\Controller\Controller as BaseController;
1820

1921
/**

src/Exception/MethodNotAllowedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($message = null, $code = 405, $previous = null)
4444
* @param string $file file name
4545
* @return void
4646
*/
47-
public function setFile($file = '')
47+
public function setFile(string $file = ''): void
4848
{
4949
$this->file = $file;
5050
}
@@ -55,7 +55,7 @@ public function setFile($file = '')
5555
* @param int $line set the line of the code
5656
* @return void
5757
*/
58-
public function setLine($line = 0)
58+
public function setLine(int $line = 0): void
5959
{
6060
$this->line = $line;
6161
}

src/Exception/ServiceException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($message = null, $code = 500, $previous = null)
4444
* @param string $file set the file
4545
* @return void
4646
*/
47-
public function setFile($file = '')
47+
public function setFile(string $file = ''): void
4848
{
4949
$this->file = $file;
5050
}
@@ -55,7 +55,7 @@ public function setFile($file = '')
5555
* @param int $line set the line
5656
* @return void
5757
*/
58-
public function setLine($line = 0)
58+
public function setLine(int $line = 0): void
5959
{
6060
$this->line = $line;
6161
}

src/Exception/UnauthenticatedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($message = null, $code = 401, $previous = null)
4444
* @param string $file set file name
4545
* @return void
4646
*/
47-
public function setFile($file = '')
47+
public function setFile(string $file = ''): void
4848
{
4949
$this->file = $file;
5050
}
@@ -55,7 +55,7 @@ public function setFile($file = '')
5555
* @param int $line set the line of the code
5656
* @return void
5757
*/
58-
public function setLine($line = 0)
58+
public function setLine(int $line = 0): void
5959
{
6060
$this->line = $line;
6161
}

src/Exception/UnauthorizedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct($message = null, $code = 403, $previous = null)
4343
* @param string $file file name
4444
* @return void
4545
*/
46-
public function setFile($file = '')
46+
public function setFile(string $file = ''): void
4747
{
4848
$this->file = $file;
4949
}
@@ -54,7 +54,7 @@ public function setFile($file = '')
5454
* @param int $line line of the code
5555
* @return void
5656
*/
57-
public function setLine($line = 0)
57+
public function setLine(int $line = 0): void
5858
{
5959
$this->line = $line;
6060
}

src/Exception/ValidationException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
$message = 'Validation errors',
4040
$code = 0,
4141
$previous = null,
42-
$validationErrors = []
42+
array $validationErrors = []
4343
) {
4444
if ($code === 0) {
4545
$code = $this->_defaultCode;
@@ -54,7 +54,7 @@ public function __construct(
5454
* @param array $validationErrors the array with the validations
5555
* @return void
5656
*/
57-
public function setValidationErrors($validationErrors = [])
57+
public function setValidationErrors(array $validationErrors = []): void
5858
{
5959
$this->_validationErrors = $validationErrors;
6060
}
@@ -64,7 +64,7 @@ public function setValidationErrors($validationErrors = [])
6464
*
6565
* @return array
6666
*/
67-
public function getValidationErrors()
67+
public function getValidationErrors(): array
6868
{
6969
return $this->_validationErrors;
7070
}

src/Middleware/ApiMiddleware.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@
1515

1616
use Cake\Core\Configure;
1717
use Cake\Http\CallbackStream;
18+
use Cake\Http\Response;
1819
use CakeDC\Api\Service\ConfigReader;
1920
use CakeDC\Api\Service\ServiceRegistry;
2021
use Exception;
2122
use Psr\Http\Message\ResponseInterface;
2223
use Psr\Http\Message\ServerRequestInterface;
24+
use Psr\Http\Server\MiddlewareInterface;
25+
use Psr\Http\Server\RequestHandlerInterface;
2326

2427
/**
2528
* Applies routing rules to the request and creates the controller
2629
* instance if possible.
2730
*/
28-
class ApiMiddleware
31+
class ApiMiddleware implements MiddlewareInterface
2932
{
3033
/**
34+
* Process an incoming server request.
35+
*
36+
* Processes an incoming server request in order to produce a response.
37+
* If unable to produce the response itself, it may delegate to the provided
38+
* request handler to do so.
39+
*
3140
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
32-
* @param \Psr\Http\Message\ResponseInterface $response The response.
33-
* @param callable $next The next middleware to call.
41+
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
3442
* @return \Psr\Http\Message\ResponseInterface A response.
3543
*/
36-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
44+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3745
{
3846
$prefix = Configure::read('Api.prefix');
3947
if (empty($prefix)) {
@@ -42,7 +50,8 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
4250
$useVersioning = Configure::read('Api.useVersioning');
4351
if ($useVersioning) {
4452
$versionPrefix = Configure::read('Api.versionPrefix');
45-
$expr = '#/' . $prefix . '/(?<version>' . $versionPrefix . '\d+)' . '/' . '(?<service>[^/?]+)' . '(?<base>/?.*)#';
53+
$expr = '#/' . $prefix . '/(?<version>' . $versionPrefix . '\d+)' . '/' .
54+
'(?<service>[^/?]+)' . '(?<base>/?.*)#';
4655
} else {
4756
$expr = '#/' . $prefix . '/' . '(?<service>[^/?]+)' . '(?<base>/?.*)#';
4857
}
@@ -60,7 +69,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
6069
'service' => $service,
6170
'version' => $version,
6271
'request' => $request,
63-
'response' => $response,
6472
'baseUrl' => $url,
6573
];
6674

@@ -71,15 +79,16 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
7179

7280
$response = $Service->respond($result);
7381
} catch (Exception $e) {
82+
$response = new Response();
7483
$response->withStatus(400);
7584
$response = $response->withBody(new CallbackStream(function () use ($e) {
76-
echo $e->getMessage();
85+
echo $e->getMessage();
7786
}));
7887
}
7988

8089
return $response;
8190
}
8291

83-
return $next($request, $response);
92+
return $handler->handle($request);
8493
}
8594
}

src/Middleware/RequestHandlerMiddleware.php

Lines changed: 3 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -13,115 +13,13 @@
1313

1414
namespace CakeDC\Api\Middleware;
1515

16-
use Cake\Utility\Exception\XmlException;
17-
use Cake\Utility\Xml;
18-
use Psr\Http\Message\ResponseInterface;
19-
use Psr\Http\Message\ServerRequestInterface;
20-
use RuntimeException;
16+
use Cake\Http\Middleware\BodyParserMiddleware;
2117

2218
/**
2319
* Applies routing rules to the request and creates the controller
2420
* instance if possible.
21+
* @deprecated
2522
*/
26-
class RequestHandlerMiddleware
23+
class RequestHandlerMiddleware extends BodyParserMiddleware
2724
{
28-
/**
29-
* Request object
30-
*
31-
* @var \Cake\Http\ServerRequest
32-
*/
33-
public $request;
34-
35-
/**
36-
* Response object
37-
*
38-
* @var \Cake\Http\Response
39-
*/
40-
public $response;
41-
42-
/**
43-
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
44-
* @param \Psr\Http\Message\ResponseInterface $response The response.
45-
* @param callable $next The next middleware to call.
46-
* @return \Psr\Http\Message\ResponseInterface A response.
47-
*/
48-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
49-
{
50-
$inputTypeMap = [
51-
'json' => ['json_decode', true],
52-
'xml' => [[$this, 'convertXml']],
53-
];
54-
55-
$this->request = $request;
56-
$this->response = $response;
57-
$parsedBody = $request->getParsedBody();
58-
59-
foreach ($inputTypeMap as $type => $handler) {
60-
if (!is_callable($handler[0])) {
61-
throw new RuntimeException(sprintf("Invalid callable for '%s' type.", $type));
62-
}
63-
if (empty($parsedBody) && $this->requestedWith($type)) {
64-
$input = call_user_func_array([$this->request, 'input'], $handler);
65-
66-
return $next($request->withParsedBody($input), $response);
67-
}
68-
}
69-
70-
return $next($request, $response);
71-
}
72-
73-
/**
74-
* Determines the content type of the data the client has sent (i.e. in a POST request)
75-
*
76-
* @param string|array|null $type Can be null (or no parameter), a string type name, or an array of types
77-
* @return mixed If a single type is supplied a boolean will be returned. If no type is provided
78-
* The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
79-
* in the request content type will be returned.
80-
*/
81-
public function requestedWith($type = null)
82-
{
83-
$request = $this->request;
84-
if (!$request->is('post') && !$request->is('put') && !$request->is('patch') && !$request->is('delete')) {
85-
return null;
86-
}
87-
if (is_array($type)) {
88-
foreach ($type as $t) {
89-
if ($this->requestedWith($t)) {
90-
return $t;
91-
}
92-
}
93-
94-
return false;
95-
}
96-
97-
[$contentType] = explode(';', $request->contentType());
98-
$response = $this->response;
99-
if ($type === null) {
100-
return $response->mapType($contentType);
101-
}
102-
if (is_string($type)) {
103-
return $type === $response->mapType($contentType);
104-
}
105-
}
106-
107-
/**
108-
* Helper method to parse xml input data, due to lack of anonymous functions
109-
* this lives here.
110-
*
111-
* @param string $xml XML string.
112-
* @return array Xml array data
113-
*/
114-
public function convertXml($xml)
115-
{
116-
try {
117-
$xml = Xml::build($xml, ['readFile' => false]);
118-
if (isset($xml->data)) {
119-
return Xml::toArray($xml->data);
120-
}
121-
122-
return Xml::toArray($xml);
123-
} catch (XmlException $e) {
124-
return [];
125-
}
126-
}
12725
}

0 commit comments

Comments
 (0)