diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..8d63d82 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,27 @@ +name: static-analysis + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + phpstan: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.4 + extensions: intl + coverage: none + + - name: Install dependencies + uses: ramsey/composer-install@v3 + + - name: Run PHPStan + run: vendor/bin/phpstan --error-format=github diff --git a/composer.json b/composer.json index aa54c99..ff8018b 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ }, "require-dev": { "orchestra/testbench-browser-kit": "^8.5|^9.0", - "phpunit/phpunit": "^10.1" + "phpunit/phpunit": "^10.1", + "phpstan/phpstan": "^2.0" }, "suggest": { "ext-intl": "*" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..ff776e8 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: 5 + paths: + - src + - tests + ignoreErrors: + - '#Call to an undefined method Illuminate\\Contracts\\Translation\\Translator::has#' + - '#Call to an undefined static method Illuminate\\Support\\Facades\\Route::localized#' + - '#Argument of an invalid type Illuminate\\Routing\\RouteCollectionInterface supplied for foreach, only iterables are supported#' \ No newline at end of file diff --git a/src/Mcamara/LaravelLocalization/LanguageNegotiator.php b/src/Mcamara/LaravelLocalization/LanguageNegotiator.php index 1601753..4c513d3 100644 --- a/src/Mcamara/LaravelLocalization/LanguageNegotiator.php +++ b/src/Mcamara/LaravelLocalization/LanguageNegotiator.php @@ -20,7 +20,7 @@ class LanguageNegotiator /** * Illuminate request class. * - * @var Illuminate\Foundation\Application + * @var \Illuminate\Foundation\Application */ protected $app; diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 8cd2215..66b185c 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -47,7 +47,7 @@ class LaravelLocalization /** * Current locale. * - * @var string + * @var string|false */ protected $currentLocale = false; @@ -267,7 +267,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 ($this->translator->has($transKeyName, $locale)) { $translation = $this->translator->get($transKeyName, [], $locale); $route .= '/'.$translation; @@ -480,7 +480,7 @@ public function getCurrentLocale() /** * Returns current regional. * - * @return string current regional + * @return string|null current regional */ public function getCurrentLocaleRegional(): string|null { diff --git a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php index e12f908..79165d8 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php @@ -8,9 +8,9 @@ class LaravelLocalizationMiddlewareBase /** * The URIs that should not be localized. * - * @var array + * @var array|null */ - protected $except; + protected $except = null; /** * Determine if the request has a URI that should not be localized. diff --git a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php index 9eb396b..214fb79 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php @@ -24,9 +24,9 @@ public function handle($request, Closure $next) } $params = explode('/', $request->path()); - $locale = $request->cookie('locale', false); + $locale = $request->cookie('locale') ?? false; - if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) { + if (app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) { return $next($request)->withCookie(cookie()->forever('locale', $params[0])); } diff --git a/src/Mcamara/LaravelLocalization/Middleware/LocaleSessionRedirect.php b/src/Mcamara/LaravelLocalization/Middleware/LocaleSessionRedirect.php index 93f9bf7..e8bbf8b 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LocaleSessionRedirect.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LocaleSessionRedirect.php @@ -26,7 +26,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 (app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) { session(['locale' => $params[0]]); return $next($request); diff --git a/tests/LaravelLocalizationTest.php b/tests/LaravelLocalizationTest.php index 93bc8e7..b9b814a 100644 --- a/tests/LaravelLocalizationTest.php +++ b/tests/LaravelLocalizationTest.php @@ -65,14 +65,14 @@ protected function setUpRoutes(): void /** * Create fake request - * @param [type] $method [description] - * @param [type] $content [description] + * @param string $method [description] + * @param mixed $content [description] * @param string $uri [description] * @param array $server [description] * @param array $parameters [description] * @param array $cookies [description] * @param array $files [description] - * @return [type] [description] + * @return \Illuminate\Http\Request [description] */ protected function createRequest( $uri = '/test', @@ -101,7 +101,7 @@ protected function createRequest( /** * Define environment setup. * - * @param Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * * @return void */