DataAssertions   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 19.13 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 17
c 4
b 1
f 0
lcom 1
cbo 10
dl 22
loc 115
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assert() 0 8 1
A assertItIsArray() 0 7 3
A assertItHasTypeMember() 7 7 3
A assertItTypeMemberIsExpectedValue() 4 13 3
A assertItHasAttributeMember() 7 7 3
B assertAttributesExists() 4 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/27/15
5
 * Time: 11:40 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Server\Data;
11
12
use NilPortugues\Api\JsonApi\JsonApiSerializer;
13
use NilPortugues\Api\JsonApi\JsonApiTransformer;
14
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
15
use NilPortugues\Api\JsonApi\Server\Errors\InvalidAttributeError;
16
use NilPortugues\Api\JsonApi\Server\Errors\InvalidTypeError;
17
use NilPortugues\Api\JsonApi\Server\Errors\MissingAttributesError;
18
use NilPortugues\Api\JsonApi\Server\Errors\MissingDataError;
19
use NilPortugues\Api\JsonApi\Server\Errors\MissingTypeError;
20
21
/**
22
 * Class DataAssertions.
23
 */
24
class DataAssertions
25
{
26
    /**
27
     * @param array             $data
28
     * @param JsonApiSerializer $serializer
29
     * @param string            $className
30
     * @param ErrorBag          $errorBag
31
     */
32
    public static function assert($data, JsonApiSerializer $serializer, $className, ErrorBag $errorBag)
33
    {
34
        self::assertItIsArray($data, $errorBag);
35
        self::assertItHasTypeMember($data, $errorBag);
36
        self::assertItTypeMemberIsExpectedValue($data, $serializer, $className, $errorBag);
37
        self::assertItHasAttributeMember($data, $errorBag);
38
        self::assertAttributesExists($data, $serializer, $errorBag);
39
    }
40
41
    /**
42
     * @param          $data
43
     * @param ErrorBag $errorBag
44
     *
45
     * @throws DataException
46
     */
47
    protected static function assertItIsArray($data, ErrorBag $errorBag)
48
    {
49
        if (empty($data) || !is_array($data)) {
50
            $errorBag->offsetSet(null, new MissingDataError());
51
            throw new DataException();
52
        }
53
    }
54
55
    /**
56
     * @param array    $data
57
     * @param ErrorBag $errorBag
58
     *
59
     * @throws DataException
60
     */
61 View Code Duplication
    protected static function assertItHasTypeMember(array $data, ErrorBag $errorBag)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        if (empty($data[JsonApiTransformer::TYPE_KEY]) || !is_string($data[JsonApiTransformer::TYPE_KEY])) {
64
            $errorBag->offsetSet(null, new MissingTypeError());
65
            throw new DataException();
66
        }
67
    }
68
69
    /**
70
     * @param array             $data
71
     * @param JsonApiSerializer $serializer
72
     * @param                   $className
73
     * @param ErrorBag          $errorBag
74
     *
75
     * @throws DataException
76
     */
77
    protected static function assertItTypeMemberIsExpectedValue(
78
        array $data,
79
        JsonApiSerializer $serializer,
80
        $className,
81
        ErrorBag $errorBag
82
    ) {
83
        $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]);
84
85 View Code Duplication
        if (null === $mapping || $mapping->getClassName() !== $className) {
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...
86
            $errorBag->offsetSet(null, new InvalidTypeError($data[JsonApiTransformer::TYPE_KEY]));
87
            throw new DataException();
88
        }
89
    }
90
91
    /**
92
     * @param          $data
93
     * @param ErrorBag $errorBag
94
     *
95
     * @throws DataException
96
     */
97 View Code Duplication
    protected static function assertItHasAttributeMember($data, ErrorBag $errorBag)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
98
    {
99
        if (empty($data[JsonApiTransformer::ATTRIBUTES_KEY]) || !is_array($data[JsonApiTransformer::ATTRIBUTES_KEY])) {
100
            $errorBag->offsetSet(null, new MissingAttributesError());
101
            throw new DataException();
102
        }
103
    }
104
105
    /**
106
     * @param array             $data
107
     * @param JsonApiSerializer $serializer
108
     * @param ErrorBag          $errorBag
109
     *
110
     * @throws DataException
111
     */
112
    protected static function assertAttributesExists(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag)
113
    {
114
        $inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]);
115
116
        $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]);
117
118
        $properties = str_replace(
119
            array_keys($mapping->getAliasedProperties()),
120
            array_values($mapping->getAliasedProperties()),
121
            $mapping->getProperties()
122
        );
123
        $properties = array_diff($properties, $mapping->getIdProperties());
124
        $properties = array_merge($properties, $mapping->getHiddenProperties());
125
126
        $hasErrors = false;
127
        foreach ($inputAttributes as $property) {
128 View Code Duplication
            if (false === in_array($property, $properties)) {
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...
129
                $hasErrors = true;
130
                $errorBag->offsetSet(null, new InvalidAttributeError($property, $data[JsonApiTransformer::TYPE_KEY]));
131
            }
132
        }
133
134
        if ($hasErrors) {
135
            throw new DataException();
136
        }
137
    }
138
}
139