GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

InfiniteCaller   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 9
loc 99
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A call() 0 8 1
A info() 0 6 1
A delegateCall() 9 16 4
A setUpCaller() 0 27 3
A tearDownCaller() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\Recoil;
4
5
use Recoil\Kernel;
6
use Rx\ObservableInterface;
7
use Rx\Subject\Subject;
8
9
final class InfiniteCaller implements QueueCallerPoolInterface
10
{
11
    /** @var Kernel */
12
    private $kernel;
13
14
    /** @var State */
15
    private $state;
16
17
    /** @var QueueCallerInterface[] */
18
    private $callers = [];
19
20
    /** @var State[] */
21
    private $callerState = [];
22
23
    /** @var Subject */
24
    private $callerStream = [];
25
26
    /**
27
     * @param Kernel $kernel
28
     */
29 1
    public function __construct(Kernel $kernel)
30
    {
31 1
        $this->kernel = $kernel;
32
33 1
        $this->state = new State();
34 1
        $this->state->onNext(State::WAITING);
35 1
        $this->setUpCaller();
36 1
    }
37
38 1
    public function call(ObservableInterface $observable): State
39
    {
40
        $observable->subscribe(function (Call $call): void {
41 1
            $this->delegateCall($call);
42 1
        });
43
44 1
        return $this->state;
45
    }
46
47 1
    public function info(): iterable
48
    {
49 1
        yield 'callers' => \count($this->callers);
50 1
        yield 'busy'    => \count($this->callers);
51 1
        yield 'waiting' => 0; // Always zero because we remove waiting callers from the pool immediately
52 1
    }
53
54 1
    private function delegateCall(Call $call): void
55
    {
56 1 View Code Duplication
        foreach ($this->callerState as $qcHash => $state) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 1
            if ($state->getState() === State::BUSY && $state->getState() !== State::DONE) {
58 1
                continue;
59
            }
60
61 1
            $this->callerStream[$qcHash]->onNext($call);
62
63 1
            return;
64
        }
65
66 1
        $this->state->onNext(State::BUSY);
67 1
        $this->setUpCaller();
68 1
        $this->delegateCall($call);
69 1
    }
70
71 1
    private function setUpCaller(): void
72
    {
73 1
        $stream = new Subject();
74 1
        $caller = new FiniteCaller($this->kernel, 13);
75 1
        $qcHash = \spl_object_hash($caller);
76
77 1
        $this->callers[$qcHash] = $caller;
78 1
        $this->callerStream[$qcHash] = $stream;
79 1
        $this->callerState[$qcHash] = $caller->call($stream);
80
        $this->callerState[$qcHash]->filter(function (int $state) {
81 1
            return $state === State::WAITING;
82
        })->subscribe(function () use ($qcHash): void {
83
            $waitingCallers = \array_filter($this->callerState, function (State $state) {
84 1
                return $state->getState() === State::WAITING;
85 1
            });
86 1
            $waitingCallersCount = \count($waitingCallers);
87 1
            if ($waitingCallersCount === \count($this->callers)) {
88 1
                $this->state->onNext(State::WAITING);
89
            }
90
91 1
            \reset($waitingCallers);
92 1
            for ($i = 1; $i < $waitingCallersCount; $i++) {
93 1
                $this->tearDownCaller(\key($waitingCallers));
94 1
                \next($waitingCallers);
95
            }
96 1
        });
97 1
    }
98
99 1
    private function tearDownCaller(string $qcHash): void
100
    {
101
        unset(
102 1
            $this->callers[$qcHash],
103 1
            $this->callerStream[$qcHash],
104 1
            $this->callerState[$qcHash]
105
        );
106 1
    }
107
}
108