set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Adds or replace an element to a collection using an optional key/index.
8
 * If the given key already exists in the collection, the corresponding value will be replaced by the element
9
 *
10
 * @example $set = ['a' => 1, 2, 3, 4]
11
 * set($set, 2, 'a') $set will contain ['a' => 2, 2, 3, 4]
12
 *
13
 * @example $set = ['a' => 1, 2, 3, 4]
14
 * set($set, 1) $set will contain ['a' => 1, 2, 3, 4, 1]
15
 *
16
 * @param  array      $set     The collection
17
 * @param  mixed      $element The element to be added
18
 * @param  string|int $key     The key/index of element
19
 * @return void
20
 */
21
function set(array &$set, $element, $key = null): void
22
{
23
    if ($key !== null) {
24
        $set[$key] = $element;
25
    } else {
26
        $set[] = $element;
27
    }
28
}
29