Completed
Pull Request — develop (#323)
by Mathias
15:36
created

HydratorStrategyProviderTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setHydratorStrategy() 0 6 1
A getHydratorStrategy() 0 13 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Form\Hydrator;
12
13
use Zend\Hydrator\Strategy\DefaultStrategy;
14
use Zend\Hydrator\Strategy\StrategyInterface;
15
16
/**
17
 * Implementation of the {@link HydratorStrategyProviderInterface}.
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
trait HydratorStrategyProviderTrait
23
{
24
25
    /**
26
     * The hydrator strategy to provide.
27
     *
28
     * @var StrategyInterface
29
     */
30
    private $hydratorStrategy;
31
32
    public function setHydratorStrategy(StrategyInterface $strategy)
33
    {
34
        $this->hydratorStrategy = $strategy;
35
36
        return $this;
37
    }
38
39
    public function getHydratorStrategy()
40
    {
41
        if (!$this->hydratorStrategy) {
42
            $strategy =
43
                method_exists($this, 'getDefaultHydratorStrategy')
44
                ? $this->getDefaultHydratorStrategy()
0 ignored issues
show
Bug introduced by
It seems like getDefaultHydratorStrategy() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
                : new DefaultStrategy();
46
47
            $this->setHydratorStrategy($strategy);
48
        }
49
50
        return $this->hydratorStrategy;
51
    }
52
}