Completed
Push — master ( a2b732...080f9b )
by Rai
10s
created

SetPropertiesEntityTrait   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 134
Duplicated Lines 5.22 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 7
loc 134
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
D setPropertiesEntity() 7 131 31

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 Bludata\Doctrine\Common\Traits;
4
5
use Bludata\Common\Helpers\FormatHelper;
6
use Bludata\Doctrine\Common\Annotations\ToObject;
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\ManyToMany;
11
use Doctrine\ORM\Mapping\ManyToOne;
12
use Doctrine\ORM\Mapping\OneToMany;
13
use ReflectionClass;
14
use ReflectionProperty;
15
16
trait SetPropertiesEntityTrait
17
{
18
    public function setPropertiesEntity(array $data)
19
    {
20
        foreach ($data as $key => $value) {
21
            $set = true;
22
23 View Code Duplication
            if (
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...
24
                ((!isset($data['id']) || !is_numeric($data['id'])) && !in_array($key, $this->getOnlyStore()))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
Bug introduced by
It seems like getOnlyStore() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
                ||
26
                (isset($data['id']) && is_numeric($data['id']) && !in_array($key, $this->getOnlyUpdate()))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 106 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
Bug introduced by
It seems like getOnlyUpdate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
            ) {
28
                $set = false;
29
            }
30
31
            $methodSet = 'set'.ucfirst($key);
32
            $methodGet = 'get'.ucfirst($key);
33
34
            if (method_exists($this, $methodSet) && $set) {
35
                /*
36
                 * Seta a propriedade com o valor enviado pelo usuário.
37
                 */
38
                $this->$methodSet(is_string($value) && strlen($value) <= 0 ? null : $value);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 92 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
39
40
                /**
41
                 * Armazena o valor enviado pelo usuário.
42
                 */
43
                $valueKey = $this->$methodGet();
44
45
                /**
46
                 * Classes utilizadas para buscar os metadados da classe e suas propriedades.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 93 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
47
                 */
48
                $reflectionClass = new ReflectionClass(get_called_class());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49
                $reflectionProperty = new ReflectionProperty(get_called_class(), $key);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 87 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
50
                $annotationReader = new AnnotationReader();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
52
                $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 102 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
53
54
                /**
55
                 * Busca a anotação Doctrine\ORM\Mapping\Column.
56
                 */
57
                $column = array_filter($propertyAnnotations, function ($annotation) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
58
                    return $annotation instanceof Column;
59
                });
60
61
                if ($column) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $column of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
                    $column = $column[0];
63
                }
64
65
                /**
66
                 * Busca a anotação Bludata\Doctrine\Common\Annotations\ToObject.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
                 */
68
                $toObject = array_filter($propertyAnnotations, function ($annotation) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 87 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
69
                    return $annotation instanceof ToObject;
70
                });
71
72
                /*
73
                 * Verifica se a propriedade está usando a anotação Bludata\Doctrine\Common\Annotations\ToObject.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
74
                 */
75
                if ($toObject) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toObject of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76
                    /*
77
                     * Caso seja um campo de data, utilizamos o método FormatHelper::parseDate para converter o valor enviado pelo usuário para um objeto DateTime.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 163 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
                     */
79
                    if ($column instanceof Column && ($column->type == 'date' || $column->type == 'datetime')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 112 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
80
                        $this->$methodSet(
81
                            FormatHelper::parseDate($valueKey)
82
                        );
83
                    } else {
84
                        /**
85
                         * Busca pelas anotações Doctrine\ORM\Mapping\ManyToOne || Doctrine\ORM\Mapping\OneToMany || Doctrine\ORM\Mapping\ManyToMany.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 149 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
86
                         */
87
                        $ormMapping = array_filter($propertyAnnotations, function ($annotation) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 97 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
88
                            return $annotation instanceof ManyToOne
89
                                   ||
90
                                   $annotation instanceof OneToMany
91
                                   ||
92
                                   $annotation instanceof ManyToMany;
93
                        });
94
95
                        /*
96
                         * Se for encontrado alguma das anotações, iremos realizar o tratamento adequado para a anotação encontrada.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
97
                         */
98
                        if ($ormMapping) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ormMapping of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99
                            $ormMapping = $ormMapping[0];
100
101
                            $targetEntityName = $reflectionClass->getNamespaceName().'\\'.$ormMapping->targetEntity;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 116 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
102
                            $targetEntity = new $targetEntityName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
103
                            $repositoryTargetEntity = $targetEntity->getRepository();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104
105
                            /*
106
                             * Se a propriedade estiver utilizando a anotação Doctrine\ORM\Mapping\ManyToOne e o usuário
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 120 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
107
                             * informou um número, então buscamos o devido objeto pelo seu id.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 94 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
                             */
109
                            if ($ormMapping instanceof ManyToOne && is_numeric($valueKey)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 92 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
                                $this->$methodSet(
111
                                    $repositoryTargetEntity->find($valueKey)
112
                                );
113
                            } elseif (($ormMapping instanceof OneToMany || $ormMapping instanceof ManyToMany) && is_array($valueKey)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
114
                                /**
115
                                 * Caso a propriedade esteja utilizando as anotações Doctrine\ORM\Mapping\OneToMany || Doctrine\ORM\Mapping\ManyToMany,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
116
                                 * então o usuário terá que implementar o método addX?().
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 89 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
117
                                 * Do contrário será lançada uma BadMethodCallException.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 88 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118
                                 */
119
                                $methodAdd = 'add'.$ormMapping->targetEntity;
120
                                if (!method_exists($this, $methodAdd)) {
121
                                    throw new \BadMethodCallException('Para utilizar Bludata\Doctrine\Common\Annotations\ToObject em '.get_called_class().'::$'.$key.' você precisar declarar o método '.get_called_class().'::'.$methodAdd.'()');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 242 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
122
                                }
123
124
                                if (
125
                                    (
126
                                        $ormMapping instanceof OneToMany
127
                                        ||
128
                                        $ormMapping instanceof ManyToMany
129
                                    )
130
                                    && is_array($valueKey)
131
                                ) {
132
                                    $this->$methodSet(new ArrayCollection());
133
134
                                    foreach ($valueKey as $value) {
135
                                        $this->$methodAdd(
136
                                            $ormMapping instanceof OneToMany ? $repositoryTargetEntity->findOrCreate($value) : $repositoryTargetEntity->find($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 164 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
137
                                        );
138
                                    }
139
                                }
140
                            }
141
                        }
142
                    }
143
                }
144
            }
145
        }
146
147
        return $this;
148
    }
149
}
150