Passed
Push — master ( 672c08...9a8256 )
by Anton
02:40
created

JsonApiParser::parse()   C

Complexity

Conditions 11
Paths 6

Size

Total Lines 24
Code Lines 16

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 8
loc 24
rs 5.3305
c 0
b 0
f 0
cc 11
eloc 16
nc 6
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi;
7
8
use yii\helpers\ArrayHelper;
9
use yii\helpers\Inflector;
10
use \yii\web\JsonParser;
11
12
class JsonApiParser extends JsonParser
13
{
14
    /**
15
     * Parse resource object into the input data to populates the model
16
     * @inheritdoc
17
     */
18
    public function parse($rawBody, $contentType)
19
    {
20
        $array = parent::parse($rawBody, $contentType);
21
        if ($type = ArrayHelper::getValue($array, 'data.type')) {
22
            $formName = Inflector::id2camel(Inflector::singularize($type));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
23
            $result[$formName] = ArrayHelper::getValue($array, 'data.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...
24
            if ($relationships = ArrayHelper::getValue($array, 'data.relationships')) {
25
                foreach ($relationships as $name => $relationship) {
26
                    if (isset($relationship[0])) {
27
                        foreach ($relationship as $item) {
28 View Code Duplication
                            if (isset($item['type']) && isset($item['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
                                $formName = Inflector::id2camel(Inflector::singularize($item['type']));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
30
                                $result[$name][$formName][] = $item;
31
                            }
32
                        }
33 View Code Duplication
                    } elseif (isset($relationship['type']) && isset($relationship['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
                        $formName = Inflector::id2camel(Inflector::singularize($relationship['type']));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
                        $result[$name][$formName] = $relationship;
36
                    }
37
                }
38
            }
39
        }
40
        return isset($result) ? $result : $array;
41
    }
42
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
43