From 89e179159c4e61b722677ca2451d3965a45c45b9 Mon Sep 17 00:00:00 2001 From: butschster Date: Wed, 4 Nov 2015 03:42:59 -0500 Subject: [PATCH] Applied fixes from StyleCI --- src/Exceptions/AuthenticateException.php | 3 +- src/Exceptions/Exception.php | 4 +- src/Exceptions/MissingParameterException.php | 12 ++-- src/Exceptions/PermissionException.php | 11 +-- src/Exceptions/ValidationException.php | 17 ++--- src/Http/Controllers/Controller.php | 73 +++++++++----------- src/Http/Response.php | 34 ++++----- src/Http/RouteAPI.php | 41 +++++------ src/RouteApiFacade.php | 3 +- 9 files changed, 75 insertions(+), 123 deletions(-) diff --git a/src/Exceptions/AuthenticateException.php b/src/Exceptions/AuthenticateException.php index 5f33902..218db75 100644 --- a/src/Exceptions/AuthenticateException.php +++ b/src/Exceptions/AuthenticateException.php @@ -6,9 +6,8 @@ class AuthenticateException extends Exception { - /** * @var int */ protected $code = Response::ERROR_UNAUTHORIZED; -} \ No newline at end of file +} diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php index b98e5e6..acc1142 100644 --- a/src/Exceptions/Exception.php +++ b/src/Exceptions/Exception.php @@ -6,13 +6,11 @@ class Exception extends \RuntimeException { - /** * @var int */ protected $code = Response::ERROR_UNKNOWN; - /** * @return array */ @@ -24,4 +22,4 @@ public function responseArray() 'message' => $this->getMessage(), ]; } -} \ No newline at end of file +} diff --git a/src/Exceptions/MissingParameterException.php b/src/Exceptions/MissingParameterException.php index 01d2f68..9b2c4dd 100644 --- a/src/Exceptions/MissingParameterException.php +++ b/src/Exceptions/MissingParameterException.php @@ -7,7 +7,6 @@ class MissingParameterException extends Exception { - /** * @var string */ @@ -16,21 +15,19 @@ class MissingParameterException extends Exception /** * @var array */ - protected $rules = [ ]; - + protected $rules = []; /** * @param Validator $validator */ public function __construct(Validator $validator) { - $this->rules = $validator->errors()->getMessages(); + $this->rules = $validator->errors()->getMessages(); $this->message = trans('api.messages.missing_params', [ - 'field' => implode(', ', array_keys($validator->failed())) + 'field' => implode(', ', array_keys($validator->failed())), ]); } - /** * @return array */ @@ -39,13 +36,12 @@ public function getFailedRules() return $this->rules; } - /** * @return array */ public function responseArray() { - $data = parent::responseArray(); + $data = parent::responseArray(); $data['failed_rules'] = $this->getFailedRules(); return $data; diff --git a/src/Exceptions/PermissionException.php b/src/Exceptions/PermissionException.php index b7cb39d..5c97f2a 100644 --- a/src/Exceptions/PermissionException.php +++ b/src/Exceptions/PermissionException.php @@ -6,24 +6,21 @@ class PermissionException extends Exception { - /** * @var string */ protected $permission = ''; - /** * @param null $permission * @param string $message */ - public function __construct($permission = null, $message = "") + public function __construct($permission = null, $message = '') { $this->setPermission($permission); - $this->message = empty( $message ) ? trans('api.messages.error_permissions') : $message; + $this->message = empty($message) ? trans('api.messages.error_permissions') : $message; } - /** * @param $permission */ @@ -32,7 +29,6 @@ public function setPermission($permission) $this->permission = $permission; } - /** * @return array */ @@ -41,8 +37,7 @@ public function responseArray() return [ 'code' => Response::ERROR_PERMISSIONS, 'type' => Response::TYPE_ERROR, - 'permission' => $this->permission + 'permission' => $this->permission, ]; } - } diff --git a/src/Exceptions/ValidationException.php b/src/Exceptions/ValidationException.php index f9b6433..2beec2e 100644 --- a/src/Exceptions/ValidationException.php +++ b/src/Exceptions/ValidationException.php @@ -7,23 +7,21 @@ class ValidationException extends Exception { - /** * @var array */ - protected $messages = [ ]; + protected $messages = []; /** * @var array */ - protected $rules = [ ]; + protected $rules = []; /** * @var int */ protected $code = Response::ERROR_VALIDATION; - /** * @param Validator $object * @@ -32,14 +30,12 @@ class ValidationException extends Exception public function setValidator(Validator $object) { $this->messages = $object->errors()->getMessages(); - $this->rules = $object->failed(); + $this->rules = $object->failed(); return $this; } - /** - * * @return array */ public function getFailedRules() @@ -47,9 +43,7 @@ public function getFailedRules() return $this->rules; } - /** - * * @return array */ public function getErrorMessages() @@ -57,16 +51,15 @@ public function getErrorMessages() return $this->messages; } - /** * /** * @return array */ public function responseArray() { - $data = parent::responseArray(); + $data = parent::responseArray(); $data['failed_rules'] = $this->getFailedRules(); - $data['errors'] = $this->getErrorMessages(); + $data['errors'] = $this->getErrorMessages(); return $data; } diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php index b79d6fa..3424752 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Http/Controllers/Controller.php @@ -17,23 +17,22 @@ abstract class Controller extends BaseController { - use DispatchesJobs, ValidatesRequests; /** - * Массив возвращаемых значений, будет преобразован в формат JSON + * Массив возвращаемых значений, будет преобразован в формат JSON. + * * @var array */ - public $responseArray = [ 'content' => null ]; - + public $responseArray = ['content' => null]; /** * @var array */ - public $requiredFields = [ ]; + public $requiredFields = []; /** - * Получение значения передаваемого параметра + * Получение значения передаваемого параметра. * * Если параметр указан как обязательный, то при его отсутсвии на запрос * вернется ошибка @@ -42,32 +41,32 @@ abstract class Controller extends BaseController * @param mixed $default Значение по умолчанию, если параметр отсутсвует * @param bool|string|array $isRequired Параметр обязателен для передачи * - * @return string * @throws MissingApiParameterException + * + * @return string */ public function getParameter($key, $default = null, $isRequired = false) { - if ( ! empty( $isRequired )) { - $this->validateParameters([ $key => $isRequired ]); + if (!empty($isRequired)) { + $this->validateParameters([$key => $isRequired]); } return array_get(Request::all(), $key, $default); } - /** * @param string $key * @param bool|string|array $rules * - * @return string * @throws MissingApiParameterException + * + * @return string */ public function getRequiredParameter($key, $rules = true) { return $this->getParameter($key, null, $rules); } - /** * @param string $message */ @@ -76,7 +75,6 @@ public function setMessage($message) $this->responseArray['message'] = $message; } - /** * @param array $errors */ @@ -85,7 +83,6 @@ public function setErrors(array $errors) $this->responseArray['errors'] = $errors; } - /** * @param mixed $data */ @@ -98,17 +95,17 @@ public function setContent($data) $this->responseArray['content'] = $data; } - /** * @param array $parameters * - * @return bool * @throws MissingApiParameterException + * + * @return bool */ final public function validateParameters(array $parameters) { $parameters = array_map(function ($rules) { - if (is_bool($rules) AND $rules === true) { + if (is_bool($rules) and $rules === true) { return 'required'; } @@ -125,31 +122,30 @@ final public function validateParameters(array $parameters) return true; } - /** * Execute an action on the controller. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * * @return array */ public function callAction($method, $parameters) { - $this->responseArray['type'] = Response::TYPE_CONTENT; + $this->responseArray['type'] = Response::TYPE_CONTENT; $this->responseArray['method'] = Request::method(); - $this->responseArray['code'] = Response::NO_ERROR; + $this->responseArray['code'] = Response::NO_ERROR; - if (isset( $this->requiredFields[$method] ) AND is_array($this->requiredFields[$method])) { + if (isset($this->requiredFields[$method]) and is_array($this->requiredFields[$method])) { $this->validateParameters($this->requiredFields[$method]); } $response = parent::callAction($method, $parameters); if ($response instanceof RedirectResponse) { - $this->responseArray['type'] = Response::TYPE_REDIRECT; + $this->responseArray['type'] = Response::TYPE_REDIRECT; $this->responseArray['targetUrl'] = $response->getTargetUrl(); - $this->responseArray['code'] = $response->getStatusCode(); + $this->responseArray['code'] = $response->getStatusCode(); } else { if ($response instanceof View) { return new JsonResponse($response->render()); @@ -163,7 +159,6 @@ public function callAction($method, $parameters) return (new Response(config('app.debug')))->createResponse($this->responseArray); } - /** * @param string $command * @@ -176,16 +171,15 @@ public function addJavascriptCommand($command) return $this; } - /** * Handle calls to missing methods on the controller. * - * @param string $method - * @param array $parameters - * - * @return mixed + * @param string $method + * @param array $parameters * * @throws BadMethodCallException + * + * @return mixed */ public function __call($method, $parameters) { @@ -205,7 +199,6 @@ public function __set($key, $value) $this->responseArray[$key] = $value; } - /** * @param string $key * @@ -216,7 +209,6 @@ public function __get($key) return $this->responseArray[$key]; } - /** * @param string $key * @@ -224,24 +216,22 @@ public function __get($key) */ public function __isset($key) { - return isset( $this->responseArray[$key] ); + return isset($this->responseArray[$key]); } - /** * @param string $key */ public function __unset($key) { - unset( $this->responseArray[$key] ); + unset($this->responseArray[$key]); } - /** * Throw the failed validation exception. * - * @param \Illuminate\Http\Request $request - * @param \Illuminate\Contracts\Validation\Validator $validator + * @param \Illuminate\Http\Request $request + * @param \Illuminate\Contracts\Validation\Validator $validator * * @return void */ @@ -253,12 +243,11 @@ protected function throwValidationException(Request $request, $validator) throw $exception; } - /** * Create the response for when a request fails validation. * - * @param \Illuminate\Http\Request $request - * @param array $errors + * @param \Illuminate\Http\Request $request + * @param array $errors * * @return \Illuminate\Http\Response */ diff --git a/src/Http/Response.php b/src/Http/Response.php index 7664b73..5ddd867 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -11,7 +11,6 @@ class Response { - const NO_ERROR = 200; const ERROR_MISSING_PAPAM = 110; const ERROR_VALIDATION = 120; @@ -28,7 +27,6 @@ class Response private $debug; - /** * @param bool $debug */ @@ -37,7 +35,6 @@ public function __construct($debug = true) $this->debug = $debug; } - /** * Creates the error Response associated with the given Exception. * @@ -50,7 +47,7 @@ public function createExceptionResponse(\Exception $exception) $responseData = [ 'code' => $exception->getCode(), 'type' => static::TYPE_ERROR, - 'message' => $exception->getMessage() + 'message' => $exception->getMessage(), ]; if ($exception instanceof Exception or method_exists($exception, 'responseArray')) { @@ -60,7 +57,7 @@ public function createExceptionResponse(\Exception $exception) $responseData['code'] = static::ERROR_PAGE_NOT_FOUND; } else { if ($exception instanceof MassAssignmentException) { - $responseData['code'] = static::ERROR_MISSING_ASSIGMENT; + $responseData['code'] = static::ERROR_MISSING_ASSIGMENT; $responseData['field'] = $exception->getMessage(); } } @@ -74,10 +71,9 @@ public function createExceptionResponse(\Exception $exception) return $this->createResponse($responseData, 500); } - /** - * @param array $responseData - * @param integer $code + * @param array $responseData + * @param int $code * * @return Response */ @@ -95,10 +91,9 @@ public function createResponse(array $responseData, $code = 200) } } - /** - * @param array $data - * @param integer $code + * @param array $data + * @param int $code * * @return Response */ @@ -107,14 +102,13 @@ protected function yamlResponse($data, $code) $yaml = Yaml::dump($data); return new \Illuminate\Http\Response($yaml, $code, [ - 'Content-Type' => 'text/x-yaml' + 'Content-Type' => 'text/x-yaml', ]); } - /** - * @param array $data - * @param integer $code + * @param array $data + * @param int $code * * @return Response */ @@ -125,11 +119,10 @@ protected function xmlResponse($data, $code) $this->arrayToXml($data, $xml); return new \Illuminate\Http\Response((string) $xml->asXML(), $code, [ - 'Content-Type' => 'application/xml' + 'Content-Type' => 'application/xml', ]); } - protected function arrayToXml($data, &$xml) { foreach ($data as $key => $value) { @@ -146,17 +139,16 @@ protected function arrayToXml($data, &$xml) } } - /** - * @param array $data - * @param integer $code + * @param array $data + * @param int $code * * @return JsonResponse */ protected function jsonResponse($data, $code) { return new JsonResponse($data, $code, [ - 'Content-Type' => 'application/json' + 'Content-Type' => 'application/json', ]); } } diff --git a/src/Http/RouteAPI.php b/src/Http/RouteAPI.php index 0d41efd..d803aaa 100644 --- a/src/Http/RouteAPI.php +++ b/src/Http/RouteAPI.php @@ -7,12 +7,11 @@ class RouteAPI { - /** * Register a new GET route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -23,12 +22,11 @@ public function get($uri, $action) return $this->addResponseType(Route::get($uri, $action)); } - /** * Register a new POST route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -39,12 +37,11 @@ public function post($uri, $action) return $this->addResponseType(Route::post($uri, $action)); } - /** * Register a new PUT route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -55,12 +52,11 @@ public function put($uri, $action) return $this->addResponseType(Route::put($uri, $action)); } - /** * Register a new PATCH route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -71,12 +67,11 @@ public function patch($uri, $action) return $this->addResponseType(Route::patch($uri, $action)); } - /** * Register a new DELETE route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -87,12 +82,11 @@ public function delete($uri, $action) return $this->addResponseType(Route::delete($uri, $action)); } - /** * Register a new OPTIONS route with the router. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -103,12 +97,11 @@ public function options($uri, $action) return $this->addResponseType(Route::options($uri, $action)); } - /** * Register a new route responding to all verbs. * - * @param string $uri - * @param \Closure|array|string $action + * @param string $uri + * @param \Closure|array|string $action * * @return \Illuminate\Routing\Route */ @@ -119,7 +112,6 @@ public function any($uri, $action) return Route::any($uri, $action); } - /** * @param Router $route * @@ -130,7 +122,6 @@ protected function addResponseType(Router $route) return $route->where('type', '\.[a-z]+'); } - /** * @param string $uri * @@ -139,9 +130,9 @@ protected function addResponseType(Router $route) protected function buildUri($uri) { if (strpos($uri, 'api.') === false) { - $uri = 'api.' . $uri; + $uri = 'api.'.$uri; } - return $uri . '{type?}'; + return $uri.'{type?}'; } } diff --git a/src/RouteApiFacade.php b/src/RouteApiFacade.php index a164563..0ed9b8b 100644 --- a/src/RouteApiFacade.php +++ b/src/RouteApiFacade.php @@ -7,7 +7,6 @@ class RouteApiFacade extends Facade { - /** * Get the registered name of the component. * @@ -17,4 +16,4 @@ protected static function getFacadeAccessor() { return RouteAPI::class; } -} \ No newline at end of file +}