Skip to content

Commit

Permalink
template return callable types
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 2, 2024
1 parent fbefa48 commit afa6143
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public function notify(): void
* });
* </code>
*
* @param callable $code The synchronized execution block.
* @template T
* @param callable(): T $code The synchronized execution block.
* @throws \Exception The execution block threw an exception.
* @throws TimeoutException The timeout was reached.
* @return mixed The return value of the execution block.
* @return T The return value of the execution block.
*
*/
public function synchronized(callable $code)
Expand Down
7 changes: 4 additions & 3 deletions src/mutex/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ abstract class Mutex
* The code block may throw an exception. In this case the lock will be
* released as well.
*
* @param callable $code The synchronized execution callback.
* @template T
* @param callable(): T $code The synchronized execution callback.
* @throws \Exception The execution callback threw an exception.
* @throws \malkusch\lock\exception\LockAcquireException The mutex could not
* be acquired, no further side effects.
* @throws \malkusch\lock\exception\LockReleaseException The mutex could not
* be released, the code was already executed.
* @throws \malkusch\lock\exception\ExecutionOutsideLockException Some code
* has been executed outside of the lock.
* @return mixed The return value of the execution callback.
* @return T The return value of the execution callback.
*/
abstract public function synchronized(callable $code);

Expand All @@ -48,7 +49,7 @@ abstract public function synchronized(callable $code);
* });
* </code>
*
* @param callable $check Callback that decides if the lock should be
* @param callable(): bool $check Callback that decides if the lock should be
* acquired and if the synchronized callback should be executed after
* acquiring the lock.
* @return \malkusch\lock\util\DoubleCheckedLocking The double-checked
Expand Down
5 changes: 3 additions & 2 deletions src/mutex/TransactionalMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ private static function checkAutocommit(\PDO $pdo): void
* If the code throws any other exception, the transaction is rolled back
* and won't be replayed.
*
* @param callable $code The synchronized execution block.
* @template T
* @param callable(): T $code The synchronized execution block.
* @throws \Exception The execution block threw an exception.
* @throws LockAcquireException The transaction was not commited.
* @return mixed The return value of the execution block.
* @return T The return value of the execution block.
* @SuppressWarnings(PHPMD)
*/
public function synchronized(callable $code)
Expand Down
9 changes: 5 additions & 4 deletions src/util/DoubleCheckedLocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DoubleCheckedLocking
private $mutex;

/**
* @var callable The check.
* @var callable(): bool The check.
*/
private $check;

Expand All @@ -29,7 +29,7 @@ class DoubleCheckedLocking
*
* @param \malkusch\lock\mutex\Mutex $mutex Provides methods for exclusive
* code execution.
* @param callable $check Callback that decides if the lock should be
* @param callable(): bool $check Callback that decides if the lock should be
* acquired and if the critical code callback should be executed after
* acquiring the lock.
*/
Expand All @@ -48,7 +48,8 @@ public function __construct(Mutex $mutex, callable $check)
* critical code callback to indicate that processing did not occure or has
* failed. It is up to the user to decide the last point.
*
* @param callable $code The critical code callback.
* @template T
* @param callable(): T $code The critical code callback.
* @throws \Exception The execution callback or the check threw an
* exception.
* @throws \malkusch\lock\exception\LockAcquireException The mutex could not
Expand All @@ -57,7 +58,7 @@ public function __construct(Mutex $mutex, callable $check)
* be released.
* @throws \malkusch\lock\exception\ExecutionOutsideLockException Some code
* has been executed outside of the lock.
* @return mixed Boolean false if check did not pass or mixed for what ever
* @return T|false Boolean false if check did not pass or mixed for what ever
* the critical code callback returns.
*/
public function then(callable $code)
Expand Down
5 changes: 3 additions & 2 deletions src/util/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ public function end(): void
*
* If the code throws an exception it will stop repeating the execution.
*
* @param callable $code The to be executed code callback.
* @template T
* @param callable(): T $code The to be executed code callback.
* @throws \Exception The execution callback threw an exception.
* @throws \malkusch\lock\exception\TimeoutException The timeout has been
* reached.
* @return mixed The return value of the executed code callback.
* @return T The return value of the executed code callback.
*
*/
public function execute(callable $code)
Expand Down
5 changes: 3 additions & 2 deletions src/util/PcntlTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public function __construct(int $timeout)
* method. It will interfer with your application and lead to unexpected
* behaviour.
*
* @param callable $code Executed code block
* @template T
* @param callable(): T $code Executed code block
* @throws \malkusch\lock\exception\DeadlineException Running the code hit
* the deadline.
* @throws \malkusch\lock\exception\LockAcquireException Installing the
* timeout failed.
* @return mixed Return value of the executed block
* @return T Return value of the executed block
*/
public function timeBoxed(callable $code)
{
Expand Down

0 comments on commit afa6143

Please sign in to comment.