ValidateJsonapiObject   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 36
ccs 9
cts 9
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateJsonapiObject() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Concern;
6
7
use VGirol\JsonApiConstant\Members;
8
use VGirol\JsonApiStructure\Messages;
9
10
/**
11
 * Validations relating to the jsonapi object
12
 */
13
trait ValidateJsonapiObject
14
{
15
    /**
16
     * Asserts that a json fragment is a valid jsonapi object.
17
     *
18
     * It will do the following checks :
19
     * 1) asserts that the jsonapi object is not an array of objects (@see mustNotBeArrayOfObjects).
20
     * 2) asserts that the jsonapi object contains only the following allowed members : "version" and "meta"
21
     * (@see containsOnlyAllowedMembers).
22
     *
23
     * Optionaly, if presents, it will :
24
     * 3) asserts that the version member is a string.
25
     * 4) asserts that meta member is valid (@see validateMetaObject).
26
     *
27
     * @param array   $json
28
     * @param boolean $strict If true, unsafe characters are not allowed when checking members name.
29
     *
30
     * @return void
31
     */
32 24
    public function validateJsonapiObject($json, bool $strict): void
33
    {
34 24
        $this->mustNotBeArrayOfObjects($json);
0 ignored issues
show
Bug introduced by
It seems like mustNotBeArrayOfObjects() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        $this->/** @scrutinizer ignore-call */ 
35
               mustNotBeArrayOfObjects($json);
Loading history...
35
36 21
        $this->containsOnlyAllowedMembers(
0 ignored issues
show
Bug introduced by
It seems like containsOnlyAllowedMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        $this->/** @scrutinizer ignore-call */ 
37
               containsOnlyAllowedMembers(
Loading history...
37 21
            $this->getRule('JsonapiObject.Allowed'),
0 ignored issues
show
Bug introduced by
It seems like getRule() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

37
            $this->/** @scrutinizer ignore-call */ 
38
                   getRule('JsonapiObject.Allowed'),
Loading history...
38
            $json
39
        );
40
41 18
        if (\array_key_exists(Members::JSONAPI_VERSION, $json)) {
42 18
            if (!\is_string($json[Members::JSONAPI_VERSION])) {
43 6
                $this->throw(Messages::JSONAPI_OBJECT_VERSION_MEMBER_MUST_BE_STRING, 403);
0 ignored issues
show
Bug introduced by
It seems like throw() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

43
                $this->/** @scrutinizer ignore-call */ 
44
                       throw(Messages::JSONAPI_OBJECT_VERSION_MEMBER_MUST_BE_STRING, 403);
Loading history...
44
            }
45
        }
46
47 12
        if (\array_key_exists(Members::META, $json)) {
48 9
            $this->validateMetaObject($json[Members::META], $strict);
0 ignored issues
show
Bug introduced by
It seems like validateMetaObject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

48
            $this->/** @scrutinizer ignore-call */ 
49
                   validateMetaObject($json[Members::META], $strict);
Loading history...
49
        }
50 6
    }
51
}
52