From 858eabf6917c81a22eb0dada3b9e9702c7cdec4c Mon Sep 17 00:00:00 2001 From: oscarotero Date: Tue, 1 Mar 2016 12:11:08 +0100 Subject: [PATCH] code styles --- src/Middleware/Csrf.php | 3 +- src/Middleware/FormTimestamp.php | 1 + src/Middleware/Geolocate.php | 2 +- src/Middleware/Honeypot.php | 1 + src/Middleware/Whoops.php | 74 ++++++++++++++++---------------- tests/LazyLoadTest.php | 2 +- tests/SessionStorageTest.php | 12 +++--- tests/UuidTest.php | 4 ++ 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/Middleware/Csrf.php b/src/Middleware/Csrf.php index cb017c5..7e13114 100644 --- a/src/Middleware/Csrf.php +++ b/src/Middleware/Csrf.php @@ -69,7 +69,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res return $next($request, $response); } - $tokens =& self::getStorage($request, self::KEY); + $tokens = &self::getStorage($request, self::KEY); if (Utils\Helpers::isPost($request) && !$this->validateRequest($request, $tokens)) { return $response->withStatus(403); @@ -85,6 +85,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res if (!$this->autoInsert) { $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); + return $next($request, $response); } diff --git a/src/Middleware/FormTimestamp.php b/src/Middleware/FormTimestamp.php index b731d40..bd15275 100644 --- a/src/Middleware/FormTimestamp.php +++ b/src/Middleware/FormTimestamp.php @@ -119,6 +119,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res if (!$this->autoInsert) { $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); + return $next($request, $response); } diff --git a/src/Middleware/Geolocate.php b/src/Middleware/Geolocate.php index 435b1e0..3380432 100644 --- a/src/Middleware/Geolocate.php +++ b/src/Middleware/Geolocate.php @@ -91,7 +91,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res if ($ip !== null) { if ($this->saveInSession) { - $ips =& self::getStorage($request, self::KEY); + $ips = &self::getStorage($request, self::KEY); if (isset($ips[$ip])) { $address = new AddressCollection($ips[$ip]); diff --git a/src/Middleware/Honeypot.php b/src/Middleware/Honeypot.php index f674595..3364cdb 100644 --- a/src/Middleware/Honeypot.php +++ b/src/Middleware/Honeypot.php @@ -96,6 +96,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res if (!$this->autoInsert) { $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); + return $next($request, $response); } diff --git a/src/Middleware/Whoops.php b/src/Middleware/Whoops.php index a9f68b5..647e144 100644 --- a/src/Middleware/Whoops.php +++ b/src/Middleware/Whoops.php @@ -19,7 +19,7 @@ class Whoops use Utils\StreamTrait; /** - * @var Run|null To handle errors using whoops + * @var Run|null The provided instance of Whoops */ private $whoops; @@ -61,43 +61,43 @@ public function catchErrors($catchErrors = true) * * @return ResponseInterface */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) - { - ob_start(); - $level = ob_get_level(); - - $method = Run::EXCEPTION_HANDLER; - $whoops = $this->getWhoopsInstance($request); - - $whoops->allowQuit(false); - $whoops->writeToOutput(false); - $whoops->sendHttpCode(false); - - //Catch errors means register whoops globally - if ($this->catchErrors) { - $whoops->register(); - } - - try { - $response = $next($request, $response); - } catch (\Throwable $exception) { - $body = self::createStream(); - $body->write($whoops->$method($exception)); - $response = $response->withStatus(500)->withBody($body); + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) + { + ob_start(); + $level = ob_get_level(); + + $method = Run::EXCEPTION_HANDLER; + $whoops = $this->getWhoopsInstance($request); + + $whoops->allowQuit(false); + $whoops->writeToOutput(false); + $whoops->sendHttpCode(false); + + //Catch errors means register whoops globally + if ($this->catchErrors) { + $whoops->register(); + } + + try { + $response = $next($request, $response); + } catch (\Throwable $exception) { + $body = self::createStream(); + $body->write($whoops->$method($exception)); + $response = $response->withStatus(500)->withBody($body); } catch (\Exception $exception) { - $body = self::createStream(); - $body->write($whoops->$method($exception)); - $response = $response->withStatus(500)->withBody($body); - } finally { - Utils\Helpers::getOutput($level); - } - - if ($this->catchErrors) { - $whoops->unregister(); - } - - return $response; - } + $body = self::createStream(); + $body->write($whoops->$method($exception)); + $response = $response->withStatus(500)->withBody($body); + } finally { + Utils\Helpers::getOutput($level); + } + + if ($this->catchErrors) { + $whoops->unregister(); + } + + return $response; + } /** * Returns the whoops instance or create one. diff --git a/tests/LazyLoadTest.php b/tests/LazyLoadTest.php index c01fa00..56c1844 100644 --- a/tests/LazyLoadTest.php +++ b/tests/LazyLoadTest.php @@ -89,7 +89,7 @@ function ($request, $response, $next) { ); $this->assertEquals($url, (string) $response->getBody()); - + if ($passed) { $this->assertTrue($response->hasHeader('X-Response-Time')); } else { diff --git a/tests/SessionStorageTest.php b/tests/SessionStorageTest.php index 593c083..5899bf5 100644 --- a/tests/SessionStorageTest.php +++ b/tests/SessionStorageTest.php @@ -32,7 +32,8 @@ public function testAuraSessionStorage() } } -class Middleware1 { +class Middleware1 +{ use Utils\StorageTrait; public function __invoke($request, $response, $next) @@ -46,14 +47,15 @@ public function __invoke($request, $response, $next) } } -class Middleware2 { +class Middleware2 +{ use Utils\StorageTrait; public function __invoke($request, $response, $next) { - $storage =& self::getStorage($request, 'test'); + $storage = &self::getStorage($request, 'test'); $storage['value'] = 'Hello'; - + return $next($request, $response); } -} \ No newline at end of file +} diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 2779e6e..b453440 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -14,6 +14,7 @@ public function testUuid1() Middleware::Uuid(), function ($request, $response, $next) { $response->getBody()->write($request->getHeaderLine('X-Uuid')); + return $next($request, $response); }, ] @@ -30,6 +31,7 @@ public function testUuid3() Middleware::Uuid(3, Uuid::NAMESPACE_DNS, 'oscarotero.com'), function ($request, $response, $next) { $response->getBody()->write($request->getHeaderLine('X-Uuid')); + return $next($request, $response); }, ] @@ -46,6 +48,7 @@ public function testUuid4() Middleware::Uuid(4), function ($request, $response, $next) { $response->getBody()->write($request->getHeaderLine('X-Uuid')); + return $next($request, $response); }, ] @@ -62,6 +65,7 @@ public function testUuid5() Middleware::Uuid(5, Uuid::NAMESPACE_DNS, 'oscarotero.com'), function ($request, $response, $next) { $response->getBody()->write($request->getHeaderLine('X-Uuid')); + return $next($request, $response); }, ]