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

DefaultValuesTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace TreeHouse\IoBundle\Item\Modifier\Item\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Modifier\Data\Transformer\EmptyValueToNullTransformer;
7
use TreeHouse\Feeder\Modifier\Item\Transformer\TransformerInterface;
8
9
/**
10
 * Sets default values when they are not present or empty in feed.
11
 */
12
class DefaultValuesTransformer implements TransformerInterface
13
{
14
    /**
15
     * @var bool indicates if values should be overwritten if they exist
16
     */
17
    protected $overwrite;
18
19
    /**
20
     * @var array
21
     */
22
    protected $defaultValues;
23
24
    /**
25
     * @param array $defaultValues
26
     * @param bool  $overwrite     indicates if values should be overwritten if they exist
27
     */
28 4
    public function __construct(array $defaultValues, $overwrite = false)
29
    {
30 4
        $this->defaultValues = $defaultValues;
31 4
        $this->overwrite = $overwrite;
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 4
    public function transform(ParameterBag $item)
38
    {
39 4
        $transformer = new EmptyValueToNullTransformer();
40
41
        // check for any default values that could be set
42 4
        foreach ($this->defaultValues as $key => $value) {
43
            // only set when it's not existing in the data or has no value
44
            if (!$item->has($key) || null === $transformer->transform($item->get($key), $key, $item) || $this->overwrite) {
0 ignored issues
show
Unused Code introduced by
The call to EmptyValueToNullTransformer::transform() has too many arguments starting with $key.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
                $item->set($key, $value);
46
            }
47
        }
48 4
    }
49
}
50