Completed
Push — master ( ff5baa...3970bf )
by Korotkov
07:54 queued 04:02
created

AbstractHandler::next()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @license   https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\ChainOfResponsibility;
11
12
/**
13
 * Class AbstractHandler
14
 * @package Behavioral\ChainOfResponsibility
15
 */
16
abstract class AbstractHandler
17
{
18
19
    /**
20
     * AbstractHandler constructor.
21
     * @param int   $count
22
     * @param array $chain
23
     */
24
    public function __invoke(int $count, array $chain)
25
    {
26
        array_shift($chain);
27
        $this->request();
28
        $this->next($count - 1, $chain);
29
    }
30
31
    /**
32
     * @param int   $count
33
     * @param array $chain
34
     */
35
    protected function next(int $count, array $chain): void
36
    {
37
        if (!($count and count($chain))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
38
            printf(PHP_EOL);
39
            return;
40
        }
41
42
        $nextChain = new $chain[0] ?? new $chain;
43
        $nextChain($count, $chain);
44
    }
45
46
    abstract public function request();
47
}
48