ValidatorTrait   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterControl() 0 8 2
A filterNullableInteger() 0 14 4
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use League\Csv\Exception\LengthException;
18
use League\Csv\Exception\OutOfRangeException;
19
use TypeError;
20
21
/**
22
 * A trait to validate input variables
23
 *
24
 * @package  League.csv
25
 * @since    9.0.0
26
 * @author   Ignace Nyamagana Butera <[email protected]>
27
 * @internal Use to validate incoming data
28
 */
29
trait ValidatorTrait
30
{
31
    /**
32
     * Filter Csv control character
33
     *
34
     * @param string $char   Csv control character
35
     * @param string $type   Csv control character type
36
     * @param string $caller public API method calling the method
37
     *
38
     * @throws LengthException If the Csv control character is not one character only.
39
     *
40
     * @return string
41
     */
42 12
    protected function filterControl(string $char, string $type, string $caller): string
43
    {
44 12
        if (1 == strlen($char)) {
45 12
            return $char;
46
        }
47
48 2
        throw new LengthException(sprintf('%s() expects %s to be a single character %s given', $caller, $type, $char));
49
    }
50
51
    /**
52
     * Filter Nullable Integer with mininal range check
53
     *
54
     * @see https://wiki.php.net/rfc/nullable_types
55
     *
56
     * @param int|null $value
57
     * @param int      $min_range
58
     * @param string   $error_message
59
     *
60
     * @throws TypError            if value is not a integer or null
61
     * @throws OutOfRangeException if value is not in a valid int range
62
     */
63 18
    protected function filterNullableInteger($value, int $min_range, string $error_message)
64
    {
65 18
        if (null === $value) {
66 10
            return;
67
        }
68
69 10
        if (!is_int($value)) {
70 4
            throw new TypeError(sprintf('Expected argument to be null or a integer %s given', gettype($value)));
71
        }
72
73 6
        if ($value < $min_range) {
74 4
            throw new OutOfRangeException($error_message);
75
        }
76 4
    }
77
}
78