Completed
Push — master ( a5acb0...57b2d8 )
by Raffael
22:45 queued 18:41
created

Map::map()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 18
cp 0
rs 7.3166
c 0
b 0
f 0
cc 11
nc 5
nop 4
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Workflow;
13
14
use MongoDB\BSON\UTCDateTimeInterface;
15
use Tubee\AttributeMap\AttributeMapInterface;
16
use Tubee\Helper;
17
18
class Map
19
{
20
    /**
21
     * Map.
22
     */
23
    public static function map(AttributeMapInterface $map, array $object, array $mongodb_object, UTCDateTimeInterface $ts): Iterable
0 ignored issues
show
Unused Code introduced by
The parameter $ts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        $object = Helper::associativeArrayToPath($object);
26
        $mongodb_object = Helper::associativeArrayToPath($mongodb_object);
27
28
        foreach ($map->getMap() as $name => $value) {
29
            $name = $value['name'];
30
31
            $exists = isset($mongodb_object[$name]);
32
            if ($value['ensure'] === WorkflowInterface::ENSURE_EXISTS && $exists === true) {
33
                continue;
34
            }
35
            if (($value['ensure'] === WorkflowInterface::ENSURE_LAST || $value['ensure'] === WorkflowInterface::ENSURE_EXISTS) && isset($object[$name])) {
36
                $mongodb_object[$name] = $object[$name];
37
            } elseif ($value['ensure'] === WorkflowInterface::ENSURE_ABSENT && isset($mongodb_object[$name]) || !isset($object[$name]) && isset($mongodb_object[$name])) {
38
                unset($mongodb_object[$name]);
39
            }
40
        }
41
42
        return Helper::pathArrayToAssociative($mongodb_object);
43
    }
44
}
45