Completed
Push — master ( 8c9cd6...ef07c2 )
by Adam
08:13
created

ExtendedArrayTrait::arrayToPairString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\ExtendedArray;
4
5
use BestServedCold\PhalueObjects\String\StringTrait;
6
7
/**
8
 * Class ExtendedArrayTrait
9
 *
10
 * @package   BestServedCold\PhalueObjects\ExtendedArray
11
 * @author    Adam Lewis <[email protected]>
12
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
13
 * @license	  http://http://opensource.org/licenses/GPL-3.0 GPL License
14
 * @link	  http://bestservedcold.com
15
 * @since	  0.0.1-alpha
16
 * @version   0.0.8-alpha
17
 */
18
trait ExtendedArrayTrait
19
{
20
    use StringTrait;
21
22
    /**
23
     * @param  array $array
24
     * @param  Integer $spaces
25
     * @return string
26
     */
27
    public function arrayToCommaString(array $array, $spaces)
28 11
    {
29
        return implode(",{$this->integerToSpace($spaces)}", $array);
0 ignored issues
show
Documentation introduced by
$spaces is of type integer, but the function expects a object<BestServedCold\Ph...s\Mathematical\Integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30 11
    }
31
32
    public function arrayToPairString(array $array)
33
    {
34
        return implode(',', array_map(function ($key, $value) {
35
            return $key . '=' . $value;
36
        }, array_keys($array), $array));
37 3
    }
38
39 3
    /**
40
     * @param  $json
41
     * @return array
42
     */
43
    public static function getArrayUsingJsonNotation($json)
44
    {
45
        return explode('.', $json);
46
    }
47
48
    /**
49
     * Get an item from an array using "dot" notation.
50
     *
51 2
     * @param array  $array
52
     * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
53
     * @param mixed  $default
54
     *
55
     * @return mixed
56 2
     */
57 1
    public static function getFromArrayUsingJsonNotation(
58
        $array,
59
        $key = null,
60 2
        $default = null
61 2
    ) {
62
        if (is_null($key)) {
63
            return $array;
64 2
        }
65 2
66 1
        if (isset($array[ $key ])) {
67
            return $array[ $key ];
68 2
        }
69 2
70
        foreach (self::getArrayUsingJsonNotation($key) as $segment) {
71 2
            if (!is_array($array) || !array_key_exists($segment, $array)) {
72
                return $default;
73
            }
74
            $array = $array[ $segment ];
75
        }
76
77
        return $array;
78
    }
79
80
    /**
81
     * @param array $array
82
     * @param bool|false $key
83
     * @return null
84
     */
85
    public static function nullIfNotSet(array $array, $key = false)
86
    {
87
        return isset($array[$key]) ? $array[$key] : null;
88
    }
89
90
    /**
91
     * @param array $array
92
     * @param bool|false $key
93
     * @return bool
94
     */
95
    public static function falseIfNotSet(array $array, $key = false)
0 ignored issues
show
Coding Style introduced by
function falseIfNotSet() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
96
    {
97
        return isset($array[$key]) ? $array[$key] : false;
98
    }
99
}
100