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

MergeHelper::mergeFields()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 7.2765
cc 10
eloc 13
nc 9
nop 3

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
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