Issues (6)

src/Chips/WRouter.php (1 issue)

1
<?php
2
/**
3
 * Service router/endpoints changed
4
 * User: moyo
5
 * Date: 25/09/2017
6
 * Time: 10:28 AM
7
 */
8
9
namespace Carno\Consul\Chips;
10
11
use Carno\Consul\Types\Router;
12
use Carno\Consul\Types\Service;
13
use Carno\Net\Endpoint;
14
15
trait WRouter
16
{
17
    /**
18
     * @var Endpoint[]
19
     */
20
    private $prevEndpoints = [];
21
22
    /**
23
     * @param Service $service
24
     * @return Router[]
25
     */
26
    protected function routing(Service $service) : array
27
    {
28
        $commands = [];
29
30
        $newEndpoints = [];
31
32
        foreach ($service->getEndpoints() as $endpoint) {
33
            $epn = $endpoint->id();
34
35
            $newEndpoints[$epn] = $endpoint;
36
37
            if (isset($this->prevEndpoints[$epn])) {
38
                // exists
39
                unset($this->prevEndpoints[$epn]);
40
                continue;
41
            } else {
42
                // join
43
                $commands[] = new Router(Router::JOIN, $endpoint);
44
            }
45
        }
46
47
        if ($this->prevEndpoints) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->prevEndpoints of type Carno\Net\Endpoint[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
            foreach ($this->prevEndpoints as $endpoint) {
49
                // leave
50
                $commands[] = new Router(Router::LEAVE, $endpoint);
51
            }
52
        }
53
54
        $this->prevEndpoints = $newEndpoints;
55
56
        return $commands;
57
    }
58
}
59