SingleIdentifierDocument   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 53
loc 53
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getIdentifier() 4 4 1
A toArray() 8 8 1
A __toString() 4 4 1

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
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document;
5
6
/**
7
 * Single resource identifier document
8
 *
9
 * @see http://jsonapi.org/format/#document-structure
10
 *
11
 * @package Mikemirten\Component\JsonApi\Document
12
 */
13 View Code Duplication
class SingleIdentifierDocument extends AbstractDocument
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...
14
{
15
    /**
16
     * Resource identifier
17
     *
18
     * @var ResourceIdentifierObject
19
     */
20
    protected $identifier;
21
22
    /**
23
     * SingleResourceDocument constructor.
24
     *
25
     * @param ResourceIdentifierObject $resource
26
     * @param array                    $metadata
27
     */
28 19
    public function __construct(ResourceIdentifierObject $resource, array $metadata = [])
29
    {
30 19
        $this->identifier = $resource;
31 19
        $this->metadata   = $metadata;
32 19
    }
33
34
    /**
35
     * Get resource identifier
36
     *
37
     * @return ResourceIdentifierObject
38
     */
39 7
    public function getIdentifier(): ResourceIdentifierObject
40
    {
41 7
        return $this->identifier;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 6
    public function toArray(): array
48
    {
49 6
        $data = parent::toArray();
50
51 6
        $data['data'] = $this->getIdentifier()->toArray();
52
53 6
        return $data;
54
    }
55
56
    /**
57
     * Cast to a string
58
     *
59
     * @return string
60
     */
61 5
    public function __toString(): string
62
    {
63 5
        return sprintf('Document contains [%s]', $this->identifier);
64
    }
65
}