MergeIntoAssignable   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 125
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 13 2
A mergeKeyIntoAssignable() 0 18 3
A fromObject() 0 6 1
A from() 0 13 3
A fromMixed() 0 4 1
A __invoke() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2015-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   DataContainers/Editors
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
41
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link      http://code.ganbarodigital.com/php-data-containers
43
 */
44
45
namespace GanbaroDigital\DataContainers\Editors;
46
47
use Traversable;
48
use GanbaroDigital\DataContainers\Containers\DataBag;
49
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType;
50
use GanbaroDigital\DataContainers\Internal\Checks\ShouldOverwrite;
51
use GanbaroDigital\Reflection\Checks\IsAssignable;
52
use GanbaroDigital\Reflection\Checks\IsIndexable;
53
use GanbaroDigital\Reflection\Checks\IsTraversable;
54
use GanbaroDigital\Reflection\Requirements\RequireAssignable;
55
use GanbaroDigital\Reflection\Requirements\RequireTraversable;
56
use GanbaroDigital\Reflection\ValueBuilders\FirstMethodMatchingType;
57
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
58
59
class MergeIntoAssignable
60
{
61
    /**
62
     * merge their array into our object
63
     *
64
     * @param  object $ours
65
     *         the object that we want to merge into
66
     * @param  array|Traversable $theirs
67
     *         the array that we want to merge from
68
     * @return void
69
     */
70
    public static function fromArray($ours, $theirs)
71
    {
72
        // robustness!
73
        RequireAssignable::check($ours, E4xx_UnsupportedType::class);
74
        RequireTraversable::check($theirs, E4xx_UnsupportedType::class);
75
76
        // copy from them to us
77
        foreach ($theirs as $key => $value) {
78
            self::mergeKeyIntoAssignable($ours, $key, $value);
79
        }
80
81
        // all done
82
    }
83
84
    /**
85
     * merge a single key into our object
86
     *
87
     * @param  object $ours
88
     *         the object to merge into
89
     * @param  mixed $key
90
     *         the array key to merge into
91
     * @param  mixed $value
92
     *         the data to merge in
93
     * @return void
94
     */
95
    private static function mergeKeyIntoAssignable($ours, $key, $value)
96
    {
97
        // general case - overwrite because merging isn't possible
98
        if (ShouldOverwrite::intoObject($ours, $key, $value)) {
99
            $ours->$key = $value;
100
            return;
101
        }
102
103
        // special case - we are merging into an array
104
        if (IsIndexable::check($ours->$key)) {
105
            MergeIntoIndexable::from($ours->$key, $value);
106
            return;
107
        }
108
109
        // at this point, we are merging into an object, using recursion
110
        // for which I am going to hell
111
        MergeIntoAssignable::from($ours->$key, $value);
112
    }
113
114
    /**
115
     * merge their object into our object
116
     *
117
     * @param  object $ours
118
     *         the object that we want to merge into
119
     * @param  object $theirs
120
     *         the object that we want to merge from
121
     * @return void
122
     */
123
    public static function fromObject($ours, $theirs)
124
    {
125
        // robustness!
126
        RequireAssignable::check($theirs, E4xx_UnsupportedType::class);
127
        self::fromArray($ours, get_object_vars($theirs));
128
    }
129
130
    /**
131
     * merge their data into our object
132
     *
133
     * @param  object $ours
134
     *         the object that we want to merge into
135
     * @param  array|object $theirs
136
     *         the data that we want to merge from
137
     * @return void
138
     */
139
    public static function from($ours, $theirs)
140
    {
141
        if (IsAssignable::check($theirs)) {
142
            return self::fromObject($ours, $theirs);
0 ignored issues
show
Bug introduced by
It seems like $theirs defined by parameter $theirs on line 139 can also be of type array; however, GanbaroDigital\DataConta...ssignable::fromObject() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143
        }
144
145
        if (IsTraversable::check($theirs)) {
146
            return self::fromArray($ours, $theirs);
0 ignored issues
show
Bug introduced by
It seems like $theirs defined by parameter $theirs on line 139 can also be of type object; however, GanbaroDigital\DataConta...Assignable::fromArray() does only seem to accept array|object<Traversable>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
        }
148
149
        // cannot merge anything that reaches here!
150
        throw new E4xx_UnsupportedType(SimpleType::from($ours));
151
    }
152
153
    /**
154
     * merge their data into our object
155
     *
156
     * @deprecated since 2.2.0
157
     * @codeCoverageIgnore
158
     *
159
     * @param  object $ours
160
     *         the object that we want to merge into
161
     * @param  array|object $theirs
162
     *         the data that we want to merge from
163
     * @return void
164
     */
165
    public static function fromMixed($ours, $theirs)
166
    {
167
        return self::from($ours, $theirs);
168
    }
169
170
    /**
171
     * merge their data into our object
172
     *
173
     * @param  object $ours
174
     *         the object that we want to merge into
175
     * @param  array|object $theirs
176
     *         the data that we want to merge from
177
     * @return void
178
     */
179
    public function __invoke($ours, $theirs)
180
    {
181
        return self::from($ours, $theirs);
182
    }
183
}