Commit 4880bdf4 authored by Anton Smirnov's avatar Anton Smirnov
Browse files

Merge branch '1.x'

# Conflicts:
#	.gitlab-ci.yml
#	composer.json
#	src/CallbackClock.php
parents fceed6d8 309462a9
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -52,4 +52,7 @@ test:
    matrix:
    - PHP_VERSION:
      - '8.0' # lowest version
      - '8.1'
      - '8.2'
      - '8.3'
      - '8' # latest 8
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@
        "psy/psysh": "*",
        "sandfox.dev/code-standard": "^1.2024.07.20",
        "squizlabs/php_codesniffer": "*",
        "vimeo/psalm": "^5"
        "vimeo/psalm": "^5 | ^6"
    },
    "provide": {
        "psr/clock-implementation": "1.0"
+9 −7
Original line number Diff line number Diff line
@@ -6,12 +6,14 @@ namespace Arokettu\Clock;

use Closure;
use DateTimeImmutable;
use Generator;
use Psr\Clock\ClockInterface;
use ReflectionFunction;

final class CallbackClock implements ClockInterface
{
    private Closure $callback;
    private Generator $generator;

    public function __construct(Closure $callback)
    {
@@ -19,17 +21,17 @@ final class CallbackClock implements ClockInterface

        // if the closure creates a generator, use it
        if ($r->isGenerator()) {
            $this->callback = function () use (&$g, $callback): DateTimeImmutable {
                if ($g === null) {
                    $g = $callback();
            $callback = function () use ($callback): DateTimeImmutable {
                if (!isset($this->generator)) {
                    $this->generator = $callback();
                } else {
                    $g->next();
                    $this->generator->next();
                }
                return $g->current();
                return $this->generator->current();
            };
        } else {
            $this->callback = $callback;
        }

        $this->callback = $callback;
    }

    public function now(): DateTimeImmutable