Skip to content

Commit 73aed51

Browse files
authored
Replace StyleCI to PHP CS Fixer (#30)
1 parent 57b68c5 commit 73aed51

9 files changed

Lines changed: 110 additions & 167 deletions

File tree

‎.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:
5+
paths:
6+
- 'migrations/**'
7+
- 'src/**'
8+
- 'tests/**'
9+
- 'rector.php'
10+
- '.php-cs-fixer.dist.php'
11+
- 'composer.json'
12+
- '.github/workflows/rector-cs.yml'
13+
14+
permissions:
15+
contents: read
16+
17+
concurrency:
18+
group: ${{ github.workflow }}-${{ github.ref }}
19+
cancel-in-progress: true
20+
21+
jobs:
22+
rector-cs:
23+
permissions:
24+
contents: write # Required to commit formatting fixes back to the PR
25+
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
26+
with:
27+
php: '8.1'

‎.github/workflows/rector.yml‎

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

‎.php-cs-fixer.dist.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Finder;
6+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
7+
use Yiisoft\CodeStyle\ConfigBuilder;
8+
9+
$finder = (new Finder())->in([
10+
__DIR__ . '/migrations',
11+
__DIR__ . '/src',
12+
__DIR__ . '/tests',
13+
]);
14+
15+
return ConfigBuilder::build()
16+
->setCacheFile(__DIR__ . '/runtime/.php-cs-fixer.cache')
17+
->setRiskyAllowed(true)
18+
->setParallelConfig(ParallelConfigFactory::detect())
19+
->setRules([
20+
'@Yiisoft/Core' => true,
21+
'@Yiisoft/Core:risky' => true,
22+
])
23+
->setFinder($finder);

‎.styleci.yml‎

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

‎composer.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
"yiisoft/queue": "dev-master"
3939
},
4040
"require-dev": {
41+
"friendsofphp/php-cs-fixer": "^3.95",
4142
"maglnet/composer-require-checker": "^4.7",
4243
"phpunit/phpunit": "^10.5",
4344
"rector/rector": "^2.0.3",
4445
"roave/infection-static-analysis-plugin": "^1.34",
4546
"spatie/phpunit-watcher": "^1.23",
4647
"vimeo/psalm": "^5.20",
4748
"yiisoft/cache": "^3.2",
49+
"yiisoft/code-style": "^1.1",
4850
"yiisoft/db-sqlite": "^2.0"
4951
},
5052
"suggest": {
@@ -78,6 +80,8 @@
7880
},
7981
"scripts": {
8082
"test": "phpunit --testdox",
81-
"test-watch": "phpunit-watcher watch"
83+
"test-watch": "phpunit-watcher watch",
84+
"rector": "rector",
85+
"cs-fix": "php-cs-fixer fix"
8286
}
8387
}

‎rector.php‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,16 @@
22

33
declare(strict_types=1);
44

5-
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
65
use Rector\Config\RectorConfig;
7-
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
8-
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
9-
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
10-
use Rector\Set\ValueObject\LevelSetList;
6+
use Yiisoft\CodeStyle\Rector\SetList;
117

12-
return static function (RectorConfig $rectorConfig): void {
13-
$rectorConfig->paths([
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/migrations',
1411
__DIR__ . '/src',
1512
__DIR__ . '/tests',
13+
])
14+
->withPhpSets(php81: true)
15+
->withSets([
16+
SetList::YII_CORE,
1617
]);
17-
18-
// register a single rule
19-
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
20-
21-
// define sets of rules
22-
$rectorConfig->sets([
23-
LevelSetList::UP_TO_PHP_81,
24-
]);
25-
26-
$rectorConfig->skip([
27-
ClosureToArrowFunctionRector::class,
28-
ReadOnlyPropertyRector::class,
29-
NullToStrictStringFuncCallArgRector::class,
30-
]);
31-
};

‎runtime/.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

‎src/Adapter.php‎

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
use Yiisoft\Mutex\MutexFactoryInterface;
2020
use Yiisoft\Mutex\MutexInterface;
2121
use Yiisoft\Queue\Provider\QueueProviderInterface;
22+
use Exception;
23+
24+
use function is_array;
25+
use function is_resource;
26+
use function is_string;
27+
28+
use const SORT_ASC;
2229

