Passed
Push — master ( 9a8256...ab52bb )
by Anton
02:11
created

src/JsonApiParser.php (2 issues)

lazy-initialization of arrays.

Coding Style Comprehensibility Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi;
7
8
use yii\base\InvalidConfigException;
9
use yii\helpers\ArrayHelper;
10
use yii\helpers\Inflector;
11
use \yii\web\JsonParser;
12
13
class JsonApiParser extends JsonParser
14
{
15
    /**
16
     * @var array|callable|null
17
     */
18
    protected $formNameCallback;
19
20
    public function __construct($formNameCallback = null)
21
    {
22
        if ($formNameCallback === null) {
23
            $formNameCallback = [$this, 'typeToFormName'];
24
        }
25
        if (!is_callable($formNameCallback, true)) {
26
            throw new InvalidConfigException('JsonApiParser::formNameCallback should be callable');
27
        }
28
29
        $this->formNameCallback = $formNameCallback;
30
    }
31
32
33
    /**
34
     * Parse resource object into the input data to populates the model
35
     * @inheritdoc
36
     */
37
    public function parse($rawBody, $contentType)
38
    {
39
        $array = parent::parse($rawBody, $contentType);
40
        if ($type = ArrayHelper::getValue($array, 'data.type')) {
41
            $formName = call_user_func($this->formNameCallback, $type);
42
            if ($attributes = ArrayHelper::getValue($array, 'data.attributes')) {
43
                $result[$formName] = $attributes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
44
            } elseif ($id = ArrayHelper::getValue($array, 'data.id')) {
45
                $result[$formName] = ['id' => $id, 'type' => $type];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
            }
47
            if ($relationships = ArrayHelper::getValue($array, 'data.relationships')) {
48
                foreach ($relationships as $name => $relationship) {
49
                    if (isset($relationship[0])) {
50 View Code Duplication
                        foreach ($relationship as $item) {
51
                            if (isset($item['type']) && isset($item['id'])) {
52
                                $formName = call_user_func($this->formNameCallback, $item['type']);
53
                                $result[$name][$formName][] = $item;
54
                            }
55
                        }
56
                    } elseif (isset($relationship['type']) && isset($relationship['id'])) {
57
                        $formName = call_user_func($this->formNameCallback, $relationship['type']);
58
                        $result[$name][$formName] = $relationship;
59
                    }
60
                }
61
            }
62
        } else {
63
            $data = ArrayHelper::getValue($array, 'data', []);
64 View Code Duplication
            foreach ($data as $relationLink) {
65
                if (isset($relationLink['type']) && isset($relationLink['id'])) {
66
                    $formName = call_user_func($this->formNameCallback, $relationLink['type']);
67
                    $result[$formName][] = $relationLink;
68
                }
69
            }
70
        }
71
        return isset($result) ? $result : $array;
72
    }
73
74
    protected function typeToFormName($type)
75
    {
76
        return Inflector::id2camel(Inflector::singularize($type));
77
    }
78
}
79