Completed
Push — master ( dbcf6b...d53ab6 )
by Peter
44:13 queued 37:37
created

MultiLineToSingleLineTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 25
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 22 4
1
<?php
2
3
namespace TreeHouse\IoBundle\Item\Modifier\Data\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Exception\TransformationFailedException;
7
use TreeHouse\Feeder\Modifier\Data\Transformer\TransformerInterface;
8
9
class MultiLineToSingleLineTransformer implements TransformerInterface
10
{
11 10
    public function transform($value, $key = null, ParameterBag $item = null)
12
    {
13 10
        if (is_null($value)) {
14
            return $value;
15
        }
16
17 10
        if (is_scalar($value)) {
18 10
            $value = (string) $value;
19
        }
20
21 10
        if (!is_string($value)) {
22
            throw new TransformationFailedException(
23
                sprintf('Expected a string to transform, got %s instead', json_encode($value))
24
            );
25
        }
26
27 10
        $value = preg_replace('/\n{1,}/', ' ', $value); // replace newlines with single newline
28 10
        $value = preg_replace('/ {2,}/', ' ', $value);  // replace double spaces with single space
29 10
        $value = trim($value);                          // trim leading and trailing spaces
30
31 10
        return $value;
32
    }
33
}
34