From 84890cdd1e239394567d3c609023c26b4dce497b Mon Sep 17 00:00:00 2001 From: stancl Date: Fri, 16 Aug 2019 16:21:59 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/CacheManager.php | 2 +- src/Commands/Install.php | 6 ++-- src/Commands/Run.php | 8 ++--- src/DatabaseManager.php | 4 +-- src/StorageDrivers/RedisStorageDriver.php | 14 ++++---- src/Tenant.php | 14 ++++---- .../SQLiteDatabaseManager.php | 4 +-- src/TenantManager.php | 32 +++++++++---------- src/TenantRouteServiceProvider.php | 2 +- src/Traits/BootstrapsTenancy.php | 2 +- src/Traits/HasATenantsOption.php | 2 +- src/Traits/TenantManagerEvents.php | 2 +- src/helpers.php | 6 ++-- tests/BootstrapsTenancyTest.php | 2 +- tests/CommandsTest.php | 10 +++--- tests/ReidentificationTest.php | 2 +- tests/TenantAssetTest.php | 6 ++-- tests/TenantDatabaseManagerTest.php | 4 +-- tests/TenantStorageTest.php | 14 ++++---- tests/TestCase.php | 10 +++--- 20 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index d3b31ea2..52952fe7 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -18,7 +18,7 @@ class CacheManager extends BaseCacheManager $names = $parameters[0]; $names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items - return $this->store()->tags(array_merge($tags, $names)); + return $this->store()->tags(\array_merge($tags, $names)); } return $this->store()->tags($tags)->$method(...$parameters); diff --git a/src/Commands/Install.php b/src/Commands/Install.php index de1a81b2..a2f162e5 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -34,14 +34,14 @@ class Install extends Command ]); $this->info('✔️ Created config/tenancy.php'); - file_put_contents(app_path('Http/Kernel.php'), str_replace( + \file_put_contents(app_path('Http/Kernel.php'), \str_replace( 'protected $middlewarePriority = [', "protected \$middlewarePriority = [\n \Stancl\Tenancy\Middleware\InitializeTenancy::class,", - file_get_contents(app_path('Http/Kernel.php')) + \file_get_contents(app_path('Http/Kernel.php')) )); $this->info('✔️ Set middleware priority'); - file_put_contents(base_path('routes/tenant.php'), + \file_put_contents(base_path('routes/tenant.php'), " 'bar', 'abc' => 'xyz=zzz'] - $arguments = array_reduce($this->option('argument'), $callback(), []); + $arguments = \array_reduce($this->option('argument'), $callback(), []); // Turns ['foo=bar', 'abc=xyz=zzz'] into ['--foo' => 'bar', '--abc' => 'xyz=zzz'] - $options = array_reduce($this->option('option'), $callback('--'), []); + $options = \array_reduce($this->option('option'), $callback('--'), []); // Run command - $this->call($this->argument('commandname'), array_merge($arguments, $options)); + $this->call($this->argument('commandname'), \array_merge($arguments, $options)); tenancy()->end(); }); diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 68e0cde0..b8152194 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -50,7 +50,7 @@ final class DatabaseManager $databaseManagers = config('tenancy.database_managers'); - if (! array_key_exists($driver, $databaseManagers)) { + if (! \array_key_exists($driver, $databaseManagers)) { throw new \Exception("Database could not be created: no database manager for driver $driver is registered."); } @@ -76,7 +76,7 @@ final class DatabaseManager $databaseManagers = config('tenancy.database_managers'); - if (! array_key_exists($driver, $databaseManagers)) { + if (! \array_key_exists($driver, $databaseManagers)) { throw new \Exception("Database could not be deleted: no database manager for driver $driver is registered."); } diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 63c087af..9866018c 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -37,7 +37,7 @@ class RedisStorageDriver implements StorageDriver return $this->redis->hgetall("tenants:$uuid"); } - return array_combine($fields, $this->redis->hmget("tenants:$uuid", $fields)); + return \array_combine($fields, $this->redis->hmget("tenants:$uuid", $fields)); } public function getTenantIdByDomain(string $domain): ?string @@ -48,7 +48,7 @@ class RedisStorageDriver implements StorageDriver public function createTenant(string $domain, string $uuid): array { $this->redis->hmset("domains:$domain", 'tenant_id', $uuid); - $this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain)); + $this->redis->hmset("tenants:$uuid", 'uuid', \json_encode($uuid), 'domain', \json_encode($domain)); return $this->redis->hgetall("tenants:$uuid"); } @@ -63,7 +63,7 @@ class RedisStorageDriver implements StorageDriver public function deleteTenant(string $id): bool { try { - $domain = json_decode($this->getTenantById($id)['domain']); + $domain = \json_decode($this->getTenantById($id)['domain']); } catch (\Throwable $th) { throw new \Exception("No tenant with UUID $id exists."); } @@ -75,7 +75,7 @@ class RedisStorageDriver implements StorageDriver public function getAllTenants(array $uuids = []): array { - $hashes = array_map(function ($hash) { + $hashes = \array_map(function ($hash) { return "tenants:{$hash}"; }, $uuids); @@ -91,13 +91,13 @@ class RedisStorageDriver implements StorageDriver $all_keys = $this->redis->scan(null, 'MATCH', $redis_prefix . 'tenants:*')[1]; } - $hashes = array_map(function ($key) use ($redis_prefix) { + $hashes = \array_map(function ($key) use ($redis_prefix) { // Left strip $redis_prefix from $key - return substr($key, strlen($redis_prefix)); + return \substr($key, \strlen($redis_prefix)); }, $all_keys); } - return array_map(function ($tenant) { + return \array_map(function ($tenant) { return $this->redis->hgetall($tenant); }, $hashes); } diff --git a/src/Tenant.php b/src/Tenant.php index 03fbeb71..db9997d0 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -54,14 +54,14 @@ class Tenant extends Model public static function decodeData($tenant) { $tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant; - $decoded = json_decode($tenant[$dataColumn = static::dataColumn()], true); + $decoded = \json_decode($tenant[$dataColumn = static::dataColumn()], true); foreach ($decoded as $key => $value) { $tenant[$key] = $value; } // If $tenant[$dataColumn] has been overriden by a value, don't delete the key. - if (! array_key_exists($dataColumn, $decoded)) { + if (! \array_key_exists($dataColumn, $decoded)) { unset($tenant[$dataColumn]); } @@ -70,7 +70,7 @@ class Tenant extends Model public function getFromData(string $key) { - $this->dataArray = $this->dataArray ?? json_decode($this->{$this->dataColumn()}, true); + $this->dataArray = $this->dataArray ?? \json_decode($this->{$this->dataColumn()}, true); return $this->dataArray[$key] ?? null; } @@ -83,18 +83,18 @@ class Tenant extends Model /** @todo In v2, this should return an associative array. */ public function getMany(array $keys): array { - return array_map([$this, 'get'], $keys); + return \array_map([$this, 'get'], $keys); } public function put(string $key, $value) { - if (array_key_exists($key, $this->customColumns())) { + if (\array_key_exists($key, $this->customColumns())) { $this->update([$key => $value]); } else { - $obj = json_decode($this->{$this->dataColumn()}); + $obj = \json_decode($this->{$this->dataColumn()}); $obj->$key = $value; - $this->update([$this->dataColumn() => json_encode($obj)]); + $this->update([$this->dataColumn() => \json_encode($obj)]); } return $value; diff --git a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php index ca6d027a..cdfc094b 100644 --- a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php +++ b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php @@ -9,7 +9,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager public function createDatabase(string $name): bool { try { - return fclose(fopen(database_path($name), 'w')); + return \fclose(\fopen(database_path($name), 'w')); } catch (\Throwable $th) { return false; } @@ -18,7 +18,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager public function deleteDatabase(string $name): bool { try { - return unlink(database_path($name)); + return \unlink(database_path($name)); } catch (\Throwable $th) { return false; } diff --git a/src/TenantManager.php b/src/TenantManager.php index 8da49762..6e27ee1a 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -64,7 +64,7 @@ final class TenantManager $tenant = $this->storage->identifyTenant($domain); - if (! $tenant || ! array_key_exists('uuid', $tenant) || ! $tenant['uuid']) { + if (! $tenant || ! \array_key_exists('uuid', $tenant) || ! $tenant['uuid']) { throw new \Exception("Tenant could not be identified on domain {$domain}."); } @@ -94,7 +94,7 @@ final class TenantManager if ($data) { $this->put($data, null, $tenant['uuid']); - $tenant = array_merge($tenant, $data); + $tenant = \array_merge($tenant, $data); } $this->database->create($this->getDatabaseName($tenant)); @@ -175,7 +175,7 @@ final class TenantManager $uuid = $this->getIdByDomain($domain); - if (is_null($uuid)) { + if (\is_null($uuid)) { throw new \Exception("Tenant with domain $domain could not be identified."); } @@ -240,7 +240,7 @@ final class TenantManager $tenants = $this->storage->getAllTenants($uuids); if ($this->useJson()) { - $tenants = array_map(function ($tenant_array) { + $tenants = \array_map(function ($tenant_array) { return $this->jsonDecodeArrayValues($tenant_array); }, $tenants); } @@ -273,8 +273,8 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; - if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && - array_key_exists($key, $this->tenant) && ! is_array($key)) { + if (\array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && + \array_key_exists($key, $this->tenant) && ! \is_array($key)) { return $this->tenant[$key]; } @@ -282,7 +282,7 @@ final class TenantManager return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); } - return json_decode($this->storage->get($uuid, $key), true); + return \json_decode($this->storage->get($uuid, $key), true); } /** @@ -295,10 +295,10 @@ final class TenantManager */ public function put($key, $value = null, string $uuid = null) { - if (in_array($key, ['uuid', 'domain'], true) || ( - is_array($key) && ( - in_array('uuid', array_keys($key), true) || - in_array('domain', array_keys($key), true) + if (\in_array($key, ['uuid', 'domain'], true) || ( + \is_array($key) && ( + \in_array('uuid', \array_keys($key), true) || + \in_array('domain', \array_keys($key), true) ) )) { throw new CannotChangeUuidOrDomainException; @@ -319,7 +319,7 @@ final class TenantManager } if (! \is_null($value)) { - return $target[$key] = json_decode($this->storage->put($uuid, $key, json_encode($value)), true); + return $target[$key] = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true); } if (! \is_array($key)) { @@ -328,7 +328,7 @@ final class TenantManager foreach ($key as $k => $v) { $target[$k] = $v; - $key[$k] = json_encode($v); + $key[$k] = \json_encode($v); } return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); @@ -349,8 +349,8 @@ final class TenantManager protected function jsonDecodeArrayValues(array $array) { - array_walk($array, function (&$value, $key) { - $value = json_decode($value, true); + \array_walk($array, function (&$value, $key) { + $value = \json_decode($value, true); }); return $array; @@ -358,7 +358,7 @@ final class TenantManager public function useJson() { - if (property_exists($this->storage, 'useJson') && $this->storage->useJson === false) { + if (\property_exists($this->storage, 'useJson') && $this->storage->useJson === false) { return false; } diff --git a/src/TenantRouteServiceProvider.php b/src/TenantRouteServiceProvider.php index 4ea77402..e6551756 100644 --- a/src/TenantRouteServiceProvider.php +++ b/src/TenantRouteServiceProvider.php @@ -10,7 +10,7 @@ class TenantRouteServiceProvider extends RouteServiceProvider public function map() { if (! \in_array(request()->getHost(), $this->app['config']['tenancy.exempt_domains'] ?? []) - && file_exists(base_path('routes/tenant.php'))) { + && \file_exists(base_path('routes/tenant.php'))) { Route::middleware(['web', 'tenancy']) ->namespace($this->app['config']['tenant_route_namespace'] ?? 'App\Http\Controllers') ->group(base_path('routes/tenant.php')); diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index b7188968..5e45c3bc 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -138,7 +138,7 @@ trait BootstrapsTenancy foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { $old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix(); - if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { + if ($root = \str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { Storage::disk($disk)->getAdapter()->setPathPrefix($root); } else { $root = $this->app['config']["filesystems.disks.{$disk}.root"]; diff --git a/src/Traits/HasATenantsOption.php b/src/Traits/HasATenantsOption.php index 1dc055af..1c37cfe1 100644 --- a/src/Traits/HasATenantsOption.php +++ b/src/Traits/HasATenantsOption.php @@ -8,7 +8,7 @@ trait HasATenantsOption { protected function getOptions() { - return array_merge([ + return \array_merge([ ['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null], ], parent::getOptions()); } diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php index 416ebd72..38e303a0 100644 --- a/src/Traits/TenantManagerEvents.php +++ b/src/Traits/TenantManagerEvents.php @@ -78,7 +78,7 @@ trait TenantManagerEvents */ public function event(string $name): Collection { - return array_reduce($this->listeners[$name], function ($prevents, $listener) { + return \array_reduce($this->listeners[$name], function ($prevents, $listener) { return $prevents->merge($listener($this) ?? []); }, collect([])); } diff --git a/src/helpers.php b/src/helpers.php index 0041f131..29cb3411 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,7 +2,7 @@ use Stancl\Tenancy\TenantManager; -if (! function_exists('tenancy')) { +if (! \function_exists('tenancy')) { function tenancy($key = null) { if ($key) { @@ -13,14 +13,14 @@ if (! function_exists('tenancy')) { } } -if (! function_exists('tenant')) { +if (! \function_exists('tenant')) { function tenant($key = null) { return tenancy($key); } } -if (! function_exists('tenant_asset')) { +if (! \function_exists('tenant_asset')) { function tenant_asset($asset) { return route('stancl.tenancy.asset', ['asset' => $asset]); diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index fc0d761f..1f21b858 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -86,7 +86,7 @@ class BootstrapsTenancyTest extends TestCase $current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix(); if ($override = config("tenancy.filesystem.root_override.{$disk}")) { - $correct_path_prefix = str_replace('%storage_path%', storage_path(), $override); + $correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override); } else { if ($base = $old_storage_facade_roots[$disk]) { $correct_path_prefix = $base . "/$suffix/"; diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 6d2a8e41..6b161738 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -117,14 +117,14 @@ class CommandsTest extends TestCase /** @test */ public function install_command_works() { - if (! is_dir($dir = app_path('Http'))) { - mkdir($dir, 0777, true); + if (! \is_dir($dir = app_path('Http'))) { + \mkdir($dir, 0777, true); } - if (! is_dir($dir = base_path('routes'))) { - mkdir($dir, 0777, true); + if (! \is_dir($dir = base_path('routes'))) { + \mkdir($dir, 0777, true); } - file_put_contents(app_path('Http/Kernel.php'), "getAdapter()->getPathPrefix(); if ($override = config("tenancy.filesystem.root_override.{$disk}")) { - $correct_path_prefix = str_replace('%storage_path%', storage_path(), $override); + $correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override); } else { if ($base = $originals[$disk]) { $correct_path_prefix = $base . "/$suffix/"; diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 01f1fd28..b43d43c6 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -16,9 +16,9 @@ class TenantAssetTest extends TestCase $this->get(tenant_asset($filename))->assertSuccessful(); $this->assertFileExists($path); - $f = fopen($path, 'r'); - $content = fread($f, filesize($path)); - fclose($f); + $f = \fopen($path, 'r'); + $content = \fread($f, \filesize($path)); + \fclose($f); $this->assertSame('bar', $content); } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 9f6e911b..533c6eee 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -87,7 +87,7 @@ class TenantDatabaseManagerTest extends TestCase config()->set('database.default', 'pgsql'); - $db_name = strtolower('testdatabase' . $this->randomString(10)); + $db_name = \strtolower('testdatabase' . $this->randomString(10)); $this->assertTrue(app(DatabaseManager::class)->create($db_name, 'pgsql')); $this->assertNotEmpty(DB::select("SELECT datname FROM pg_database WHERE datname = '$db_name'")); @@ -104,7 +104,7 @@ class TenantDatabaseManagerTest extends TestCase config()->set('database.default', 'pgsql'); - $db_name = strtolower('testdatabase' . $this->randomString(10)); + $db_name = \strtolower('testdatabase' . $this->randomString(10)); $databaseManagers = config('tenancy.database_managers'); $job = new QueuedTenantDatabaseCreator(app($databaseManagers['pgsql']), $db_name); diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 7999f37c..79001bdd 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -40,7 +40,7 @@ class TenantStorageTest extends TestCase { $keys = ['foo', 'abc']; $vals = ['bar', 'xyz']; - $data = array_combine($keys, $vals); + $data = \array_combine($keys, $vals); tenancy()->put($data); dd(tenant()->get($keys)); @@ -76,13 +76,13 @@ class TenantStorageTest extends TestCase $keys = ['foo', 'abc']; $vals = ['bar', 'xyz']; - $data = array_combine($keys, $vals); + $data = \array_combine($keys, $vals); tenancy()->put($data, null, $uuid); $this->assertSame($vals, tenancy()->get($keys, $uuid)); $this->assertNotSame($vals, tenancy()->get($keys)); - $this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset + $this->assertFalse(\array_intersect($data, tenant()->tenant) == $data); // assert array not subset } /** @test */ @@ -130,15 +130,15 @@ class TenantStorageTest extends TestCase public function data_is_stored_with_correct_data_types() { tenancy()->put('someBool', false); - $this->assertSame('boolean', gettype(tenancy()->get('someBool'))); + $this->assertSame('boolean', \gettype(tenancy()->get('someBool'))); tenancy()->put('someInt', 5); - $this->assertSame('integer', gettype(tenancy()->get('someInt'))); + $this->assertSame('integer', \gettype(tenancy()->get('someInt'))); tenancy()->put('someDouble', 11.40); - $this->assertSame('double', gettype(tenancy()->get('someDouble'))); + $this->assertSame('double', \gettype(tenancy()->get('someDouble'))); tenancy()->put('string', 'foo'); - $this->assertSame('string', gettype(tenancy()->get('string'))); + $this->assertSame('string', \gettype(tenancy()->get('string'))); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 9cafb202..b4f39982 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,7 +24,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase Redis::connection('cache')->flushdb(); $this->loadMigrationsFrom([ - '--path' => realpath(__DIR__ . '/../assets/migrations'), + '--path' => \realpath(__DIR__ . '/../assets/migrations'), '--database' => 'central', ]); config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom @@ -63,11 +63,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase */ protected function getEnvironmentSetUp($app) { - if (file_exists(__DIR__ . '/../.env')) { + if (\file_exists(__DIR__ . '/../.env')) { \Dotenv\Dotenv::create(__DIR__ . '/..')->load(); } - fclose(fopen(database_path('central.sqlite'), 'w')); + \fclose(\fopen(database_path('central.sqlite'), 'w')); $app['config']->set([ 'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'), @@ -160,7 +160,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase public function randomString(int $length = 10) { - return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length); + return \substr(\str_shuffle(\str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', \ceil($length / \strlen($x)))), 1, $length); } public function isContainerized() @@ -170,6 +170,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase public function assertArrayIsSubset($subset, $array, string $message = ''): void { - parent::assertTrue(array_intersect($subset, $array) == $subset, $message); + parent::assertTrue(\array_intersect($subset, $array) == $subset, $message); } }