ManagerObjectTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 19
c 4
b 0
f 3
lcom 1
cbo 4
dl 32
loc 144
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addTag() 0 4 1
A addTags() 0 10 4
A refreshTags() 0 5 1
A getTagRelation() 0 6 1
A queryTag() 0 9 1
A slugify() 0 10 2
A addNameConstraint() 16 16 3
A addModelTypeConstraint() 16 16 3
A queryRelation() 0 15 1
A findRelationByTagName() 0 22 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
 * Created by PhpStorm.
4
 * User: Rafidion Michael
5
 * Date: 10/12/2014
6
 * Time: 12:33
7
 */
8
9
namespace Mykees\TagBundle\Traits;
10
11
use Mykees\TagBundle\Util\Urlizer;
12
use Mykees\TagBundle\Entity\Tag;
13
use Mykees\TagBundle\Interfaces\Taggable;
14
use Doctrine\ORM\Query\Expr;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\ORM\QueryBuilder;
17
18
trait ManagerObjectTrait {
19
20
    /**
21
     * Insert one Tag in model referer
22
     * @param Tag $tag
23
     * @param Taggable $model
24
     */
25
    protected function addTag(Tag $tag, Taggable $model)
26
    {
27
        $model->getTags()->add($tag);
28
    }
29
30
    /**
31
     * Insert several Tags in model referer
32
     * @param array $tags
33
     * @param Taggable $model
34
     */
35
    protected function addTags(array $tags, Taggable $model)
36
    {
37
        foreach($tags as $tag)
38
        {
39
            if(!empty($tag) && $tag instanceof Tag)
40
            {
41
                $this->addTag($tag,$model);
42
            }
43
        }
44
    }
45
46
    /**
47
     * Remove and insert tags in model referer
48
     * @param array $tags
49
     * @param Taggable $model
50
     */
51
    protected function refreshTags(array $tags, Taggable $model)
52
    {
53
        $model->getTags()->clear();
54
        $this->addTags($tags,$model);
55
    }
56
57
    /**
58
     * Get Tags and Relations
59
     * @param Taggable $model
60
     */
61
    protected function getTagRelation(Taggable $model)
62
    {
63
        $query = $this->queryRelation($model);
64
65
        return $this->refreshTags($query,$model);
66
    }
67
68
    protected function queryTag()
69
    {
70
        $query = $this->em->createQueryBuilder()
0 ignored issues
show
Bug introduced by
The property em does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
            ->select('t')
72
            ->from($this->tag,'t')
0 ignored issues
show
Bug introduced by
The property tag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
        ;
74
75
        return $query;
76
    }
77
78
    protected function slugify(array $datas)
79
    {
80
        $datas = array_unique($datas);
81
        foreach($datas as $k=>$d)
82
        {
83
            $datas[$k] = Urlizer::urlize($d);
84
        }
85
86
        return $datas;
87
    }
88
89 View Code Duplication
    protected function addNameConstraint(QueryBuilder $query,$names)
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...
90
    {
91
        if($names)
92
        {
93
            if(is_array($names))
94
            {
95
                $names = $this->slugify($names);
96
                $query->where($query->expr()->in('t.slug', $names));
97
            }else{
98
                $query->andWhere('t.slug = :slug')
99
                    ->setParameter('slug',Urlizer::urlize($names))
100
                ;
101
            }
102
        }
103
        return $query;
104
    }
105
106 View Code Duplication
    protected function addModelTypeConstraint(QueryBuilder $query,$modelType)
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...
107
    {
108
        if($modelType)
109
        {
110
            if(is_array($modelType))
111
            {
112
                $modelType = $this->slugify($modelType);
113
                $query->andWhere($query->expr()->in('tr.model', $modelType));
114
            }else{
115
                $query->andWhere('tr.model = :model')
116
                    ->setParameter('model',$modelType)
117
                ;
118
            }
119
        }
120
        return $query;
121
    }
122
123
    protected function queryRelation(Taggable $model)
124
    {
125
        $query = $this->em->createQueryBuilder()
126
            ->select('t')
127
            ->from($this->tag,'t')
128
            ->innerJoin('t.tagRelation','tg',Expr\Join::WITH, 'tg.model = :model AND tg.modelId = :modelId')
129
            ->addSelect('tg')
130
            ->setParameter('model',$model->getModel())
131
            ->setParameter('modelId',$model->getModelId())
132
            ->getQuery()
133
            ->getResult()
134
        ;
135
136
        return $query;
137
    }
138
139
    protected function findRelationByTagName(ArrayCollection $names, Taggable $model)
140
    {
141
        $slugified = [];
142
        foreach($names as $name)
143
        {
144
            $slugified[] = $name->getSlug();
145
        }
146
147
        $query = $this->em->createQueryBuilder()
148
            ->select('t')
149
            ->from($this->tag,'t')
150
            ->innerJoin('t.tagRelation','tg',Expr\Join::WITH, 'tg.model = :model AND tg.modelId = :modelId')
151
            ->addSelect('tg')
152
            ->where($this->em->createQueryBuilder()->expr()->in('t.slug', $slugified))
153
            ->setParameter('model',$model->getModel())
154
            ->setParameter('modelId',$model->getModelId())
155
            ->getQuery()
156
            ->getResult()
157
        ;
158
        
159
        return $query;
160
    }
161
}
162