Skip to content

Commit

Permalink
Minor code tweaks (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalessil authored and Marc Cámara committed Jul 26, 2018
1 parent 79f0ea6 commit 568d319
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Mcamara/LaravelLocalization/LanguageNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function getMatchesFromAcceptedLanguages()
}
// Unweighted values, get high weight by their position in the
// list
$q = isset($q) ? $q : 1000 - count($matches);
$q = $q ?? 1000 - \count($matches);
$matches[$l] = $q;

//If for some reason the Accept-Language header only sends language with country
Expand Down
33 changes: 13 additions & 20 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

use Illuminate\Config\Repository;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\URL;
use Illuminate\Translation\Translator;
use Illuminate\View\Factory;
use Mcamara\LaravelLocalization\Exceptions\SupportedLocalesNotDefined;
use Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException;

Expand Down Expand Up @@ -140,7 +134,7 @@ public function __construct()
*/
public function setLocale($locale = null)
{
if (empty($locale) || !is_string($locale)) {
if (empty($locale) || !\is_string($locale)) {
// If the locale has not been passed through the function
// it tries to get it from the first segment of the url
$locale = $this->request->segment(1);
Expand Down Expand Up @@ -277,8 +271,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation);
}

if (!empty($locale)) {
if ($locale != $this->getDefaultLocale() || !$this->hideDefaultLocaleInURL() || $forceDefaultLocation) {
if (!empty($locale)) {
if ($forceDefaultLocation || $locale != $this->getDefaultLocale() || !$this->hideDefaultLocaleInURL()) {
$parsed_url['path'] = $locale.'/'.ltrim($parsed_url['path'], '/');
}
}
Expand Down Expand Up @@ -319,7 +313,7 @@ public function getURLFromRouteNameTranslated($locale, $transKeyName, $attribute
throw new UnsupportedLocaleException('Locale \''.$locale.'\' is not in the list of supported locales.');
}

if (!is_string($locale)) {
if (!\is_string($locale)) {
$locale = $this->getDefaultLocale();
}

Expand All @@ -328,7 +322,7 @@ public function getURLFromRouteNameTranslated($locale, $transKeyName, $attribute
if ($forceDefaultLocation || !($locale === $this->defaultLocale && $this->hideDefaultLocaleInURL())) {
$route = '/'.$locale;
}
if (is_string($locale) && $this->translator->has($transKeyName, $locale)) {
if (\is_string($locale) && $this->translator->has($transKeyName, $locale)) {
$translation = $this->translator->trans($transKeyName, [], $locale);
$route .= '/'.$translation;

Expand Down Expand Up @@ -381,7 +375,7 @@ public function getSupportedLocales()

$locales = $this->configRepository->get('laravellocalization.supportedLocales');

if (empty($locales) || !is_array($locales)) {
if (empty($locales) || !\is_array($locales)) {
throw new SupportedLocalesNotDefined();
}

Expand Down Expand Up @@ -555,8 +549,7 @@ protected function substituteAttributesInRoute($attributes, $route)
if ($value instanceOf UrlRoutable) {
$value = $value->getRouteKey();
}
$route = str_replace('{'.$key.'}', $value, $route);
$route = str_replace('{'.$key.'?}', $value, $route);
$route = str_replace(array('{'.$key.'}', '{'.$key.'?}'), $value, $route);
}

// delete empty optional arguments that are not in the $attributes array
Expand Down Expand Up @@ -594,7 +587,7 @@ public function setRouteName($routeName)
*/
public function transRoute($routeName)
{
if (!in_array($routeName, $this->translatedRoutes)) {
if (!\in_array($routeName, $this->translatedRoutes)) {
$this->translatedRoutes[] = $routeName;
}

Expand Down Expand Up @@ -840,7 +833,7 @@ protected function extractAttributes($url = false, $locale = '')
$response = array_shift($response);
}

if (is_array($response)) {
if (\is_array($response)) {
$attributes = array_merge($attributes, $response);
}
}
Expand All @@ -863,16 +856,16 @@ protected function unparseUrl($parsed_url)

$url = '';
$url .= isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : '';
$url .= isset($parsed_url['host']) ? $parsed_url['host'] : '';
$url .= $parsed_url['host'] ?? '';
$url .= isset($parsed_url['port']) ? ':'.$parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$user = $parsed_url['user'] ?? '';
$pass = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : '';
$url .= $user.(($user || $pass) ? "$pass@" : '');

if (!empty($url)) {
$url .= isset($parsed_url['path']) ? '/'.ltrim($parsed_url['path'], '/') : '';
} else {
$url .= isset($parsed_url['path']) ? $parsed_url['path'] : '';
$url .= $parsed_url['path'] ?? '';
}

$url .= isset($parsed_url['query']) ? '?'.$parsed_url['query'] : '';
Expand All @@ -889,7 +882,7 @@ protected function unparseUrl($parsed_url)
*/
protected function normalizeAttributes($attributes)
{
if (array_key_exists('data', $attributes) && is_array($attributes['data']) && ! count($attributes['data'])) {
if (array_key_exists('data', $attributes) && \is_array($attributes['data']) && ! \count($attributes['data'])) {
$attributes['data'] = null;
return $attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

class LaravelLocalizationServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class LaravelLocalizationRedirectFilter extends LaravelLocalizationMiddlewareBase
{
Expand All @@ -29,7 +28,7 @@ public function handle($request, Closure $next)
// Dump the first element (empty string) as getPathInfo() always returns a leading slash
array_shift($params);

if (count($params) > 0) {
if (\count($params) > 0) {
$localeCode = $params[0];
$locales = app('laravellocalization')->getSupportedLocales();
$hideDefaultLocale = app('laravellocalization')->hideDefaultLocaleInURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Mcamara\LaravelLocalization\Middleware;

use Closure;
use Illuminate\Http\Request;

class LaravelLocalizationRoutes extends LaravelLocalizationMiddlewareBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;

class LaravelLocalizationViewPath extends LaravelLocalizationMiddlewareBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Mcamara\LaravelLocalization\Middleware;

use Illuminate\Http\RedirectResponse;
use Closure;
use Illuminate\Http\RedirectResponse;

class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
{
Expand All @@ -23,7 +23,7 @@ public function handle($request, Closure $next) {
$params = explode('/', $request->path());
$locale = $request->cookie('locale', false);

if (count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle($request, Closure $next)
$params = explode('/', $request->path());
$locale = session('locale', false);

if (count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
session(['locale' => $params[0]]);

return $next($request);
Expand Down

0 comments on commit 568d319

Please sign in to comment.