Skip to content

Commit 0ced0d6

Browse files
committed
refactor: optimize imports by converting fully qualified names to use statements
- Add fully_qualified_strict_types rule to PHP CS Fixer config with import_symbols enabled - Automatically convert fully qualified class names to use statements - Fix ArrayCache import in CacheFactory manually (classes from tests namespace) - Apply rule to all src and tests files (74 files fixed)
1 parent aafdde5 commit 0ced0d6

File tree

105 files changed

+917
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+917
-756
lines changed

.php-cs-fixer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
'imports_order' => ['class', 'function', 'const'],
4040
'sort_algorithm' => 'alpha'
4141
],
42+
'fully_qualified_strict_types' => [
43+
'import_symbols' => true,
44+
'leading_backslash_in_global_namespace' => false,
45+
],
4246
'phpdoc_align' => ['align' => 'left'],
4347
'phpdoc_indent' => true,
4448
'phpdoc_no_empty_return' => true,

src/cache/CacheFactory.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
namespace tommyknocker\pdodb\cache;
66

7+
use Predis\Client;
78
use Psr\SimpleCache\CacheInterface;
9+
use Symfony\Component\Cache\Adapter\ApcuAdapter;
10+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
11+
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
12+
use Symfony\Component\Cache\Adapter\RedisAdapter;
13+
use Symfony\Component\Cache\Psr16Cache;
14+
use tommyknocker\pdodb\tests\fixtures\ArrayCache;
815

