Skip to content

Commit 707d492

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #33 from TheLevti/develop
Code clean up
2 parents a7fa877 + 63a35b6 commit 707d492

Some content is hidden

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

43 files changed

+911
-667
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/.gitignore export-ignore
55
/.travis.yml export-ignore
66
/phpunit.xml.dist export-ignore
7+
/php_cs.dist export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/vendor
33
/composer.lock
44
/phpunit.xml
5+
/.php_cs

.php_cs.dist

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->notPath('vendor')
5+
->in(__DIR__)
6+
->name('*.php')
7+
->ignoreDotFiles(true)
8+
->ignoreVCS(true);
9+
10+
return PhpCsFixer\Config::create()
11+
->setUsingCache(false)
12+
->setRiskyAllowed(true)
13+
->setRules([
14+
'@PSR2' => true,
15+
'psr4' => true,
16+
'binary_operator_spaces' => false,
17+
'array_syntax' => ['syntax' => 'short'],
18+
'linebreak_after_opening_tag' => true,
19+
'not_operator_with_successor_space' => true,
20+
'no_unused_imports' => true,
21+
'not_operator_with_successor_space' => false,
22+
'object_operator_without_whitespace' => true,
23+
'ordered_imports' => true,
24+
'phpdoc_order' => true,
25+
'blank_line_before_return' => true,
26+
'single_quote' => true,
27+
'blank_line_after_opening_tag' => true,
28+
'no_extra_blank_lines' => [
29+
'extra',
30+
'continue',
31+
'curly_brace_block',
32+
'parenthesis_brace_block',
33+
'return',
34+
'square_brace_block',
35+
'throw',
36+
'use',
37+
'use_trait',
38+
'switch',
39+
'case',
40+
'default',
41+
],
42+
'no_whitespace_in_blank_line' => true,
43+
'no_blank_lines_after_class_opening' => true,
44+
'return_type_declaration' => [
45+
'space_before' => 'none',
46+
],
47+
'concat_space' => [
48+
'spacing' => 'one',
49+
],
50+
'compact_nullable_typehint' => true,
51+
'ternary_operator_spaces' => true,
52+
'unary_operator_spaces' => true,
53+
'binary_operator_spaces' => [
54+
'default' => 'single_space',
55+
'operators' => [
56+
'|' => 'no_space',
57+
]
58+
]
59+
])
60+
->setFinder($finder);

README.md

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ php-lock/lock follows semantic versioning. Read more on [semver.org][1].
2222

2323
- PHP 7.1 or above
2424
- Optionally [nrk/predis][2] to use the Predis locks.
25-
- Optionally the [php-pcntl][3] extension to enable locking with `flock()` without
26-
busy waiting in CLI scripts.
27-
- Optionally `flock()`, `ext-redis`, `ext-pdo_mysql`, `ext-pdo_sqlite`, `ext-pdo_pgsql` or `ext-memcached` can be used as a backend for locks. See examples below.
28-
- If `ext-redis` is used for locking and is configured to use igbinary for serialization or
29-
lzf for compression, additionally `ext-igbinary` and/or `ext-lzf` have to be installed.
25+
- Optionally the [php-pcntl][3] extension to enable locking with `flock()`
26+
without busy waiting in CLI scripts.
27+
- Optionally `flock()`, `ext-redis`, `ext-pdo_mysql`, `ext-pdo_sqlite`,
28+
`ext-pdo_pgsql` or `ext-memcached` can be used as a backend for locks. See
29+
examples below.
30+
- If `ext-redis` is used for locking and is configured to use igbinary for
31+
serialization or lzf for compression, additionally `ext-igbinary` and/or
32+
`ext-lzf` have to be installed.
3033

3134
----
3235

@@ -64,7 +67,10 @@ return value `false` or `null` should be seen as a failed action.
6467
Example:
6568

