Passed
Branch nested_save (73f795)
by JHONATAN
02:34
created

TransferMetadata   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 19.72 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 8
dl 14
loc 71
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B addPropertyMetadata() 0 20 5
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\Id;
12
13
class TransferMetadata extends ClassMetadata
14
{
15
    /**
16
     * @var PropertyMetadata
17
     */
18
    public $id;
19
    
20
    /**
21
     * @var PropertyMetadata[]
22
     */
23
    public $associations = [];
24
    
25 20
    public function addPropertyMetadata(BasePropertyMetadata $metadata)
26
    {
27 20
        if ($metadata instanceof PropertyMetadata) {
28 20
            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...
29 10
                $this->id = $metadata;
30
            }
31
        }
32
        
33 20
        parent::addPropertyMetadata($metadata);
34
        
35 20
        if ($metadata->hasAnnotation(BelongsTo::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

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