Completed
Pull Request — master (#62)
by Tom
08:04
created

Graph   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 98.77 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 1
dl 80
loc 81
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 6 6 2
A add() 9 9 2
A has() 4 4 1
A set() 4 4 1
A get() 8 8 2
A getOrNew() 10 10 3
A hide() 6 6 1
A show() 6 6 1
A toArray() 14 14 3

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;
4
5
use ReflectionClass;
6
use InvalidArgumentException;
7
8
/**
9
 * @mixin Schema
10
 */
11 View Code Duplication
class Graph extends BaseType
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...
12
{
13
    /** @var array */
14
    protected $hidden = [];
15
16
    public function __call(string $method, array $arguments)
17
    {
18
        if (is_callable([Schema::class, $method])) {
19
            return $this->getOrNew((new ReflectionClass(Schema::class))->getMethod($method)->getReturnType());
20
        }
21
    }
22
23
    public function add(Type $schema)
24
    {
25
        $type = get_class($schema);
26
        if ($this->has($type)) {
27
            throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type));
28
        }
29
30
        return $this->set($schema);
31
    }
32
33
    public function has(string $type): bool
34
    {
35
        return $this->offsetExists($type);
36
    }
37
38
    public function set(Type $schema)
39
    {
40
        return $this->setProperty(get_class($schema), $schema);
41
    }
42
43
    public function get(string $type): Type
44
    {
45
        if (! $this->has($type)) {
46
            throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type));
47
        }
48
49
        return $this->getProperty($type);
50
    }
51
52
    public function getOrNew(string $type): Type
53
    {
54
        if ($this->has($type)) {
55
            return $this->get($type);
56
        } elseif (is_subclass_of($type, Type::class)) {
57
            return $this->properties[$type] = new $type();
58
        }
59
60
        throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
61
    }
62
63
    public function hide(string $type)
64
    {
65
        $this->hidden[$type] = true;
66
67
        return $this;
68
    }
69
70
    public function show(string $type)
71
    {
72
        $this->hidden[$type] = false;
73
74
        return $this;
75
    }
76
77
    public function toArray(): array
78
    {
79
        $properties = $this->getProperties();
80
        foreach ($this->hidden as $type => $hide) {
81
            if ($hide) {
82
                unset($properties[$type]);
83
            }
84
        }
85
86
        return [
87
            '@context' => $this->getContext(),
88
            '@graph' => $this->serializeProperty(array_values($properties)),
89
        ];
90
    }
91
}
92