Completed
Pull Request — master (#79)
by Jacob
02:58
created

EmbeddedPropMetadata   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 9.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 1
dl 8
loc 81
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isOne() 0 4 1
A isMany() 0 4 1
A setEmbedType() 0 7 1
A validateType() 8 8 2

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 As3\Modlr\Metadata;
4
5
use As3\Modlr\Exception\MetadataException;
6
7
/**
8
 * Defines metadata for an embedded field.
9
 * Should be loaded using the MetadataFactory, not instantiated directly.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class EmbeddedPropMetadata extends FieldMetadata
14
{
15
    /**
16
     * The embedded metadata for this embedded property.
17
     *
18
     * @var string
19
     */
20
    public $embedMeta;
21
22
    /**
23
     * The embed type: one or many
24
     *
25
     * @var string
26
     */
27
    public $embedType;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param   string          $key
33
     * @param   string          $embedType
34
     * @param   EmbedMetadata   $embedMeta
35
     * @param   bool            $mixin
36
     */
37
    public function __construct($key, $embedType, EmbedMetadata $embedMeta, $mixin = false)
38
    {
39
        $this->embedMeta = $embedMeta;
0 ignored issues
show
Documentation Bug introduced by
It seems like $embedMeta of type object<As3\Modlr\Metadata\EmbedMetadata> is incompatible with the declared type string of property $embedMeta.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $this->setEmbedType($embedType);
41
        parent::__construct($key, $mixin);
42
    }
43
44
    /**
45
     * Determines if this is a one (single) embed.
46
     *
47
     * @return  bool
48
     */
49
    public function isOne()
50
    {
51
        return 'one' === $this->embedType;
52
    }
53
54
    /**
55
     * Determines if this is a many embed.
56
     *
57
     * @return bool
58
     */
59
    public function isMany()
60
    {
61
        return 'many' === $this->embedType;
62
    }
63
64
    /**
65
     * Sets the embed type: one or many.
66
     *
67
     * @param   string  $relType
0 ignored issues
show
Bug introduced by
There is no parameter named $relType. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
     * @return  self
69
     */
70
    public function setEmbedType($embedType)
71
    {
72
        $embedType = strtolower($embedType);
73
        $this->validateType($embedType);
74
        $this->embedType = $embedType;
75
        return $this;
76
    }
77
78
    /**
79
     * Validates the embed type.
80
     *
81
     * @param   string  $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     * @return  bool
83
     * @throws  MetadataException
84
     */
85 View Code Duplication
    protected function validateType($embedType)
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...
86
    {
87
        $valid = ['one', 'many'];
88
        if (!in_array($embedType, $valid)) {
89
            throw MetadataException::invalidRelType($embedType, $valid);
90
        }
91
        return true;
92
    }
93
}
94