MediaTypeDataTransformer::transform()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Media\Form\DataTransformer;
4
5
use Media\Entity\MediaInterface;
6
use Media\Provider\MediaProviderInterface;
7
use Symfony\Component\Form\DataTransformerInterface;
8
9
class MediaTypeDataTransformer implements DataTransformerInterface
10
{
11
12
    /**
13
     *
14
     * @var string
15
     */
16
    private $class;
17
    
18
    /**
19
     *
20
     * @var array|null
21
     */
22
    private $options;
23
    
24
    /**
25
     *
26
     * @var MediaProviderInterface
27
     */
28
    private $provider;
29
30
    /**
31
     * 
32
     * @param MediaProviderInterface $provider
33
     * @param string $class
34
     * @param array|null $options
35
     */
36
    public function __construct(MediaProviderInterface $provider, $class, $options)
37
    {
38
        $this->class = $class;
39
        $this->options = $options;
40
        $this->provider = $provider;
41
    }
42
43
    /**
44
     * 
45
     * @param MediaInterface|null $value
46
     * 
47
     * @return MediaInterface
48
     */
49
    public function reverseTransform($value)
50
    {
51
52
        if (is_null($value)) {
53
            return null;
54
        } else if ($value instanceof MediaInterface) {
55
            return $this->provider->save($value, $this->class, $this->options);
0 ignored issues
show
Bug introduced by
It seems like $this->options can also be of type null; however, Media\Provider\MediaProviderInterface::save() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
        }
57
        
58
        return $value;
59
    }
60
61
    /**
62
     * 
63
     * @param MediaInterface|null $value
64
     * 
65
     * @return MediaInterface|null
66
     */
67
    public function transform($value)
68
    {
69
        return $value;
70
    }
71
72
}
73