ColumnConsistency::getColumnCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Csv (https://csv.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Csv;
15
16
use function count;
17
use function sprintf;
18
19
/**
20
 * Validates column consistency when inserting records into a CSV document.
21
 */
22
class ColumnConsistency
23
{
24
    /**
25
     * The number of column per record.
26
     *
27
     * @var int
28
     */
29
    protected $columns_count;
30
31
    /**
32
     * New Instance.
33
     *
34
     * @throws Exception if the column count is lesser than -1
35
     */
36 9
    public function __construct(int $columns_count = -1)
37
    {
38 9
        if ($columns_count < -1) {
39 3
            throw new Exception(sprintf('%s() expects the column count to be greater or equal to -1 %s given', __METHOD__, $columns_count));
40
        }
41
42 6
        $this->columns_count = $columns_count;
43 6
    }
44
45
    /**
46
     * Returns the column count.
47
     */
48 3
    public function getColumnCount(): int
49
    {
50 3
        return $this->columns_count;
51
    }
52
53
    /**
54
     * Tell whether the submitted record is valid.
55
     */
56 6
    public function __invoke(array $record): bool
57
    {
58 6
        $count = count($record);
59 6
        if (-1 === $this->columns_count) {
60 3
            $this->columns_count = $count;
61
62 3
            return true;
63
        }
64
65 6
        return $count === $this->columns_count;
66
    }
67
}
68