Completed
Push — master ( 675da0...765c9a )
by Changwan
02:43
created

LengthBetweenTester::test()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 6
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Validator\Testers;
3
4
use Wandu\Validator\Contracts\Tester;
5
6
class LengthBetweenTester implements Tester
7
{
8
    /** @var int */
9
    protected $min;
10
11
    /** @var int */
12
    protected $max;
13
    
14
    /**
15
     * @param int $min
16
     * @param int $max
17
     */
18 1
    public function __construct($min, $max)
19
    {
20 1
        $this->min = $min;
21 1
        $this->max = $max;
22 1
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function test($data, $origin = null, array $keys = []): bool
28
    {
29 1
        if (is_array($data) || $data instanceof \Countable) {
30 1
            $length = count($data);
31 1
        } elseif (is_string($data)) {
32 1
            $length = mb_strlen($data, 'utf-8');
33
        }
34 1
        return $length >= $this->min && $length <= $this->max;
0 ignored issues
show
Bug introduced by
The variable $length does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
35
    }
36
}
37