Passed
Branch master (7d5d56)
by Carlos
04:10
created

data_to_array()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 6
nop 1
dl 0
loc 15
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel;
13
14
use EasyWeChat\Kernel\Contracts\Arrayable;
15
use EasyWeChat\Kernel\Exceptions\RuntimeException;
16
use EasyWeChat\Kernel\Support\Arr;
17
use EasyWeChat\Kernel\Support\Collection;
18
19
function data_get($data, $key, $default = null)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
20
{
21
    switch (true) {
22
        case is_array($data):
23
            return Arr::get($data, $key, $default);
24
        case $data instanceof Collection:
25
            return $data->get($key, $default);
26
        case $data instanceof Arrayable:
27
            return Arr::get($data->toArray(), $key, $default);
28
        case $data instanceof \ArrayIterator:
29
            return $data->getArrayCopy()[$key] ?? $default;
30
        case $data instanceof \ArrayAccess:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
31
            return $data[$key] ?? $default;
32
        case $data instanceof \IteratorAggregate && $data->getIterator() instanceof \ArrayIterator:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
33
            return $data->getIterator()->getArrayCopy()[$key] ?? $default;
0 ignored issues
show
Bug introduced by
The method getArrayCopy() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as ArrayObject or ArrayIterator or RecursiveArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            return $data->getIterator()->/** @scrutinizer ignore-call */ getArrayCopy()[$key] ?? $default;
Loading history...
34
        default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
35
            throw new RuntimeException(sprintf('Can\'t access data with key "%s"', $key));
36
    }
37
}
38
39
function data_to_array($data)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
40
{
41
    switch (true) {
42
        case is_array($data):
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
43
            return $data;
44
        case $data instanceof Collection:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
45
            return $data->all();
46
        case $data instanceof Arrayable:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
            return $data->toArray();
48
        case $data instanceof \IteratorAggregate && $data->getIterator() instanceof \ArrayIterator:
49
            return $data->getIterator()->getArrayCopy();
50
        case $data instanceof \ArrayIterator:
51
            return $data->getArrayCopy();
52
        default:
53
            throw new RuntimeException(sprintf('Can\'t transform data to array'));
54
    }
55
}
56