LimitQualifier   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTarget() 0 3 1
A __construct() 0 6 1
A setTarget() 0 5 2
A setLimit() 0 5 1
A qualify() 0 7 3
1
<?php
2
3
/*
4
 * This file is part of YaEtl
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Qualifiers;
11
12
use fab2s\NodalFlow\Flows\InterrupterInterface;
13
use fab2s\NodalFlow\Interrupter;
14
15
/**
16
 * Class LimitQualifier
17
 */
18
class LimitQualifier extends QualifierAbstract
19
{
20
    /**
21
     * @var int|null
22
     */
23
    protected $limit;
24
25
    /**
26
     * @var int
27
     */
28
    protected $count = 0;
29
30
    /**
31
     * @var string
32
     */
33
    protected $target;
34
35
    public function __construct(?int $limit = null, string $target = InterrupterInterface::TARGET_SELF)
36
    {
37
        parent::__construct();
38
39
        $this->setLimit($limit)
40
            ->setTarget($target);
41
    }
42
43
    /**
44
     * @param int|null $limit
45
     *
46
     * @return LimitQualifier
47
     */
48
    public function setLimit(?int $limit): LimitQualifier
49
    {
50
        $this->limit = $limit;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getTarget(): string
59
    {
60
        return $this->target;
61
    }
62
63
    /**
64
     * @param string $target
65
     *
66
     * @return LimitQualifier
67
     */
68
    public function setTarget(string $target): LimitQualifier
69
    {
70
        $this->target = $target === InterrupterInterface::TARGET_TOP ? InterrupterInterface::TARGET_TOP : InterrupterInterface::TARGET_SELF;
71
72
        return $this;
73
    }
74
75
    public function qualify($param)
76
    {
77
        if ($this->limit && ++$this->count >= $this->limit + 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
78
            return new Interrupter($this->getTarget(), null, InterrupterInterface::TYPE_BREAK);
79
        }
80
81
        return true;
82
    }
83
}
84