From c856755a15b621ac6e780f4b12f65d2bf37006c6 Mon Sep 17 00:00:00 2001 From: Gawain Lynch Date: Thu, 1 Dec 2016 08:28:12 +0100 Subject: [PATCH] PSR-2, PSR-5, Bolt & Symfony clean up and unused imports --- src/Action/ContentListAction.php | 11 +++--- src/Action/FetchAction.php | 7 ++-- src/Action/MenuAction.php | 10 +++-- src/Action/SearchAction.php | 13 ++++--- src/Action/SingleAction.php | 17 ++++---- src/Config/Config.php | 7 +++- src/Controllers/ContentController.php | 19 ++++----- src/Converter/JSONAPIConverter.php | 11 +++--- .../Parameter/ParameterCollection.php | 6 ++- src/Converter/Parameter/ParameterFactory.php | 8 ++-- .../Parameter/ParameterInterface.php | 6 ++- .../Parameter/Type/AbstractParameter.php | 12 ++++-- src/Converter/Parameter/Type/Contains.php | 3 ++ src/Converter/Parameter/Type/ContentType.php | 4 +- src/Converter/Parameter/Type/Fields.php | 4 +- src/Converter/Parameter/Type/Filters.php | 4 ++ src/Converter/Parameter/Type/Includes.php | 5 +++ src/Converter/Parameter/Type/Order.php | 2 +- src/Converter/Parameter/Type/Page.php | 5 ++- src/Converter/Parameter/Type/Search.php | 6 ++- src/Converter/Parameter/Type/Sort.php | 3 ++ src/Exception/ApiException.php | 12 +++--- src/Exception/ApiInvalidRequestException.php | 6 +-- src/Exception/ApiNotFoundException.php | 6 +-- src/Helpers/DataLinks.php | 39 +++++++++++-------- src/JSONAPIExtension.php | 4 +- src/Parser/Field/FieldCollection.php | 6 +-- src/Parser/Field/FieldFactory.php | 12 +++--- src/Parser/Field/Type/AbstractType.php | 9 +++-- src/Parser/Field/Type/Date.php | 2 +- src/Parser/Field/Type/File.php | 9 +++-- src/Parser/Field/Type/Generic.php | 1 - src/Parser/Field/Type/RepeatingCollection.php | 6 ++- .../Field/Type/RepeatingFieldCollection.php | 2 - src/Parser/Field/Type/Taxonomy.php | 1 - src/Parser/Field/Type/TypeInterface.php | 2 - src/Parser/Parser.php | 14 +++---- src/Provider/APIProvider.php | 8 +++- src/Response/ApiErrorResponse.php | 8 ++-- src/Response/ApiResponse.php | 9 +++-- .../Query/Handler/Directive/PagerHandler.php | 4 +- src/Storage/Query/Handler/PagingHandler.php | 6 +-- src/Storage/Query/PagingResultSet.php | 10 +++-- src/Storage/Repository.php | 7 +++- 44 files changed, 198 insertions(+), 148 deletions(-) diff --git a/src/Action/ContentListAction.php b/src/Action/ContentListAction.php index 0ee6f20..514e22a 100644 --- a/src/Action/ContentListAction.php +++ b/src/Action/ContentListAction.php @@ -1,6 +1,5 @@ get($contentType); - $this->throwErrorOnNoResults($results, "Bad request: There were no results based upon your criteria!"); + $this->throwErrorOnNoResults($results, 'Bad request: There were no results based upon your criteria!'); $this->fetchIncludes( $parameters->getParametersByType('includes'), @@ -58,8 +59,8 @@ public function handle($contentType, Request $request, ParameterCollection $para $request ), 'meta' => [ - "count" => count($items), - "total" => $set->getTotalResults() + 'count' => count($items), + 'total' => $set->getTotalResults(), ], 'data' => $items, ]; diff --git a/src/Action/FetchAction.php b/src/Action/FetchAction.php index 82a70f5..810edaf 100644 --- a/src/Action/FetchAction.php +++ b/src/Action/FetchAction.php @@ -24,10 +24,11 @@ class FetchAction /** * FetchAction constructor. - * @param Query $query - * @param Parser $parser + * + * @param Query $query + * @param Parser $parser * @param DataLinks $dataLinks - * @param Config $config + * @param Config $config */ public function __construct( Query $query, diff --git a/src/Action/MenuAction.php b/src/Action/MenuAction.php index 90df879..eaf0356 100644 --- a/src/Action/MenuAction.php +++ b/src/Action/MenuAction.php @@ -1,6 +1,5 @@ boltConfig->get('menu'.$name, false); + $menu = $this->boltConfig->get('menu' . $name, false); if (! $menu) { throw new ApiNotFoundException( @@ -50,7 +52,7 @@ public function handle(Request $request) } return new ApiResponse([ - 'data' => $menu + 'data' => $menu, ], $this->extensionConfig); } } diff --git a/src/Action/SearchAction.php b/src/Action/SearchAction.php index 70e85a1..393084f 100644 --- a/src/Action/SearchAction.php +++ b/src/Action/SearchAction.php @@ -1,6 +1,5 @@ [ - "count" => count($items), - "total" => $set->getTotalResults() + 'count' => count($items), + 'total' => $set->getTotalResults(), ], 'data' => $items, ], $this->config); diff --git a/src/Action/SingleAction.php b/src/Action/SingleAction.php index d1b481d..ab29d62 100644 --- a/src/Action/SingleAction.php +++ b/src/Action/SingleAction.php @@ -1,6 +1,5 @@ [ 'self' => $this->config->getBasePath() . "/$contentType/$slug/$relatedContentType" . - $this->dataLinks->makeQueryParameters($request->query->all()) + $this->dataLinks->makeQueryParameters($request->query->all()), ], 'meta' => [ - "count" => count($items), - "total" => count($items) + 'count' => count($items), + 'total' => count($items), ], - 'data' => $items + 'data' => $items, ], $this->config); } else { //$allFields = $this->APIHelper->getAllFieldNames($contentType); @@ -81,7 +82,7 @@ public function handle($contentType, $slug, $relatedContentType, Request $reques $defaultQueryString = $this->dataLinks->makeQueryParameters($request->query->all()); $links = [ - 'self' => $values['links']['self'] . $defaultQueryString + 'self' => $values['links']['self'] . $defaultQueryString, ]; // optional: This adds additional relationships links in the root @@ -118,7 +119,7 @@ public function handle($contentType, $slug, $relatedContentType, Request $reques $response = [ 'links' => $links, - 'data' => $values, + 'data' => $values, ]; if (!empty($included)) { diff --git a/src/Config/Config.php b/src/Config/Config.php index 6118ba3..e9b204d 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -7,11 +7,10 @@ class Config { - /** * @var string */ - private $base = "/json"; + private $base = '/json'; /** * @var string @@ -258,6 +257,7 @@ public function setJsonOptions($jsonOptions) /** * @param $contentType + * * @return array */ public function getWhereClauses($contentType) @@ -271,6 +271,7 @@ public function getWhereClauses($contentType) /** * @param $contentType + * * @return array */ public function getListFields($contentType) @@ -284,6 +285,7 @@ public function getListFields($contentType) /** * @param $contentType + * * @return array */ public function getItemFields($contentType) @@ -297,6 +299,7 @@ public function getItemFields($contentType) /** * @param $contentType + * * @return array */ public function getAllowedFields($contentType) diff --git a/src/Controllers/ContentController.php b/src/Controllers/ContentController.php index 235979f..015e64f 100644 --- a/src/Controllers/ContentController.php +++ b/src/Controllers/ContentController.php @@ -1,20 +1,14 @@ get("/menu", [$app['jsonapi.action.menu'], "handle"]) + $ctr->get('/menu', [$app['jsonapi.action.menu'], 'handle']) ->bind('jsonapi.menu'); - $ctr->get("/search", [$app['jsonapi.action.search'], "handle"]) + $ctr->get('/search', [$app['jsonapi.action.search'], 'handle']) ->bind('jsonapi.searchAll') ->convert('parameters', 'jsonapi.converter:grabParameters'); - $ctr->get("/{contentType}/search", [$app['jsonapi.action.search'], "handle"]) + $ctr->get('/{contentType}/search', [$app['jsonapi.action.search'], 'handle']) ->bind('jsonapi.searchContent') ->convert('parameters', 'jsonapi.converter:grabParameters'); - $ctr->get("/{contentType}/{slug}/{relatedContentType}", [$app['jsonapi.action.single'], 'handle']) + $ctr->get('/{contentType}/{slug}/{relatedContentType}', [$app['jsonapi.action.single'], 'handle']) ->value('relatedContentType', null) ->assert('slug', '[a-zA-Z0-9_\-]+') ->bind('jsonapi.singleContent') ->convert('parameters', 'jsonapi.converter:grabParameters'); - $ctr->get("/{contentType}", [$app['jsonapi.action.contentlist'], "handle"]) + $ctr->get('/{contentType}', [$app['jsonapi.action.contentlist'], 'handle']) ->bind('jsonapi.listContent') ->convert('parameters', 'jsonapi.converter:grabParameters'); diff --git a/src/Converter/JSONAPIConverter.php b/src/Converter/JSONAPIConverter.php index 75700eb..19c9878 100644 --- a/src/Converter/JSONAPIConverter.php +++ b/src/Converter/JSONAPIConverter.php @@ -1,24 +1,21 @@ search = $search; + return $this; } } diff --git a/src/Converter/Parameter/Type/Sort.php b/src/Converter/Parameter/Type/Sort.php index 03f59eb..c8eb382 100644 --- a/src/Converter/Parameter/Type/Sort.php +++ b/src/Converter/Parameter/Type/Sort.php @@ -4,6 +4,7 @@ /** * Class Sort + * * @package Bolt\Extension\Bolt\JsonApi\Converter\Parameter\Type */ class Sort extends AbstractParameter @@ -15,6 +16,7 @@ class Sort extends AbstractParameter /** * Parameter example: sort=id,-body + * * @return $this */ public function convertRequest() @@ -54,6 +56,7 @@ public function getSort() /** * @param string $sort + * * @return Sort */ public function setSort($sort) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 300969f..51cefee 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -1,13 +1,13 @@ makeQueryParameters($parameters); + $links['self'] = $basePathContentType . $this->makeQueryParameters($parameters); //If there are more pages defined, then show them... if ($firstPage != $totalPages) { // The following links only exists if a query was made using pagination. if ($currentPage != $firstPage) { $params = $this->makeQueryParameters($parameters, ['page' => ['number' => $firstPage]]); - $links["first"] = $basePathContentType.$params; + $links['first'] = $basePathContentType . $params; } if ($currentPage != $totalPages) { $params = $this->makeQueryParameters($parameters, ['page' => ['number' => $totalPages]]); - $links["last"] = $basePathContentType.$params; + $links['last'] = $basePathContentType . $params; } $previousPage = $this->getPreviousPage($currentPage); if ($currentPage != $previousPage) { $params = $this->makeQueryParameters($parameters, ['page' => ['number' => $previousPage]]); - $links["prev"] = $basePathContentType.$params; + $links['prev'] = $basePathContentType . $params; } $nextPage = $this->getNextPage($currentPage, $totalPages); if ($currentPage != $nextPage) { $params = $this->makeQueryParameters($parameters, ['page' => ['number' => $nextPage]]); - $links["next"] = $basePathContentType.$params; + $links['next'] = $basePathContentType . $params; } } @@ -69,6 +69,7 @@ public function makeLinks($contentType, $currentPage, $totalPages, $pageSize, Re /** * @param $currentPage + * * @return mixed */ protected function getPreviousPage($currentPage) @@ -79,6 +80,7 @@ protected function getPreviousPage($currentPage) /** * @param $currentPage * @param $totalPages + * * @return mixed */ protected function getNextPage($currentPage, $totalPages) @@ -88,6 +90,7 @@ protected function getNextPage($currentPage, $totalPages) /** * @param Request $request + * * @return array */ protected function getQueryParameters(Request $request) @@ -99,12 +102,13 @@ protected function getQueryParameters(Request $request) * Make related links for a singular item. * * @param \Bolt\Storage\Entity\Content $item + * * @return mixed[] An array with URLs for the relationships. */ public function makeRelatedLinks($item) { $related = []; - $contentType = (string)$item->getContenttype(); + $contentType = (string) $item->getContenttype(); if (count($item->getRelation()) > 0) { foreach ($item->getRelation() as $relatedType) { @@ -112,10 +116,10 @@ public function makeRelatedLinks($item) $fromType = $relatedType->getFromContenttype(); $related[$fromType] = [ - 'href' => $this->config->getBasePath()."/$contentType/$id/$fromType", + 'href' => $this->config->getBasePath() . "/$contentType/$id/$fromType", 'meta' => [ - 'count' => 1 - ] + 'count' => 1, + ], ]; } } @@ -127,10 +131,11 @@ public function makeRelatedLinks($item) * Make a new querystring while preserving current query parameters with the * option to override values. * - * @param array $overrides A (key,value)-array with elements to override in - * the current query string. - * @param bool $buildQuery Returns a querystring if set to true, otherwise + * @param array $overrides A (key,value)-array with elements to override in + * the current query string. + * @param bool $buildQuery Returns a querystring if set to true, otherwise * returns the array with (key,value)-pairs. + * * @return mixed query parameters in either array or string form. * * @see \Bolt\Helpers\Arr::mergeRecursiveDistinct() @@ -155,8 +160,10 @@ public function makeQueryParameters($parameters, $overrides = [], $buildQuery = if (!empty($queryString)) { $queryString = '?' . $queryString; } + return $queryString; } + return $queryParameters; } } diff --git a/src/JSONAPIExtension.php b/src/JSONAPIExtension.php index 384132b..9ec2868 100644 --- a/src/JSONAPIExtension.php +++ b/src/JSONAPIExtension.php @@ -11,7 +11,6 @@ namespace Bolt\Extension\Bolt\JsonApi; use Bolt\Extension\Bolt\JsonApi\Controllers\ContentController; -use Bolt\Extension\Bolt\JsonApi\Controllers\MenuController; use Bolt\Extension\Bolt\JsonApi\Exception\ApiException; use Bolt\Extension\Bolt\JsonApi\Provider\APIProvider; use Bolt\Extension\Bolt\JsonApi\Response\ApiErrorResponse; @@ -34,7 +33,7 @@ public function getServiceProviders() { return [ $this, - new APIProvider($this->getConfig()) + new APIProvider($this->getConfig()), ]; } @@ -72,6 +71,7 @@ public static function getSubscribedEvents() /** * Listener to handle all exceptions thrown of type ApiException. It converts * the exception into an ApiErrorResponse. + * * @param GetResponseForExceptionEvent $response */ public function error(GetResponseForExceptionEvent $response) diff --git a/src/Parser/Field/FieldCollection.php b/src/Parser/Field/FieldCollection.php index c06f092..74c03b8 100644 --- a/src/Parser/Field/FieldCollection.php +++ b/src/Parser/Field/FieldCollection.php @@ -1,6 +1,5 @@ type = $type; + return $this; } @@ -49,17 +50,19 @@ public function getValue() /** * @param mixed $value + * * @return AbstractType */ public function setValue($value) { $this->value = $value; + return $this; } /** * @return mixed - * Internal function used to display the json representation + * Internal function used to display the json representation */ abstract public function render(); diff --git a/src/Parser/Field/Type/Date.php b/src/Parser/Field/Type/Date.php index aa193c7..4b1ce25 100644 --- a/src/Parser/Field/Type/Date.php +++ b/src/Parser/Field/Type/Date.php @@ -1,6 +1,5 @@ getFieldType() == 'filelist') { foreach ($this->getValue() as &$file) { $file['url'] = $this->makeAbsoluteLinkToResource($file['filename']); @@ -77,6 +76,7 @@ public function render() /** * @param string $filename + * * @return string */ protected function makeAbsoluteLinkToResource($filename = '') @@ -91,6 +91,7 @@ protected function makeAbsoluteLinkToResource($filename = '') /** * @param string $filename + * * @return string */ protected function makeAbsoluteLinkToThumbnail($filename = '') @@ -114,11 +115,13 @@ public function getFieldType() /** * @param mixed $fieldType + * * @return File */ public function setFieldType($fieldType) { $this->fieldType = $fieldType; + return $this; } } diff --git a/src/Parser/Field/Type/Generic.php b/src/Parser/Field/Type/Generic.php index 6f0c7fa..6f81298 100644 --- a/src/Parser/Field/Type/Generic.php +++ b/src/Parser/Field/Type/Generic.php @@ -1,6 +1,5 @@ type = $type; + return $this; } @@ -40,11 +40,13 @@ public function getValue() /** * @param mixed $value + * * @return RepeatingCollection */ public function setValue($value) { $this->value = $value; + return $this; } diff --git a/src/Parser/Field/Type/RepeatingFieldCollection.php b/src/Parser/Field/Type/RepeatingFieldCollection.php index 1cd9b1b..5dd1ba6 100644 --- a/src/Parser/Field/Type/RepeatingFieldCollection.php +++ b/src/Parser/Field/Type/RepeatingFieldCollection.php @@ -1,11 +1,9 @@ getId(); $values = [ - 'id' => strval($id), + 'id' => strval($id), 'type' => $contentType, ]; $fields = array_unique($fields); @@ -80,7 +77,6 @@ public function parseItem($item, $fields = []) $item = str_replace($from, $to, $item); } } - }); $values['attributes'] = $attributes; @@ -107,15 +103,15 @@ public function parseItem($item, $fields = []) $data[] = [ 'type' => $fromType, - 'id' => $id + 'id' => $id, ]; $relationships[$toType] = [ 'links' => [ // 'self' -- this is irrelevant for now - 'related' => $this->config->getBasePath()."/$fromType/$id/$toType" + 'related' => $this->config->getBasePath() . "/$fromType/$id/$toType", ], - 'data' => $data + 'data' => $data, ]; } $values['relationships'] = $relationships; diff --git a/src/Provider/APIProvider.php b/src/Provider/APIProvider.php index 5dfe651..fd05a10 100644 --- a/src/Provider/APIProvider.php +++ b/src/Provider/APIProvider.php @@ -17,11 +17,11 @@ /** * Class APIProvider + * * @package JSONAPI\Provider */ class APIProvider implements ServiceProviderInterface { - /** @var array */ private $config; /** @@ -39,6 +39,7 @@ public function __construct(array $config) * * This method should only be used to configure services and parameters. * It should not get services. + * * @param Application $app */ public function register(Application $app) @@ -69,6 +70,7 @@ function ($app) { /** * Class to handle parsing of individual items + * * @todo Refactor to include individual parsers based upon the field type */ $app['jsonapi.parser'] = $app->share( @@ -83,6 +85,7 @@ function ($app) { /** * Simple class to handle linking to related items and the current content type + * * @todo Refactor... */ $app['jsonapi.datalinks'] = $app->share( @@ -144,6 +147,7 @@ function ($classMetadata) use ($app) { $repoClass = 'Bolt\Extension\Bolt\JsonApi\Storage\Repository'; $repo = new $repoClass($app['storage'], $classMetadata); $repo->setLegacyService($app['storage.legacy_service']); + return $repo; } ); @@ -151,7 +155,6 @@ function ($classMetadata) use ($app) { $app['query.parser']->addDirectiveHandler('paginate', new PagerHandler()); $app['query.parser']->addHandler('pager', new PagingHandler()); $app['query.parser']->addOperation('pager'); - } /** @@ -160,6 +163,7 @@ function ($classMetadata) use ($app) { * This method is called after all services are registered * and should be used for "dynamic" configuration (whenever * a service must be requested). + * * @param Application $app */ public function boot(Application $app) diff --git a/src/Response/ApiErrorResponse.php b/src/Response/ApiErrorResponse.php index 058d261..e4b492a 100644 --- a/src/Response/ApiErrorResponse.php +++ b/src/Response/ApiErrorResponse.php @@ -6,11 +6,11 @@ /** * Class ApiErrorResponse + * * @package Bolt\Extension\Bolt\JsonApi\Response */ class ApiErrorResponse extends ApiResponse { - /** * @var string */ @@ -23,9 +23,10 @@ class ApiErrorResponse extends ApiResponse /** * ApiErrorResponse constructor. + * * @param string $status * @param string $title - * @param array $data + * @param array $data * @param Config $config */ public function __construct($status, $title, array $data, Config $config) @@ -38,6 +39,7 @@ public function __construct($status, $title, array $data, Config $config) /** * @param array $content + * * @return \Symfony\Component\HttpFoundation\Response */ public function setContent($content) @@ -46,7 +48,7 @@ public function setContent($content) $content['title'] = $this->title; return parent::setContent([ - 'errors' => $content + 'errors' => $content, ]); } } diff --git a/src/Response/ApiResponse.php b/src/Response/ApiResponse.php index 1958295..8a2a4f3 100644 --- a/src/Response/ApiResponse.php +++ b/src/Response/ApiResponse.php @@ -7,6 +7,7 @@ /** * Class ApiResponse + * * @package Bolt\Extension\Bolt\JsonApi\Response */ class ApiResponse extends Response @@ -18,7 +19,8 @@ class ApiResponse extends Response /** * ApiResponse constructor. - * @param array $data + * + * @param array $data * @param Config $config */ public function __construct(array $data, Config $config) @@ -32,11 +34,11 @@ public function __construct(array $data, Config $config) * encodes the data and sets the json options from the config object * * @param array $content + * * @return Response */ public function setContent($content) { - if ($this->config->getJsonOptions()) { $json_encodeOptions = $this->config->getJsonOptions(); } else { @@ -49,7 +51,6 @@ public function setContent($content) return parent::setContent($json); } - /** * Adds the headers to the response based on the config */ @@ -62,9 +63,9 @@ private function setHeadersFromConfig() } } - /** * Checks the data for 'errors' key and sets a status code based on + * * @param array $data */ private function setStatusCodeFromData(array $data) diff --git a/src/Storage/Query/Handler/Directive/PagerHandler.php b/src/Storage/Query/Handler/Directive/PagerHandler.php index 232d671..859fec8 100644 --- a/src/Storage/Query/Handler/Directive/PagerHandler.php +++ b/src/Storage/Query/Handler/Directive/PagerHandler.php @@ -1,6 +1,5 @@ getNumber()-1) * $page->getSize(); + $offset = ($page->getNumber() - 1) * $page->getSize(); //Set pagination offsets and limits $query->getQueryBuilder()->setFirstResult($offset); diff --git a/src/Storage/Query/Handler/PagingHandler.php b/src/Storage/Query/Handler/PagingHandler.php index 4ded09d..4d669c6 100644 --- a/src/Storage/Query/Handler/PagingHandler.php +++ b/src/Storage/Query/Handler/PagingHandler.php @@ -1,6 +1,5 @@ resetQueryParts(['maxResults', 'firstResult', 'orderBy']) ->setFirstResult(null) ->setMaxResults(null) - ->select("COUNT(*) as total"); + ->select('COUNT(*) as total'); $totalItems = count($repo->findResults($query2)); @@ -78,6 +77,5 @@ public function __invoke(ContentQueryParser $contentQuery) } return $set; - } } diff --git a/src/Storage/Query/PagingResultSet.php b/src/Storage/Query/PagingResultSet.php index ccc0cdd..3f83b76 100644 --- a/src/Storage/Query/PagingResultSet.php +++ b/src/Storage/Query/PagingResultSet.php @@ -1,6 +1,5 @@ 1) { //Round the total pages up (Don't want 2.5) $this->totalPages = ceil($totalPages); + return $this; } $this->totalPages = Page::DEFAULT_PAGE_NUMBER; + return $this; } @@ -57,11 +59,13 @@ public function getTotalResults() /** * @param mixed $totalResults + * * @return PagingResultSet */ public function setTotalResults($totalResults) { $this->totalResults += $totalResults; + return $this; } } diff --git a/src/Storage/Repository.php b/src/Storage/Repository.php index 4a8c5ea..833e0d3 100644 --- a/src/Storage/Repository.php +++ b/src/Storage/Repository.php @@ -1,6 +1,5 @@