Completed
Pull Request — master (#290)
by Leny
07:15
created

ArrayToViewReferenceTransformer::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 13
loc 13
rs 9.4286
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Transformer;
4
5
use Symfony\Component\Form\DataTransformerInterface;
6
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
7
8
class ArrayToViewReferenceTransformer implements DataTransformerInterface
9
{
10
    public $className = 'Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference';
11
12
    public function __construct()
13
    {
14
        $refClass = new \ReflectionClass($this->className);
15
        $this->properties = $refClass->getProperties();
0 ignored issues
show
Bug introduced by
The property properties does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
    }
17
18
    /**
19
     * Array to ViewReference
20
     * {@inheritdoc}
21
     *
22
     * @param array $array
23
     *
24
     * @return ViewReference
25
     */
26 View Code Duplication
    public function transform($array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $className = $this->className;
29
        $viewReference = new $className();
30
        foreach ($array['viewReference'] as $array) {
31
            foreach ($array as $prop => $value) {
32
                $methodName = 'set'.ucfirst($prop);
33
                $viewReference->$methodName((string) $value);
34
            }
35
        }
36
37
        return $viewReference;
38
    }
39
40
    /**
41
     * View Reference to array
42
     * {@inheritdoc}
43
     *
44
     * @param ViewReference $viewReference
45
     *
46
     * @return array
47
     */
48
    public function reverseTransform($viewReference)
49
    {
50
        $array = [];
51
        foreach ($this->properties as $prop) {
52
            $methodName = 'get'.ucfirst($prop->getName());
53
            $array[$prop->getName()] = $viewReference->$methodName();
54
        }
55
56
        return $array;
57
    }
58
}
59