From f941df3a82e6856a1dc299ff89e063c2baaed4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 1 Sep 2022 19:06:54 +0200 Subject: [PATCH] minor improvements for phpstan --- src/Features/CrossDomainRedirect.php | 11 ++++++++--- src/Tenancy.php | 2 +- src/helpers.php | 12 ++++++++---- tests/TestCase.php | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Features/CrossDomainRedirect.php b/src/Features/CrossDomainRedirect.php index 0b6d7682..4efc767b 100644 --- a/src/Features/CrossDomainRedirect.php +++ b/src/Features/CrossDomainRedirect.php @@ -15,11 +15,16 @@ class CrossDomainRedirect implements Feature RedirectResponse::macro('domain', function (string $domain) { /** @var RedirectResponse $this */ - // Replace first occurrence of the hostname fragment with $domain $url = $this->getTargetUrl(); + + /** + * The original hostname in the redirect response. + * + * @var string $hostname + */ $hostname = parse_url($url, PHP_URL_HOST); - $position = strpos($url, $hostname); - $this->setTargetUrl(substr_replace($url, $domain, $position, strlen($hostname))); + + $this->setTargetUrl((string) str($url)->replace($hostname, $domain)); return $this; }); diff --git a/src/Tenancy.php b/src/Tenancy.php index 012881ae..0a8d4542 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -146,7 +146,7 @@ class Tenancy $tenants = is_string($tenants) ? [$tenants] : $tenants; // Use all tenants if $tenants is falsey - $tenants = $tenants ?: $this->model()->cursor(); + $tenants = $tenants ?: $this->model()->cursor(); // todo0 phpstan thinks this isn't needed, but tests fail without it $originalTenant = $this->tenant; diff --git a/src/helpers.php b/src/helpers.php index 23b5a627..ac805aa5 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -58,11 +58,15 @@ if (! function_exists('global_cache')) { if (! function_exists('tenant_route')) { function tenant_route(string $domain, string $route, array $parameters = [], bool $absolute = true): string { - // replace the first occurrence of the hostname fragment with $domain $url = route($route, $parameters, $absolute); - $hostname = parse_url($url, PHP_URL_HOST); - $position = strpos($url, $hostname); - return substr_replace($url, $domain, $position, strlen($hostname)); + /** + * The original hostname in the generated route. + * + * @var string $hostname + */ + $hostname = parse_url($url, PHP_URL_HOST); + + return (string) str($url)->replace($hostname, $domain); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1c6c6d8a..67029422 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -96,7 +96,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase '--realpath' => true, '--force' => true, ], - 'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, + 'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // todo0 change this to []? two tests in TenantDatabaseManagerTest are failing with that 'queue.connections.central' => [ 'driver' => 'sync', 'central' => true,