ColumnConsistency   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnCount() 0 3 1
A __invoke() 0 10 2
A __construct() 0 7 2
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.1.3
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
/**
18
 * A class to validate column consistency when inserting records into a CSV document
19
 *
20
 * @package League.csv
21
 * @since   7.0.0
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 */
24
class ColumnConsistency
25
{
26
    /**
27
     * The number of column per record
28
     *
29
     * @var int
30
     */
31
    protected $columns_count;
32
33
    /**
34
     * New Instance
35
     *
36
     *
37
     * @param  int                 $columns_count
38
     * @throws OutOfRangeException if the column count is lesser than -1
39
     */
40 6
    public function __construct(int $columns_count = -1)
41
    {
42 6
        if ($columns_count < -1) {
43 2
            throw new Exception(sprintf('%s() expects the column count to be greater or equal to -1 %s given', __METHOD__, $columns_count));
44
        }
45
46 4
        $this->columns_count = $columns_count;
47 4
    }
48
49
    /**
50
     * Returns the column count
51
     *
52
     * @return int
53
     */
54 2
    public function getColumnCount(): int
55
    {
56 2
        return $this->columns_count;
57
    }
58
59
    /**
60
     * Tell whether the submitted record is valid
61
     *
62
     * @param array $record
63
     *
64
     * @return bool
65
     */
66 4
    public function __invoke(array $record): bool
67
    {
68 4
        $count = count($record);
69 4
        if (-1 === $this->columns_count) {
70 2
            $this->columns_count = $count;
71
72 2
            return true;
73
        }
74
75 4
        return $count === $this->columns_count;
76
    }
77
}
78