Completed
Pull Request — master (#62)
by Tom
05:40
created

Graph::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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