Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Apr 15, 2020
1 parent 05585b8 commit d47543c
Show file tree
Hide file tree
Showing 34 changed files with 382 additions and 317 deletions.
16 changes: 9 additions & 7 deletions src/EventListener/ExceptionConversionListener.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\EventListener;

use Jsor\Stack\Hal\ExceptionConverter;
Expand All @@ -9,7 +11,7 @@
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ExceptionConversionListener implements EventSubscriberInterface
final class ExceptionConversionListener implements EventSubscriberInterface
{
private $logger;
private $prettyPrint;
Expand All @@ -18,17 +20,17 @@ class ExceptionConversionListener implements EventSubscriberInterface

public function __construct(
LoggerInterface $logger = null,
$prettyPrint = true,
$debug = false,
bool $prettyPrint = true,
bool $debug = false,
array $formats = null
) {
$this->logger = $logger;
$this->prettyPrint = (bool) $prettyPrint;
$this->debug = (bool) $debug;
$this->prettyPrint = $prettyPrint;
$this->debug = $debug;
$this->formats = $formats;
}

public function onKernelException(ExceptionEvent $event)
public function onKernelException(ExceptionEvent $event): void
{
$response = ExceptionConverter::handleThrowable(
$event->getThrowable(),
Expand All @@ -44,7 +46,7 @@ public function onKernelException(ExceptionEvent $event)
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
Expand Down
8 changes: 5 additions & 3 deletions src/EventListener/RequestFormatNegotiationListener.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\EventListener;

use Jsor\Stack\Hal\RequestFormatNegotiator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class RequestFormatNegotiationListener implements EventSubscriberInterface
final class RequestFormatNegotiationListener implements EventSubscriberInterface
{
private $formats;
private $priorities;
Expand All @@ -20,7 +22,7 @@ public function __construct(
$this->priorities = $priorities;
}

public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
RequestFormatNegotiator::negotiate(
$event->getRequest(),
Expand All @@ -29,7 +31,7 @@ public function onKernelRequest(RequestEvent $event)
);
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
Expand Down
8 changes: 5 additions & 3 deletions src/EventListener/RequestFormatValidationListener.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\EventListener;

use Jsor\Stack\Hal\RequestFormatValidator;
Expand All @@ -8,7 +10,7 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class RequestFormatValidationListener implements EventSubscriberInterface
final class RequestFormatValidationListener implements EventSubscriberInterface
{
private $acceptableFormats;
private $exclude;
Expand All @@ -21,7 +23,7 @@ public function __construct(
$this->exclude = $exclude;
}

public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
$response = RequestFormatValidator::intercept(
$event->getRequest(),
Expand All @@ -34,7 +36,7 @@ public function onKernelRequest(RequestEvent $event)
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
Expand Down
8 changes: 5 additions & 3 deletions src/EventListener/ResponseConversionListener.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\EventListener;

use Jsor\Stack\Hal\Response\HalResponse;
Expand All @@ -8,7 +10,7 @@
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ResponseConversionListener implements EventSubscriberInterface
final class ResponseConversionListener implements EventSubscriberInterface
{
private $prettyPrint;

Expand All @@ -17,7 +19,7 @@ public function __construct($prettyPrint = true)
$this->prettyPrint = (bool) $prettyPrint;
}

public function onKernelView(ViewEvent $event)
public function onKernelView(ViewEvent $event): void
{
$hal = $event->getControllerResult();

Expand All @@ -28,7 +30,7 @@ public function onKernelView(ViewEvent $event)
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => 'onKernelView',
Expand Down
8 changes: 5 additions & 3 deletions src/Exception/ErrorException.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\Exception;

use Nocarrier\Hal;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class ErrorException extends BadRequestHttpException implements HalException
final class ErrorException extends BadRequestHttpException implements HalException
{
private $errors;
private $logref;
Expand All @@ -23,7 +25,7 @@ public function __construct(
$this->logref = $logref;
}

public function getHal()
public function getHal(): Hal
{
$data = [
'message' => $this->getMessage(),
Expand All @@ -40,7 +42,7 @@ public function getHal()
return $hal;
}

private function appendErrors(Hal $hal, array $errors)
private function appendErrors(Hal $hal, array $errors): void
{
foreach ($errors as $error) {
if (!\is_array($error)) {
Expand Down
12 changes: 7 additions & 5 deletions src/Exception/FormErrorException.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\Exception;

use Nocarrier\Hal;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class FormErrorException extends BadRequestHttpException implements HalException
final class FormErrorException extends BadRequestHttpException implements HalException
{
private $form;
private $logref;
Expand All @@ -25,7 +27,7 @@ public function __construct(
$this->logref = $logref;
}

public function getHal()
public function getHal(): Hal
{
$data = [
'message' => $this->getMessage(),
Expand All @@ -42,7 +44,7 @@ public function getHal()
return $hal;
}

private function appendErrors(Hal $hal, FormInterface $form)
private function appendErrors(Hal $hal, FormInterface $form): void
{
$formPath = null;

Expand All @@ -54,7 +56,7 @@ private function appendErrors(Hal $hal, FormInterface $form)

$origin = $error->getOrigin();

if ($origin) {
if ($origin !== null) {
$currPath = $this->getPath($origin);
} else {
if (null === $formPath) {
Expand All @@ -76,7 +78,7 @@ private function appendErrors(Hal $hal, FormInterface $form)
}
}

private function getPath(FormInterface $form)
private function getPath(FormInterface $form): string
{
$path = [];

Expand Down
7 changes: 3 additions & 4 deletions src/Exception/HalException.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\Exception;

use Nocarrier\Hal;

interface HalException
{
/**
* @return Hal
*/
public function getHal();
public function getHal(): Hal;
}
6 changes: 4 additions & 2 deletions src/Exception/ValidationErrorException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal\Exception;

use Nocarrier\Hal;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\ConstraintViolationListInterface;

class ValidationErrorException extends BadRequestHttpException implements HalException
final class ValidationErrorException extends BadRequestHttpException implements HalException
{
private $violationList;
private $logref;
Expand All @@ -24,7 +26,7 @@ public function __construct(
$this->logref = $logref;
}

public function getHal()
public function getHal(): Hal
{
$data = [
'message' => $this->getMessage(),
Expand Down
26 changes: 14 additions & 12 deletions src/ExceptionConverter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Jsor\Stack\Hal;

use Jsor\Stack\Hal\Response\VndErrorResponse;
Expand All @@ -14,7 +16,7 @@
*
* @see https://github.com/blongden/vnd.error
*/
class ExceptionConverter implements HttpKernelInterface
final class ExceptionConverter implements HttpKernelInterface
{
private $app;
private $logger;
Expand All @@ -26,16 +28,16 @@ class ExceptionConverter implements HttpKernelInterface
public function __construct(
HttpKernelInterface $app,
LoggerInterface $logger = null,
$prettyPrint = true,
$debug = false,
$passThroughCatch = false,
bool $prettyPrint = true,
bool $debug = false,
bool $passThroughCatch = false,
array $formats = null
) {
$this->app = $app;
$this->logger = $logger;
$this->prettyPrint = (bool) $prettyPrint;
$this->debug = (bool) $debug;
$this->passThroughCatch = (bool) $passThroughCatch;
$this->prettyPrint = $prettyPrint;
$this->debug = $debug;
$this->passThroughCatch = $passThroughCatch;
$this->formats = $formats;
}

Expand Down Expand Up @@ -76,10 +78,10 @@ public static function handleThrowable(
\Throwable $throwable,
Request $request,
LoggerInterface $logger = null,
$prettyPrint = true,
$debug = false,
bool $prettyPrint = true,
bool $debug = false,
array $formats = null
) {
): ?VndErrorResponse {
if (null !== $logger) {
self::logThrowable($logger, $throwable);
}
Expand All @@ -89,7 +91,7 @@ public static function handleThrowable(
$format = $request->getRequestFormat(null);

if (!$format || !\in_array($format, $formats, true)) {
return;
return null;
}

return VndErrorResponse::fromThrowable(
Expand All @@ -102,7 +104,7 @@ public static function handleThrowable(
public static function logThrowable(
LoggerInterface $logger,
\Throwable $throwable
) {
): void {
$message = \sprintf(
'Uncaught PHP Exception %s: "%s" at %s line %s',
\get_class($throwable),
Expand Down
Loading

0 comments on commit d47543c

Please sign in to comment.