916
/**
1017
* Factory for creating cache adapters from configuration.
@@ -44,7 +51,7 @@ public static function create(array $config): ?CacheInterface
4451
*/
4552
public static function createFilesystemCache(array $config): ?CacheInterface
4653
{
47-
if (!class_exists(\Symfony\Component\Cache\Adapter\FilesystemAdapter::class)) {
54+
if (!class_exists(FilesystemAdapter::class)) {
4855
return null;
4956
}
5057

@@ -61,9 +68,9 @@ public static function createFilesystemCache(array $config): ?CacheInterface
6168
$defaultLifetime = $config['default_lifetime'] ?? $config['ttl'] ?? 0;
6269
$defaultLifetime = is_int($defaultLifetime) ? $defaultLifetime : (is_numeric($defaultLifetime) ? (int)$defaultLifetime : 0);
6370

64-
$adapter = new \Symfony\Component\Cache\Adapter\FilesystemAdapter($namespace, $defaultLifetime, $directory);
71+
$adapter = new FilesystemAdapter($namespace, $defaultLifetime, $directory);
6572

66-
return new \Symfony\Component\Cache\Psr16Cache($adapter);
73+
return new Psr16Cache($adapter);
6774
}
6875

6976
/**
@@ -75,7 +82,7 @@ public static function createFilesystemCache(array $config): ?CacheInterface
7582
*/
7683
public static function createApcuCache(array $config): ?CacheInterface
7784
{
78-
if (!extension_loaded('apcu') || !class_exists(\Symfony\Component\Cache\Adapter\ApcuAdapter::class)) {
85+
if (!extension_loaded('apcu') || !class_exists(ApcuAdapter::class)) {
7986
return null;
8087
}
8188

@@ -88,12 +95,12 @@ public static function createApcuCache(array $config): ?CacheInterface
8895
$version = $config['version'] ?? null;
8996
$version = is_string($version) ? $version : null;
9097

91-
/** @var class-string<\Symfony\Component\Cache\Adapter\ApcuAdapter> $adapterClass */
92-
$adapterClass = \Symfony\Component\Cache\Adapter\ApcuAdapter::class;
98+
/** @var class-string<ApcuAdapter> $adapterClass */
99+
$adapterClass = ApcuAdapter::class;
93100
$adapter = new $adapterClass($namespace, $defaultLifetime, $version);
94101

95-
/** @var class-string<\Symfony\Component\Cache\Psr16Cache> $cacheClass */
96-
$cacheClass = \Symfony\Component\Cache\Psr16Cache::class;
102+
/** @var class-string<Psr16Cache> $cacheClass */
103+
$cacheClass = Psr16Cache::class;
97104
return new $cacheClass($adapter);
98105
}
99106

@@ -106,22 +113,22 @@ public static function createApcuCache(array $config): ?CacheInterface
106113
*/
107114
public static function createRedisCache(array $config): ?CacheInterface
108115
{
109-
if (!class_exists(\Symfony\Component\Cache\Adapter\RedisAdapter::class)) {
116+
if (!class_exists(RedisAdapter::class)) {
110117
return null;
111118
}
112119

113120
// Try to use provided Redis connection
114121
$redisConnection = $config['redis'] ?? null;
115122
if ($redisConnection !== null) {
116123
$isRedis = $redisConnection instanceof \Redis;
117-
$isPredis = class_exists(\Predis\Client::class) && $redisConnection instanceof \Predis\Client;
124+
$isPredis = class_exists(Client::class) && $redisConnection instanceof Client;
118125
if ($isRedis || $isPredis) {
119-
/** @var class-string<\Symfony\Component\Cache\Adapter\RedisAdapter> $adapterClass */
120-
$adapterClass = \Symfony\Component\Cache\Adapter\RedisAdapter::class;
126+
/** @var class-string<RedisAdapter> $adapterClass */
127+
$adapterClass = RedisAdapter::class;
121128
$adapter = new $adapterClass($redisConnection);
122129

123-
/** @var class-string<\Symfony\Component\Cache\Psr16Cache> $cacheClass */
124-
$cacheClass = \Symfony\Component\Cache\Psr16Cache::class;
130+
/** @var class-string<Psr16Cache> $cacheClass */
131+
$cacheClass = Psr16Cache::class;
125132
// phpstan-ignore-next-line - adapter is CacheItemPoolInterface from RedisAdapter
126133
return new $cacheClass($adapter);
127134
}
@@ -172,12 +179,12 @@ public static function createRedisCache(array $config): ?CacheInterface
172179
$defaultLifetime = $config['default_lifetime'] ?? $config['ttl'] ?? 0;
173180
$defaultLifetime = is_int($defaultLifetime) ? $defaultLifetime : (is_numeric($defaultLifetime) ? (int)$defaultLifetime : 0);
174181

175-
/** @var class-string<\Symfony\Component\Cache\Adapter\RedisAdapter> $adapterClass */
176-
$adapterClass = \Symfony\Component\Cache\Adapter\RedisAdapter::class;
182+
/** @var class-string<RedisAdapter> $adapterClass */
183+
$adapterClass = RedisAdapter::class;
177184
$adapter = new $adapterClass($redis, $namespace, $defaultLifetime);
178185

179-
/** @var class-string<\Symfony\Component\Cache\Psr16Cache> $cacheClass */
180-
$cacheClass = \Symfony\Component\Cache\Psr16Cache::class;
186+
/** @var class-string<Psr16Cache> $cacheClass */
187+
$cacheClass = Psr16Cache::class;
181188
// phpstan-ignore-next-line - adapter is CacheItemPoolInterface from RedisAdapter
182189
return new $cacheClass($adapter);
183190
}
@@ -191,7 +198,7 @@ public static function createRedisCache(array $config): ?CacheInterface
191198
*/
192199
public static function createMemcachedCache(array $config): ?CacheInterface
193200
{
194-
if (!extension_loaded('memcached') || !class_exists(\Symfony\Component\Cache\Adapter\MemcachedAdapter::class)) {
201+
if (!extension_loaded('memcached') || !class_exists(MemcachedAdapter::class)) {
195202
return null;
196203
}
197204

@@ -204,12 +211,12 @@ public static function createMemcachedCache(array $config): ?CacheInterface
204211
$defaultLifetime = $config['default_lifetime'] ?? $config['ttl'] ?? 0;
205212
$defaultLifetime = is_int($defaultLifetime) ? $defaultLifetime : (is_numeric($defaultLifetime) ? (int)$defaultLifetime : 0);
206213

207-
/** @var class-string<\Symfony\Component\Cache\Adapter\MemcachedAdapter> $adapterClass */
208-
$adapterClass = \Symfony\Component\Cache\Adapter\MemcachedAdapter::class;
214+
/** @var class-string<MemcachedAdapter> $adapterClass */
215+
$adapterClass = MemcachedAdapter::class;
209216
$adapter = new $adapterClass($memcachedConnection, $namespace, $defaultLifetime);
210217

211-
/** @var class-string<\Symfony\Component\Cache\Psr16Cache> $cacheClass */
212-
$cacheClass = \Symfony\Component\Cache\Psr16Cache::class;
218+
/** @var class-string<Psr16Cache> $cacheClass */
219+
$cacheClass = Psr16Cache::class;
213220
return new $cacheClass($adapter);
214221
}
215222

@@ -241,16 +248,16 @@ public static function createMemcachedCache(array $config): ?CacheInterface
241248
$defaultLifetime = $config['default_lifetime'] ?? $config['ttl'] ?? 0;
242249
$defaultLifetime = is_int($defaultLifetime) ? $defaultLifetime : (is_numeric($defaultLifetime) ? (int)$defaultLifetime : 0);
243250

244-
/** @var class-string<\Symfony\Component\Cache\Adapter\MemcachedAdapter> $adapterClass */
245-
$adapterClass = \Symfony\Component\Cache\Adapter\MemcachedAdapter::class;
251+
/** @var class-string<MemcachedAdapter> $adapterClass */
252+
$adapterClass = MemcachedAdapter::class;
246253

247254
try {
248255
// createConnection returns Memcached instance, need to wrap it in adapter
249256
$memcached = $adapterClass::createConnection($serverList);
250257
$adapter = new $adapterClass($memcached, $namespace, $defaultLifetime);
251258

252-
/** @var class-string<\Symfony\Component\Cache\Psr16Cache> $cacheClass */
253-
$cacheClass = \Symfony\Component\Cache\Psr16Cache::class;
259+
/** @var class-string<Psr16Cache> $cacheClass */
260+
$cacheClass = Psr16Cache::class;
254261
return new $cacheClass($adapter);
255262
} catch (\Exception $e) {
256263
// Memcached server not available or connection failed
@@ -268,8 +275,8 @@ public static function createMemcachedCache(array $config): ?CacheInterface
268275
public static function createArrayCache(array $config): ?CacheInterface
269276
{
270277
// Use test fixture if available
271-
if (class_exists(\tommyknocker\pdodb\tests\fixtures\ArrayCache::class)) {
272-
return new \tommyknocker\pdodb\tests\fixtures\ArrayCache();
278+
if (class_exists(ArrayCache::class)) {
279+
return new ArrayCache();
273280
}
274281

275282
// Fallback: try to create simple array-based cache

0 commit comments

Comments
 (0)