Completed
Pull Request — master (#92)
by Tom
13:35 queued 12:09
created

LdJson   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 98.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 65
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromJson() 6 6 1
A fromArray() 14 14 3
B generateType() 36 36 8
A isValidType() 4 4 2

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
namespace Spatie\SchemaOrg\Decoder;
4
5
use InvalidArgumentException;
6
use Spatie\SchemaOrg\BaseType;
7
use Spatie\SchemaOrg\Exceptions\InvalidType;
8
9 View Code Duplication
class LdJson
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    public static function fromJson(string $json): BaseType
12
    {
13
        $data = json_decode($json, true);
14
15
        return static::fromArray($data);
16
    }
17
18
    public static function fromArray(array $data): BaseType
19
    {
20
        if (! isset($data['@context'])) {
21
            throw new InvalidArgumentException('No context given in schema.');
22
        }
23
24
        if (strpos($data['@context'], 'schema.org') === false) {
25
            throw new InvalidArgumentException('The given @context is not valid.');
26
        }
27
28
        unset($data['@context']);
29
30
        return static::generateType($data);
31
    }
32
33
    protected static function generateType(array $data): BaseType
34
    {
35
        if (! isset($data['@type'])) {
36
            throw new InvalidType('No type given in schema.');
37
        }
38
39
        $fqcn = '\\Spatie\\SchemaOrg\\'.$data['@type'];
40
        unset($data['@type']);
41
        if (! static::isValidType($fqcn)) {
42
            throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $fqcn, BaseType::class));
43
        }
44
        /** @var BaseType $type */
45
        $type = new $fqcn();
46
47
        foreach ($data as $property => $value) {
48
            if (is_array($value)) {
49
                if (isset($value['@type'])) {
50
                    $type->setProperty($property, static::generateType($value));
51
                    continue;
52
                }
53
54
                $type->setProperty($property, array_map(function ($value) {
55
                    if (is_array($value) && isset($value['@type'])) {
56
                        return static::generateType($value);
57
                    }
58
59
                    return $value;
60
                }, $value));
61
                continue;
62
            }
63
64
            $type->setProperty($property, $value);
65
        }
66
67
        return $type;
68
    }
69
70
    protected static function isValidType(string $fqcn): bool
71
    {
72
        return class_exists($fqcn) && is_a($fqcn, BaseType::class);
73
    }
74
}
75