AbstractFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyFilter() 0 13 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[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
namespace RoadBunch\Csv\Formatter;
13
14
use RoadBunch\Csv\Exception\FormatterResultException;
15
16
17
/**
18
 * Class Formatter
19
 *
20
 * @author  Dan McAdams
21
 * @package RoadBunch\Csv\Formatter
22
 */
23
abstract class AbstractFormatter implements FormatterInterface
24
{
25 9
    protected static function applyFilter(callable $filter, array $data): array
26
    {
27 9
        $formatted = [];
28 9
        foreach ($data as $element) {
29 9
            if (!is_string($element)) {
30 1
                throw new \InvalidArgumentException('All elements of the array must be strings');
31
            }
32 8
            if (!is_string($formattedElement = call_user_func($filter, $element))) {
33 1
                throw new FormatterResultException('Formatter must result in an array of strings');
34
            }
35 7
            $formatted[] = $formattedElement;
36
        }
37 7
        return $formatted;
38
    }
39
}
40