Skip to content

Commit fdc34d9

Browse files
authored
Replace StyleCI with PHP CS Fixer (#41)
1 parent 0886627 commit fdc34d9

11 files changed

Lines changed: 145 additions & 185 deletions

.github/workflows/rector-cs.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Rector + PHP CS Fixer
2+
3+
on:
4+
pull_request_target:
5+
paths:
6+
- 'src/**'
7+
- 'tests/**'
8+
- '.github/workflows/rector-cs.yml'
9+
- 'composer.json'
10+
- 'rector.php'
11+
- '.php-cs-fixer.dist.php'
12+
13+
permissions:
14+
contents: read
15+
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.ref }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
rector:
22+
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
23+
secrets:
24+
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
25+
with:
26+
repository: ${{ github.event.pull_request.head.repo.full_name }}
27+
php: '8.1'

.github/workflows/rector.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ phpunit.phar
3030
.phpunit.result.cache
3131
# phpunit coverage report
3232
coverage.xml
33+
34+
# PHP CS Fixer
35+
/.php-cs-fixer.cache
36+
/.php-cs-fixer.php

.php-cs-fixer.dist.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\Finder;
7+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
8+
9+
$finder = (new Finder())->in([
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
]);
13+
14+
return (new Config())
15+
->setRiskyAllowed(true)
16+
->setParallelConfig(ParallelConfigFactory::detect())
17+
->setRules([
18+
'@PER-CS3.0' => true,
19+
'no_unused_imports' => true,
20+
'ordered_class_elements' => true,
21+
'class_attributes_separation' => ['elements' => ['method' => 'one']],
22+
'declare_strict_types' => true,
23+
'native_function_invocation' => true,
24+
'native_constant_invocation' => true,
25+
'fully_qualified_strict_types' => [
26+
'import_symbols' => true
27+
],
28+
'global_namespace_import' => [
29+
'import_classes' => true,
30+
'import_constants' => true,
31+
'import_functions' => true,
32+
],
33+
])
34+
->setFinder($finder);

.styleci.yml

Lines changed: 0 additions & 85 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Enh #30: Remove unneeded casting to array in private method `RedisCache::iterableToArray()` (@vjik)
66
- Chg #38: Bump minimum PHP version to `8.1` (@s1lver)
77
- Chg #40: Make `RedisCache::$client` readonly (@s1lver)
8+
- Enh #41: Explicitly import classes and functions in "use" section (@mspirkov)
89

