Completed
Pull Request — master (#6)
by Guilh
04:05
created

MergeHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B mergeFields() 0 19 10
1
<?php
2
namespace gossi\swagger\util;
3
4
use phootwork\collection\Map;
5
use phootwork\collection\Set;
6
7
/**
8
 * @internal
9
 */
10
class MergeHelper {
11
    /**
12
     * @param string|integer|null|Map|Set $original
13
     * @param string|integer|null|Map|Set $external
14
     * @param bool                $overwrite
15
     */
16
    public static function mergeFields(&$original, $external, $overwrite) {
17
        if ($original instanceof Map) {
18
            foreach ($external as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $external of type string|integer|null|obje...ootwork\collection\Set> 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...
19
                if ($overwrite || !$original->has($key)) {
20
                    $original->set($key, $value);
21
                }
22
            }
23
        } elseif ($original instanceof Set) {
24
            foreach ($external as $value) {
0 ignored issues
show
Bug introduced by
The expression $external of type string|integer|null|obje...ootwork\collection\Set> 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...
25
                $original->add($value);
26
            }
27
        } else { // if scalar
28
            if ($overwrite) {
29
                $original = null !== $external ? $external : $original;
30
            } else {
31
                $original = null === $original ? $external : $original;
32
            }
33
        }
34
    }
35
}
36