Completed
Push — develop ( 62651e...ddc457 )
by
unknown
09:17
created

SnapshotGenerator::setSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Core\Service;
12
13
use Core\Entity\EntityInterface;
14
use Core\Entity\Hydrator\CloneHydrator;
15
use Core\Entity\Hydrator\EntityHydrator;
16
use Core\Entity\SnapshotInterface;
17
use Core\Entity\SnapshotMeta;
18
19
/**
20
 * Class SnapshotGenerator
21
 * @package Core\Service
22
 */
23
class SnapshotGenerator
24
{
25
26
    /**
27
     * @var
28
     */
29
    protected $hydrator;
30
31
    /**
32
     *
33
     *
34
     * @var array
35
     */
36
    protected $snapshotAttributes = [];
37
38
39
    /**
40
     * @param $hydrator
41
     * @return $this
42
     */
43
    public function setHydrator($hydrator)
44
    {
45
        if ($hydrator instanceof EntityHydrator) {
46
            $this->hydrator = $hydrator;
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * @return EntityHydrator
53
     */
54
    public function getHydrator()
55
    {
56
        if (!isset($this->hydrator)) {
57
            $this->hydrator = new CloneHydrator();
58
        }
59
        return $this->hydrator;
60
    }
61
62
    /**
63
     * @param array $snapshotAttributes
64
     *
65
     * @return self
66
     */
67
    public function setSnapshotAttributes($snapshotAttributes)
68
    {
69
        $this->snapshotAttributes = $snapshotAttributes;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function getSnapshotAttributes()
78
    {
79
        return $this->snapshotAttributes;
80
    }
81
82
83
84
    public function __invoke($source, $attributes = [], $target = null)
85
    {
86
        if (!is_array($attributes)) {
87
            $target = $attributes;
88
            $attributes = null;
89
        }
90
91
        if (!$target) {
92
            $target = get_class($source) . 'Snapshot';
93
            $target = new $target($source);
94
        }
95
96
        if ($target instanceOf SnapshotAttributesProviderInterface) {
0 ignored issues
show
Bug introduced by
The class Core\Service\SnapshotAttributesProviderInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
97
            $attributes = $target->getSnapshotAttributes();
98
        } else if (empty($attributes)) {
99
            $attributes = $this->getSnapshotAttributes();
100
        }
101
102
        $hydrator = $this->getHydrator();
103
        $data     = $hydrator->extract($source, $attributes);
0 ignored issues
show
Unused Code introduced by
The call to EntityHydrator::extract() has too many arguments starting with $attributes.

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...
104
        $snapshot = $hydrator->hydrate($target, $data);
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
106
        return $snapshot;
107
    }
108
}
109