2330
final class Adapter implements AdapterInterface
2431
{
@@ -39,6 +46,11 @@ final class Adapter implements AdapterInterface
3946
*/
4047
public $deleteReleased = true;
4148

49+
/**
50+
* @var int reserve time
51+
*/
52+
private $reserveTime = 0;
53+
4254
public function __construct(
4355
private ConnectionInterface $db,
4456
private MessageSerializerInterface $serializer,
@@ -123,17 +135,42 @@ public function withChannel(BackedEnum|string $channel): self
123135
return $new;
124136
}
125137

138+
/**
139+
* Listens queue and runs each job.
140+
*
141+
* @param callable(MessageInterface): bool $handlerCallback The handler which will handle messages. Returns false if it cannot continue handling messages
142+
* @param bool $repeat whether to continue listening when queue is empty.
143+
* @param non-negative-int $timeout number of seconds to sleep before next iteration.
144+
*/
145+
public function run(callable $handlerCallback, bool $repeat, int $timeout = 0): void
146+
{
147+
while ($this->loop->canContinue()) {
148+
if ($payload = $this->reserve()) {
149+
if ($handlerCallback($this->serializer->unserialize($payload['job']))) {
150+
$this->release($payload);
151+
}
152+
continue;
153+
}
154+
if (!$repeat) {
155+
break;
156+
}
157+
if ($timeout > 0) {
158+
sleep($timeout);
159+
}
160+
}
161+
}
162+
126163
/**
127164
* Takes one message from waiting list and reserves it for handling.
128165
*
129-
* @throws \Exception in case it hasn't waited the lock
166+
* @throws Exception in case it hasn't waited the lock
130167
* @return array|null payload
131168
*/
132-
protected function reserve(): array|null
169+
protected function reserve(): ?array
133170
{
134171
// TWK TODO what is useMaster in Yii3 return $this->db->useMaster(function () {
135172
if (!$this->mutex->acquire($this->mutexTimeout)) {
136-
throw new \Exception('Has not waited the lock.');
173+
throw new Exception('Has not waited the lock.');
137174
}
138175

139176
try {
@@ -182,13 +219,13 @@ protected function release($payload): void
182219
if ($this->deleteReleased) {
183220
$this->db->createCommand()->delete(
184221
$this->tableName,
185-
['id' => $payload['id']]
222+
['id' => $payload['id']],
186223
)->execute();
187224
} else {
188225
$this->db->createCommand()->update(
189226
$this->tableName,
190227
['done_at' => time()],
191-
['id' => $payload['id']]
228+
['id' => $payload['id']],
192229
)->execute();
193230
}
194231
}
@@ -205,38 +242,8 @@ private function moveExpired(): void
205242
['reserved_at' => null],
206243
'[[reserved_at]] < :time - [[ttr]] and [[reserved_at]] is not null and [[done_at]] is null',
207244
null,
208-
[':time' => $this->reserveTime]
245+
[':time' => $this->reserveTime],
209246
)->execute();
210247
}
211248
}
212-
213-
/**
214-
* @var int reserve time
215-
*/
216-
private $reserveTime = 0;
217-
218-
/**
219-
* Listens queue and runs each job.
220-
*
221-
* @param callable(MessageInterface): bool $handlerCallback The handler which will handle messages. Returns false if it cannot continue handling messages
222-
* @param bool $repeat whether to continue listening when queue is empty.
223-
* @param non-negative-int $timeout number of seconds to sleep before next iteration.
224-
*/
225-
public function run(callable $handlerCallback, bool $repeat, int $timeout = 0): void
226-
{
227-
while ($this->loop->canContinue()) {
228-
if ($payload = $this->reserve()) {
229-
if ($handlerCallback($this->serializer->unserialize($payload['job']))) {
230-
$this->release($payload);
231-
}
232-
continue;
233-
}
234-
if (!$repeat) {
235-
break;
236-
}
237-
if ($timeout > 0) {
238-
sleep($timeout);
239-
}
240-
}
241-
}
242249
}

‎tests/AdapterTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
1919
use Yiisoft\Queue\Provider\QueueProviderInterface;
2020

21+
use function in_array;
22+
2123
final class AdapterTest extends TestCase
2224
{
2325
public function testPushUsesMessageMetaAndReturnsIdEnvelope(): void

0 commit comments

Comments
 (0)