6669
```php
67-
$newBalance = $mutex->synchronized(function () use ($bankAccount, $amount): int {
70+
$newBalance = $mutex->synchronized(function () use (
71+
$bankAccount,
72+
$amount
73+
): int {
6874
$balance = $bankAccount->getBalance();
6975
$balance -= $amount;
7076
if ($balance < 0) {
@@ -133,26 +139,26 @@ Example:
133139
```php
134140
try {
135141
// or $mutex->check(...)
136-
$mutex->synchronized(function () {
142+
$result = $mutex->synchronized(function () {
137143
if (someCondition()) {
138144
throw new \DomainException();
139145
}
140146

141147
return "result";
142148
});
143-
} catch (LockReleaseException $unlock_exception) {
144-
if ($unlock_exception->getCodeException() !== null) {
145-
$code_exception = $unlock_exception->getCodeException()
149+
} catch (LockReleaseException $unlockException) {
150+
if ($unlockException->getCodeException() !== null) {
151+
$codeException = $unlockException->getCodeException()
146152
// do something with the code exception
147153
} else {
148-
$code_result = $unlock_exception->getCodeResult();
154+
$code_result = $unlockException->getCodeResult();
149155
// do something with the code result
150156
}
151-
157+
152158
// deal with LockReleaseException or propagate it
153-
throw $unlock_exception;
159+
throw $unlockException;
154160
}
155-
```
161+
```
156162

157163
### Implementations
158164

@@ -189,15 +195,15 @@ $mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
189195
$balance -= $amount;
190196
if (!$memcached->cas($casToken, "balance", $balance)) {
191197
return;
192-
193198
}
194199
$mutex->notify();
195200
});
196201
```
197202

198203
#### FlockMutex
199204

200-
The **FlockMutex** is a lock implementation based on [`flock()`](http://php.net/manual/en/function.flock.php).
205+
The **FlockMutex** is a lock implementation based on
206+
[`flock()`](http://php.net/manual/en/function.flock.php).
201207

202208
Example:
203209
```php
@@ -207,19 +213,18 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
207213
$balance -= $amount;
208214
if ($balance < 0) {
209215
throw new \DomainException("You have no credit.");
210-
211216
}
212217
$bankAccount->setBalance($balance);
213218
});
214219
```
215220

216221
Timeouts are supported as an optional second argument. This uses the `ext-pcntl`
217-
extension if possible or busy waiting if not.
222+
extension if possible or busy waiting if not.
218223

219224
#### MemcachedMutex
220225

221-
The **MemcachedMutex**
222-
is a spinlock implementation which uses the [`Memcached` API](http://php.net/manual/en/book.memcached.php).
226+
The **MemcachedMutex** is a spinlock implementation which uses the
227+
[`Memcached` API](http://php.net/manual/en/book.memcached.php).
223228

224229
Example:
225230
```php
@@ -232,21 +237,21 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
232237
$balance -= $amount;
233238
if ($balance < 0) {
234239
throw new \DomainException("You have no credit.");
235-
236240
}
237241
$bankAccount->setBalance($balance);
238242
});
239243
```
240244

241245
#### PHPRedisMutex
242246

243-
The **PHPRedisMutex** is the distributed lock implementation of [RedLock](http://redis.io/topics/distlock)
244-
which uses the [`phpredis` extension](https://github.com/phpredis/phpredis).
247+
The **PHPRedisMutex** is the distributed lock implementation of
248+
[RedLock](http://redis.io/topics/distlock) which uses the
249+
[`phpredis` extension](https://github.com/phpredis/phpredis).
245250

246251
This implementation requires at least `phpredis-2.2.4`.
247252

248-
If used with a cluster of Redis servers, acquiring and releasing locks will continue to function as
249-
long as a majority of the servers still works.
253+
If used with a cluster of Redis servers, acquiring and releasing locks will
254+
continue to function as long as a majority of the servers still works.
250255

251256
Example:
252257
```php
@@ -259,16 +264,16 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
259264
$balance -= $amount;
260265
if ($balance < 0) {
261266
throw new \DomainException("You have no credit.");
262-
263267
}
264268
$bankAccount->setBalance($balance);
265269
});
266270
```
267271

268272
#### PredisMutex
269273

270-
The **PredisMutex** is the distributed lock implementation of [RedLock](http://redis.io/topics/distlock)
271-
which uses the [`Predis` API](https://github.com/nrk/predis).
274+
The **PredisMutex** is the distributed lock implementation of
275+
[RedLock](http://redis.io/topics/distlock) which uses the
276+
[`Predis` API](https://github.com/nrk/predis).
272277

273278
Example:
274279
```php
@@ -280,27 +285,25 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
280285
$balance -= $amount;
281286
if ($balance < 0) {
282287
throw new \DomainException("You have no credit.");
283-
284288
}
285289
$bankAccount->setBalance($balance);
286290
});
287291
```
288292

289293
#### SemaphoreMutex
290294

291-
The **SemaphoreMutex**
292-
is a lock implementation based on [Semaphore](http://php.net/manual/en/ref.sem.php).
295+
The **SemaphoreMutex** is a lock implementation based on
296+
[Semaphore](http://php.net/manual/en/ref.sem.php).
293297

294298
Example:
295299
```php
296300
$semaphore = sem_get(ftok(__FILE__, "a"));
297-
$mutex = new SemaphoreMutex($semaphore);
301+
$mutex = new SemaphoreMutex($semaphore);
298302
$mutex->synchronized(function () use ($bankAccount, $amount) {
299303
$balance = $bankAccount->getBalance();
300304
$balance -= $amount;
301305
if ($balance < 0) {
302306
throw new \DomainException("You have no credit.");
303-
304307
}
305308
$bankAccount->setBalance($balance);
306309
});
@@ -321,14 +324,15 @@ Example:
321324
```php
322325
$mutex = new TransactionalMutex($pdo);
323326
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
324-
$select = $pdo->prepare("SELECT balance FROM account WHERE id = ? FOR UPDATE");
327+
$select = $pdo->prepare(
328+
"SELECT balance FROM account WHERE id = ? FOR UPDATE"
329+
);
325330
$select->execute([$accountId]);
326331
$balance = $select->fetchColumn();
327332

328333
$balance -= $amount;
329334
if ($balance < 0) {
330335
throw new \DomainException("You have no credit.");
331-
332336
}
333337
$pdo->prepare("UPDATE account SET balance = ? WHERE id = ?")
334338
->execute([$balance, $accountId]);
@@ -337,15 +341,16 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
337341

338342
#### MySQLMutex
339343

340-
The **MySQLMutex** uses MySQL's
344+
The **MySQLMutex** uses MySQL's
341345
[`GET_LOCK`](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock)
342346
function.
343347

344-
It supports time outs. If the connection to the database server is lost or interrupted, the lock is
345-
automatically released.
348+
It supports time outs. If the connection to the database server is lost or
349+
interrupted, the lock is automatically released.
346350

347-
Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will silently release already
348-
held locks. You should probably refrain from using this mutex on MySQL versions < 5.7.5.
351+
Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will
352+
silently release already held locks. You should probably refrain from using this
353+
mutex on MySQL versions < 5.7.5.
349354

350355
```php
351356
$pdo = new PDO("mysql:host=localhost;dbname=test", "username");
@@ -356,22 +361,22 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
356361
$balance -= $amount;
357362
if ($balance < 0) {
358363
throw new \DomainException("You have no credit.");
359-
360364
}
361365
$bankAccount->setBalance($balance);
362366
});
363367
```
364368

365369
#### PgAdvisoryLockMutex
366370

367-
The **PgAdvisoryLockMutex** uses PostgreSQL's
371+
The **PgAdvisoryLockMutex** uses PostgreSQL's
368372
[advisory locking](https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
369373
functions.
370374

371-
Named locks are offered. PostgreSQL locking functions require integers but the conversion is handled automatically.
375+
Named locks are offered. PostgreSQL locking functions require integers but the
376+
conversion is handled automatically.
372377

373-
No time outs are supported. If the connection to the database server is lost or interrupted, the lock is
374-
automatically released.
378+
No time outs are supported. If the connection to the database server is lost or
379+
interrupted, the lock is automatically released.
375380

376381
```php
377382
$pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
@@ -382,7 +387,6 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
382387
$balance -= $amount;
383388
if ($balance < 0) {
384389
throw new \DomainException("You have no credit.");
385-
386390
}
387391
$bankAccount->setBalance($balance);
388392
});
@@ -410,4 +414,4 @@ If you like this project and feel generous donate a few Bitcoins here:
410414
[10]: https://en.wikipedia.org/wiki/Compare-and-swap
411415
[11]: https://github.com/php-lock/lock/blob/master/classes/mutex/CASMutex.php#L44
412416
[12]: https://github.com/php-lock/lock/blob/master/classes/mutex/LockMutex.php
413-
[13]: https://github.com/php-lock/lock/blob/master/classes/exception/LockReleaseException.php
417+
[13]: https://github.com/php-lock/lock/blob/master/classes/exception/LockReleaseException.php
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace malkusch\lock\exception;
46

5-
class DeadlineException extends \Exception
6-
{
7+
use RuntimeException;
78

9+
/**
10+
* Deadline exception.
11+
*
12+
* @author Willem Stuursma-Ruwen <[email protected]>
13+
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations
14+
* @license WTFPL
15+
*/
16+
class DeadlineException extends RuntimeException implements PhpLockException
17+
{
818
}

0 commit comments

Comments
 (0)