Completed
Push — master ( 4e8bc9...f46df8 )
by Tobias
13:01 queued 05:03
created

ReplaceOperation::processDomain()   D

Complexity

Conditions 9
Paths 120

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 29
cts 29
cp 1
rs 4.6666
c 0
b 0
f 0
cc 9
eloc 22
nc 120
nop 1
crap 9
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Catalogue\Operation;
13
14
use Symfony\Component\Translation\Catalogue\AbstractOperation;
15
use Symfony\Component\Translation\MetadataAwareInterface;
16
17
/**
18
 * This will merge and replace all values in $target with values from $source.
19
 * It is the equivalent of running array_merge($target, $source). When in conflict,
20
 * always take values from $source.
21
 *
22
 * This operation is metadata aware. It will do the same recursive merge on metadata.
23
 *
24
 * all = source ∪ target = {x: x ∈ source ∨ x ∈ target}
25
 * new = all ∖ target = {x: x ∈ source ∧ x ∉ target}
26
 * obsolete = target ∖ all = {x: x ∈ target ∧ x ∉ source}
27
 *
28
 * @author Tobias Nyholm <[email protected]>
29
 */
30
final class ReplaceOperation extends AbstractOperation
31
{
32 6
    protected function processDomain($domain)
33
    {
34 6
        $this->messages[$domain] = [
35 6
            'all' => [],
36 6
            'new' => [],
37 6
            'obsolete' => [],
38
        ];
39 6
        $sourceMessages = $this->source->all($domain);
40
41 6
        foreach ($this->target->all($domain) as $id => $message) {
42 5
            $this->messages[$domain]['all'][$id] = $message;
43
44
            // If $id is NOT defined in source.
45 5
            if (!array_key_exists($id, $sourceMessages)) {
46 5
                $this->messages[$domain]['obsolete'][$id] = $message;
47 5
            }
48 6
        }
49
50 6
        foreach ($sourceMessages as $id => $message) {
51 5
            if (!empty($message)) {
52 4
                $this->messages[$domain]['all'][$id] = $message;
53 4
            }
54
55 5
            if (!$this->target->has($id, $domain)) {
56 5
                $this->messages[$domain]['new'][$id] = $message;
57
58
                // Make sure to add it to the source if even if empty($message)
59 5
                $this->messages[$domain]['all'][$id] = $message;
60 5
            }
61 6
        }
62
63 6
        $this->result->add($this->messages[$domain]['all'], $domain);
64
65 6
        $targetMetadata = $this->target instanceof MetadataAwareInterface ? $this->target->getMetadata('', $domain) : [];
66 6
        $sourceMetadata = $this->source instanceof MetadataAwareInterface ? $this->source->getMetadata('', $domain) : [];
67 6
        $resultMetadata = $this->mergeMetaData($sourceMetadata, $targetMetadata);
68
69
        // Write back metadata
70 6
        foreach ($resultMetadata as $id => $data) {
0 ignored issues
show
Bug introduced by
The expression $resultMetadata of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
71 3
            $this->result->setMetadata($id, $data, $domain);
72 6
        }
73 6
    }
74
75
    /**
76
     * @param array|null $source
77
     * @param array|null $target
78
     *
79
     * @return array
80
     */
81 6
    private function mergeMetadata($source, $target)
82
    {
83 6
        if (empty($source) && empty($target)) {
84 3
            return [];
85
        }
86
87 3
        if (empty($source)) {
88
            return $target;
89
        }
90
91 3
        if (empty($target)) {
92
            return $source;
93
        }
94
95 3
        if (!is_array($source) || !is_array($target)) {
96
            return $source;
97
        }
98
99 3
        $result = $this->doMergeMetadata($source, $target);
100
101 3
        return $result;
102
    }
103
104 3
    private function doMergeMetadata(array $source, array $target)
105
    {
106 3
        $isTargetArrayAssociative = $this->isArrayAssociative($target);
107 3
        foreach ($target as $key => $value) {
108 3
            if ($isTargetArrayAssociative) {
109 3
                if (isset($source[$key]) && $source[$key] !== $value) {
110 3
                    if (is_array($source[$key]) && is_array($value)) {
111
                        // If both arrays, do recursive call
112 2
                        $source[$key] = $this->doMergeMetadata($source[$key], $value);
113 2
                    }
114
                    // Else, use value form $source
115 3
                } else {
116
                    // Add new value
117 3
                    $source[$key] = $value;
118
                }
119
                // if sequential
120 3
            } elseif (!in_array($value, $source)) {
121 1
                $source[] = $value;
122 1
            }
123 3
        }
124
125 3
        return $source;
126
    }
127
128 3
    public function isArrayAssociative(array $arr)
129
    {
130 3
        if ([] === $arr) {
131 1
            return false;
132
        }
133
134 3
        return array_keys($arr) !== range(0, count($arr) - 1);
135
    }
136
}
137