Completed
Push — master ( ee5489...aa45e5 )
by Luka
03:39
created

helpers.php ➔ mapValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Helper functions for Mastodon API Entity
5
 *
6
 * @author    USAMI Kenta <[email protected]>
7
 * @copyright 2017 Baguette HQ
8
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
9
 */
10
namespace Baguette\Mastodon\Entity
11
{
12
    const DATETIME_FORMAT = 'Y-m-d\TH:i:s.uO';
13
14
    /**
15
     * @param  mixed $value
16
     * @return string|array
17
     */
18
    function toArrayValue($value)
19
    {
20 22
        if ($value instanceof \DateTimeInterface) {
21 3
            return $value->format(\DateTime::W3C);
22
        }
23
24 22
        return \Teto\Object\Helper::toArray($value);
25
    }
26
27
    /**
28
     * Mapping value to object
29
     *
30
     * @param  string|string[] $class
31
     * @param  mixed           $values
32
     * @return Entity|Entity[]|\DateTimeImmutable
33
     */
34
    function map($class, $values)
35
    {
36 5
        if (!is_array($class)) {
37 4
            return ($values instanceof $class) ? $values : new $class($values);
38
        }
39
40 2
        $class = array_pop($class);
41 2
        $retval = [];
42 2
        foreach ($values as $obj) {
43 1
            $retval[] = ($obj instanceof $class) ? $obj : new $class($obj);
44
        }
45
46 2
        return $retval;
47
    }
48
49
    /**
50
     * Mapping values to object by class map
51
     *
52
     * @param  array|\ArrayAccess $input_values
53
     * @param  array|iterable     $class_map
54
     * @param  mixed           $values
0 ignored issues
show
Documentation introduced by
There is no parameter named $values. Did you maybe mean $input_values?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
55
     * @return array
56
     */
57
    function mapValues($input_values, $class_map)
58
    {
59 9
        foreach ($class_map as $prop_name => $class) {
60 9
            if (isset($input_values[$prop_name])) {
61 9
                $input_values[$prop_name] = map($class, $input_values[$prop_name]);
62
            }
63
        }
64
65 9
        return $input_values;
66
    }
67
}
68