Completed
Push — master ( e4e412...69c76a )
by Sebastian
06:13 queued 03:47
created

Graph   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 4
dl 97
loc 97
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 18 18 4
A add() 10 10 2
A has() 4 4 1
A set() 4 4 1
A get() 8 8 2
A getOrCreate() 12 12 3
A hide() 6 6 1
A show() 6 6 1
A toArray() 15 15 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 BadMethodCallException;
7
use Spatie\SchemaOrg\Exceptions\InvalidType;
8
use Spatie\SchemaOrg\Exceptions\TypeNotInGraph;
9
use Spatie\SchemaOrg\Exceptions\TypeAlreadyInGraph;
10
11
/**
12
 * @mixin \Spatie\SchemaOrg\Schema
13
 */
14 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...
15
{
16
    /** @var array */
17
    protected $hidden = [];
18
19
    public function __call(string $method, array $arguments)
20
    {
21
        if (is_callable([Schema::class, $method])) {
22
            $type = (new ReflectionClass(Schema::class))->getMethod($method)->getReturnType();
23
24
            $schema = $this->getOrCreate($type);
25
26
            if (isset($arguments[0]) && is_callable($arguments[0])) {
27
                call_user_func($arguments[0], $schema, $this);
28
29
                return $this;
30
            }
31
32
            return $schema;
33
        }
34
35
        throw new BadMethodCallException(sprintf('The method "%" does not exist on class "%s".', $method, get_class($this)));
36
    }
37
38
    public function add(Type $schema): self
39
    {
40
        $type = get_class($schema);
41
42
        if ($this->has($type)) {
43
            throw new TypeAlreadyInGraph(sprintf('The graph already has an item of type "%s".', $type));
44
        }
45
46
        return $this->set($schema);
47
    }
48
49
    public function has(string $type): bool
50
    {
51
        return $this->offsetExists($type);
52
    }
53
54
    public function set(Type $schema)
55
    {
56
        return $this->setProperty(get_class($schema), $schema);
57
    }
58
59
    public function get(string $type): Type
60
    {
61
        if (! $this->has($type)) {
62
            throw new TypeNotInGraph(sprintf('The graph does not have an item of type "%s".', $type));
63
        }
64
65
        return $this->getProperty($type);
66
    }
67
68
    public function getOrCreate(string $type): Type
69
    {
70
        if (! is_subclass_of($type, Type::class)) {
71
            throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
72
        }
73
74
        if (! $this->has($type)) {
75
            $this->set(new $type());
76
        }
77
78
        return $this->get($type);
79
    }
80
81
    public function hide(string $type): self
82
    {
83
        $this->hidden[$type] = true;
84
85
        return $this;
86
    }
87
88
    public function show(string $type): self
89
    {
90
        $this->hidden[$type] = false;
91
92
        return $this;
93
    }
94
95
    public function toArray(): array
96
    {
97
        $properties = $this->getProperties();
98
99
        foreach ($this->hidden as $type => $hide) {
100
            if ($hide) {
101
                unset($properties[$type]);
102
            }
103
        }
104
105
        return [
106
            '@context' => $this->getContext(),
107
            '@graph' => $this->serializeProperty(array_values($properties)),
108
        ];
109
    }
110
}
111