Passed
Push — master ( 30b30c...28afa9 )
by JHONATAN
09:58
created

TransferMetadata   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 19.18 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 11
dl 14
loc 73
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C addPropertyMetadata() 0 22 8
A serialize() 0 11 1
A unserialize() 14 14 1
A getAssociation() 0 3 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
3
namespace Vox\Webservice\Metadata;
4
5
use Metadata\PropertyMetadata as BasePropertyMetadata;
6
use ReflectionClass;
7
use Vox\Data\Mapping\Exclude;
8
use Vox\Metadata\ClassMetadata;
9
use Vox\Metadata\PropertyMetadata;
10
use Vox\Webservice\Mapping\BelongsTo;
11
use Vox\Webservice\Mapping\HasMany;
12
use Vox\Webservice\Mapping\HasOne;
13
use Vox\Webservice\Mapping\Id;
14
15
/**
16
 * Holds a single transfer metadata information
17
 * 
18
 * @author Jhonatan Teixeira <[email protected]>
19
 */
20
class TransferMetadata extends ClassMetadata
21
{
22
    /**
23
     * @var PropertyMetadata
24
     */
25
    public $id;
26
    
27
    /**
28
     * @var PropertyMetadata[]
29
     */
30
    public $associations = [];
31
    
32 22
    public function addPropertyMetadata(BasePropertyMetadata $metadata)
33
    {
34 22
        if ($metadata instanceof PropertyMetadata) {
35 22
            if ($id = $metadata->getAnnotation(Id::class)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
36 12
                $this->id = $metadata;
37
            }
38
        }
39
        
40 22
        parent::addPropertyMetadata($metadata);
41
        
42 22
        if ($metadata->hasAnnotation(BelongsTo::class) && !$metadata->hasAnnotation(Exclude::class)) {
0 ignored issues
show
introduced by
The method hasAnnotation() does not exist on Metadata\PropertyMetadata. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        if ($metadata->/** @scrutinizer ignore-call */ hasAnnotation(BelongsTo::class) && !$metadata->hasAnnotation(Exclude::class)) {
Loading history...
43 6
            $metadata->annotations[Exclude::class] = new Exclude();
44 6
            $metadata->annotations[Exclude::class]->output = false;
45
        }
46
        
47 22
        if ($metadata->hasAnnotation(BelongsTo::class)
48 22
            || $metadata->hasAnnotation(HasOne::class)
49 22
            || $metadata->hasAnnotation(HasMany::class)) {
50 6
            $this->associations[$metadata->name] = $metadata;
51
        }
52
        
53 22
        return $this;
54
    }
55
    
56
    /**
57
     * @param string $name
58
     * @return PropertyMetadata
59
     */
60
    public function getAssociation(string $name)
61
    {
62
        return $this->associations[$name] ?? null;
63
    }
64
    
65 4
    public function serialize()
66
    {
67 4
        return serialize(array(
68 4
            $this->name,
69 4
            $this->methodMetadata,
70 4
            $this->propertyMetadata,
71 4
            $this->fileResources,
72 4
            $this->createdAt,
73 4
            $this->annotations,
74 4
            $this->id,
75 4
            $this->associations,
76
        ));
77
    }
78
79 3 View Code Duplication
    public function unserialize($str)
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...
80
    {
81
        list(
82 3
            $this->name,
83 3
            $this->methodMetadata,
84 3
            $this->propertyMetadata,
85 3
            $this->fileResources,
86 3
            $this->createdAt,
87 3
            $this->annotations,
88 3
            $this->id,
89 3
            $this->associations
90 3
        ) = unserialize($str);
91
92 3
        $this->reflection = new ReflectionClass($this->name);
93 3
    }
94
}
95