910
## 2.0.0 July 10, 2023
1011

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"psr/simple-cache": "^3.0"
3636
},
3737
"require-dev": {
38+
"friendsofphp/php-cs-fixer": "^3.92.5",
3839
"maglnet/composer-require-checker": "^4.7.1",
3940
"phpunit/phpunit": "^9.5",
4041
"rector/rector": "^2.2.14",

src/InvalidArgumentException.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Cache\Redis;
66

7-
final class InvalidArgumentException extends \RuntimeException implements \Psr\SimpleCache\InvalidArgumentException
8-
{
9-
}
7+
use Psr\SimpleCache\InvalidArgumentException as PsrInvalidArgumentException;
8+
use RuntimeException;
9+
10+
final class InvalidArgumentException extends RuntimeException implements PsrInvalidArgumentException {}

src/RedisCache.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function serialize;
2323
use function strpbrk;
2424
use function unserialize;
25+
use function in_array;
2526

2627
/**
2728
* RedisCache stores cache data in a Redis.
@@ -43,26 +44,6 @@ public function __construct(private readonly ClientInterface $client)
4344
$this->connections = $this->client->getConnection();
4445
}
4546

46-
/**
47-
* Returns whether Predis cluster is used.
48-
*
49-
* @return bool Whether Predis cluster is used.
50-
*/
51-
private function isCluster(): bool
52-
{
53-
/** @psalm-suppress MixedAssignment, PossibleRawObjectIteration */
54-
foreach ($this->connections as $connection) {
55-
/** @var StreamConnection $connection */
56-
$cluster = (new Client($connection->getParameters()))->info('Cluster');
57-
/** @psalm-suppress MixedArrayAccess */
58-
if (isset($cluster['Cluster']['cluster_enabled']) && 1 === (int)$cluster['Cluster']['cluster_enabled']) {
59-
return true;
60-
}
61-
}
62-
63-
return false;
64-
}
65-
6647
public function get(string $key, mixed $default = null): mixed
6748
{
6849
$this->validateKey($key);
@@ -71,7 +52,7 @@ public function get(string $key, mixed $default = null): mixed
7152
return $value === null ? $default : unserialize($value);
7253
}
7354

74-
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
55+
public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool
7556
{
7657
$ttl = $this->normalizeTtl($ttl);
7758

@@ -145,7 +126,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
145126
return $values;
146127
}
147128

148-
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
129+
public function setMultiple(iterable $values, int|DateInterval|null $ttl = null): bool
149130
{
150131
$values = $this->iterableToArray($values);
151132
$keys = array_map(\strval(...), array_keys($values));
@@ -164,7 +145,7 @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null)
164145
$results = [];
165146
if ($this->isCluster()) {
166147
foreach ($serializeValues as $key => $value) {
167-
$this->set((string)$key, $value, $this->isInfinityTtl($ttl) ? null : $ttl);
148+
$this->set((string) $key, $value, $this->isInfinityTtl($ttl) ? null : $ttl);
168149
}
169150
} else {
170151
if ($this->isInfinityTtl($ttl)) {
@@ -176,14 +157,14 @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null)
176157
$this->client->mset($serializeValues);
177158

178159
foreach ($keys as $key) {
179-
$this->client->expire($key, (int)$ttl);
160+
$this->client->expire($key, (int) $ttl);
180161
}
181162

182163
/** @var array|null $results */
183164
$results = $this->client->exec();
184165
}
185166

186-
return !in_array(null, (array)$results, true);
167+
return !in_array(null, (array) $results, true);
187168
}
188169

189170
public function deleteMultiple(iterable $keys): bool
@@ -210,14 +191,34 @@ public function has(string $key): bool
210191
return $ttl > 0 || $ttl === -1;
211192
}
212193

194+
/**
195+
* Returns whether Predis cluster is used.
196+
*
197+
* @return bool Whether Predis cluster is used.
198+
*/
199+
private function isCluster(): bool
200+
{
201+
/** @psalm-suppress MixedAssignment, PossibleRawObjectIteration */
202+
foreach ($this->connections as $connection) {
203+
/** @var StreamConnection $connection */
204+
$cluster = (new Client($connection->getParameters()))->info('Cluster');
205+
/** @psalm-suppress MixedArrayAccess */
206+
if (isset($cluster['Cluster']['cluster_enabled']) && 1 === (int) $cluster['Cluster']['cluster_enabled']) {
207+
return true;
208+
}
209+
}
210+
211+
return false;
212+
}
213+
213214
/**
214215
* Normalizes cache TTL handling `null` value, strings and {@see DateInterval} objects.
215216
*
216217
* @param DateInterval|int|string|null $ttl The raw TTL.
217218
*
218219
* @return int|null TTL value as UNIX timestamp.
219220
*/
220-
private function normalizeTtl(null|int|string|DateInterval $ttl): ?int
221+
private function normalizeTtl(int|string|DateInterval|null $ttl): ?int
221222
{
222223
if ($ttl === null) {
223224
return null;

tests/RedisCacheTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public function testDeleteMultiple(): void
290290
$this->assertSameExceptObject($data, $this->cache->getMultiple($keys));
291291
$this->assertTrue($this->cache->deleteMultiple($keys));
292292

293-
$emptyData = array_map(static fn () => null, $data);
293+
$emptyData = array_map(static fn() => null, $data);
294294

295295
$this->assertSameExceptObject($emptyData, $this->cache->getMultiple($keys));
296296
}
@@ -359,7 +359,7 @@ public function iterableProvider(): array
359359
],
360360
'IteratorAggregate' => [
361361
['a' => 1, 'b' => 2,],
362-
new class () implements IteratorAggregate {
362+
new class implements IteratorAggregate {
363363
public function getIterator(): ArrayIterator
364364
{
365365
return new ArrayIterator(['a' => 1, 'b' => 2,]);

0 commit comments

Comments
 (0)