From a42bad401458be7d8c4f1ad0c87b09554666908f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 3 Aug 2019 15:46:58 +0800 Subject: [PATCH] Refactor code. Signed-off-by: Mior Muhammad Zaki --- Concerns/Transformable.php | 20 ++++++++++---------- FormRequest.php | 2 +- HashIdServiceProvider.php | 2 +- Middleware/NotModified.php | 2 +- Middleware/RequireCsrfToken.php | 2 +- Providers/VersionServiceProvider.php | 4 ++-- RouteManager.php | 10 +++------- RouteResolver.php | 14 +++++++------- VersionControl.php | 8 ++++---- 9 files changed, 30 insertions(+), 34 deletions(-) diff --git a/Concerns/Transformable.php b/Concerns/Transformable.php index 65e8569..83f1408 100644 --- a/Concerns/Transformable.php +++ b/Concerns/Transformable.php @@ -84,18 +84,18 @@ public function setRequest($request) */ protected function merge($meta, array $options = []): array { - if (is_array($meta) && empty($options)) { + if (\is_array($meta) && empty($options)) { $options = $meta; $meta = null; } - $options = array_merge(['includes' => [], 'excludes' => []], $options); + $options = \array_merge(['includes' => [], 'excludes' => []], $options); foreach ($options as $key => $value) { - $filtered = Arr::expand(array_flip($value)); - $parent = Arr::get($this->options, is_null($meta) ? $key : "{$key}.{$meta}", []); + $filtered = Arr::expand(\array_flip($value)); + $parent = Arr::get($this->options, \is_null($meta) ? $key : "{$key}.{$meta}", []); - $options[$key] = array_keys(Arr::dot(array_merge_recursive($filtered, $parent))); + $options[$key] = \array_keys(Arr::dot(\array_merge_recursive($filtered, $parent))); } return $options; @@ -120,13 +120,13 @@ protected function transformByMeta(string $meta, array $data, ...$parameters): a } foreach ($types as $type => $index) { - if (is_array($type)) { + if (\is_array($type)) { continue; } $method = $name.Str::studly($type); - if (method_exists($this, $method)) { + if (\method_exists($this, $method)) { $data = $this->{$method}($data, ...$parameters); } } @@ -145,10 +145,10 @@ protected function filterMetaType(string $name): void { $types = $this->options[$name] ?? $this->getRequest()->input($name); - if (is_string($types)) { - $types = explode(',', $types); + if (\is_string($types)) { + $types = \explode(',', $types); } - $this->options[$name] = is_array($types) ? Arr::expand(array_flip($types)) : []; + $this->options[$name] = \is_array($types) ? Arr::expand(\array_flip($types)) : []; } } diff --git a/FormRequest.php b/FormRequest.php index 535ed1a..764a2be 100644 --- a/FormRequest.php +++ b/FormRequest.php @@ -51,7 +51,7 @@ protected function setupValidationScenario() 'DELETE' => 'destroy', ]; - if (in_array($current, $available)) { + if (\in_array($current, $available)) { $this->onValidationScenario($available[$current]); } } diff --git a/HashIdServiceProvider.php b/HashIdServiceProvider.php index 6df442d..42e16a8 100644 --- a/HashIdServiceProvider.php +++ b/HashIdServiceProvider.php @@ -22,7 +22,7 @@ class HashIdServiceProvider extends ServiceProvider */ public function register() { - $this->app->singleton('orchestra.hashid', function (Application $app) { + $this->app->singleton('orchestra.hashid', static function (Application $app) { return new Hashids($app->make('config')->get('app.key')); }); } diff --git a/Middleware/NotModified.php b/Middleware/NotModified.php index a778541..8a709eb 100644 --- a/Middleware/NotModified.php +++ b/Middleware/NotModified.php @@ -19,7 +19,7 @@ public function handle($request, Closure $next) $response = $next($request); if (! $response->headers->has('Etag')) { - $response->setEtag(md5($response->getContent())); + $response->setEtag(\md5($response->getContent())); } $response->isNotModified($request); diff --git a/Middleware/RequireCsrfToken.php b/Middleware/RequireCsrfToken.php index e39d174..a10d958 100644 --- a/Middleware/RequireCsrfToken.php +++ b/Middleware/RequireCsrfToken.php @@ -67,7 +67,7 @@ protected function tokensMatch($request) $token = $this->encrypter->decrypt($header); } - return hash_equals((string) $request->session()->token(), (string) $token); + return \hash_equals((string) $request->session()->token(), (string) $token); } /** diff --git a/Providers/VersionServiceProvider.php b/Providers/VersionServiceProvider.php index 13d14a1..cc4e5f0 100644 --- a/Providers/VersionServiceProvider.php +++ b/Providers/VersionServiceProvider.php @@ -29,7 +29,7 @@ class VersionServiceProvider extends ServiceProvider public function register() { $this->app->singleton('orchestra.http.version', function (Application $app) { - return tap(new VersionControl(), function ($version) { + return \tap(new VersionControl(), function ($version) { $this->registerSupportedVersions($version); }); }); @@ -48,7 +48,7 @@ protected function registerSupportedVersions(VersionControl $version): void $version->addVersion($code, $namespace); } - if (! is_null($this->defaultVersion)) { + if (! \is_null($this->defaultVersion)) { $version->setDefaultVersion($this->defaultVersion); } } diff --git a/RouteManager.php b/RouteManager.php index d52571e..660e373 100644 --- a/RouteManager.php +++ b/RouteManager.php @@ -38,13 +38,9 @@ abstract class RouteManager implements RouteManagerContract */ public function __construct(Application $app, ?RouteResolver $resolver = null) { - if (is_null($resolver)) { - $resolver = new RouteResolver($app); - } - $this->app = $app; $this->router = $this->resolveApplicationRouter($app); - $this->resolver = $resolver; + $this->resolver = $resolver instanceof RouteResolver ? $resolver : new RouteResolver($app); } /** @@ -78,9 +74,9 @@ public function group(string $name, string $default, $attributes = [], Closure $ $attributes = []; } - $attributes = array_merge($attributes, $route->group()); + $attributes = \array_merge($attributes, $route->group()); - if (! is_null($callback)) { + if (! \is_null($callback)) { $this->router->group($attributes, $callback); } diff --git a/RouteResolver.php b/RouteResolver.php index 3b2d825..efa88b5 100644 --- a/RouteResolver.php +++ b/RouteResolver.php @@ -108,8 +108,8 @@ public function locate(string $path, array $options = []): array // split URI and query string, the route resolver should not worry // about provided query string. - if (strpos($path, '?') !== false) { - list($path, $query) = explode('?', $path, 2); + if (\strpos($path, '?') !== false) { + list($path, $query) = \explode('?', $path, 2); } list($package, $route, $item) = with(new NamespacedItemResolver())->parseKey($path); @@ -148,7 +148,7 @@ public function route(string $name, string $default = '/') */ public function mode(): string { - if (is_null($this->status)) { + if (\is_null($this->status)) { return 'normal'; } @@ -189,7 +189,7 @@ public function to(string $path, array $options = []): string */ protected function generateRouteByName(string $name, string $default) { - if (is_null($this->extension)) { + if (\is_null($this->extension)) { return $default; } @@ -216,7 +216,7 @@ protected function prepareValidRoute(string $route, ?string $item, string $query $appends['_token'] = $this->getCsrfToken(); } - if (! in_array($mode, ['normal'])) { + if (! \in_array($mode, ['normal'])) { $appends['_mode'] = $mode; } @@ -240,7 +240,7 @@ protected function prepareValidRoute(string $route, ?string $item, string $query protected function prepareHttpQueryString(string $query, array $appends = []): string { if (! empty($appends)) { - $query .= (! empty($query) ? '&' : '').http_build_query($appends); + $query .= (! empty($query) ? '&' : '').\http_build_query($appends); } return $query; @@ -253,7 +253,7 @@ protected function prepareHttpQueryString(string $query, array $appends = []): s */ protected function getCsrfToken(): ?string { - if (is_null($this->csrfToken)) { + if (\is_null($this->csrfToken)) { $this->csrfToken = $this->app->make('session')->token(); } diff --git a/VersionControl.php b/VersionControl.php index 6492d14..ecc47d8 100644 --- a/VersionControl.php +++ b/VersionControl.php @@ -51,7 +51,7 @@ public function addVersion(string $code, string $namespace, bool $default = fals */ public function setDefaultVersion(string $code) { - if (! array_key_exists($code, $this->supportedVersions)) { + if (! \array_key_exists($code, $this->supportedVersions)) { throw new InvalidArgumentException("Unable to set [{$code}] as default version!"); } @@ -72,12 +72,12 @@ public function setDefaultVersion(string $code) */ public function resolve(string $namespace, string $version, string $name): string { - $class = str_replace('.', '\\', $name); + $class = \str_replace('.', '\\', $name); - if (! array_key_exists($version, $this->supportedVersions)) { + if (! \array_key_exists($version, $this->supportedVersions)) { $version = $this->defaultVersion; } - return sprintf('%s\%s\%s\%s', $namespace, $this->supportedVersions[$version], $class); + return \sprintf('%s\%s\%s\%s', $namespace, $this->supportedVersions[$version], $class); } }