Completed
Push — 1.4 ( a3fc6f...89c66f )
by Paweł
14s
created

KeywordsAwareTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 40
loc 40
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeywords() 4 4 1
A setKeywords() 4 4 1
A addKeyword() 6 6 2
A removeKeyword() 6 6 2
A hasKeyword() 4 4 1
A getKeywordsNames() 6 6 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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Bridge Component.
7
 *
8
 * Copyright 2018 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Model;
18
19
use Doctrine\Common\Collections\Collection;
20
21 View Code Duplication
trait KeywordsAwareTrait
1 ignored issue
show
Duplication introduced by
This class 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...
22
{
23
    protected $keywords;
24
25
    public function getKeywords(): Collection
26
    {
27
        return $this->keywords;
28
    }
29
30
    public function setKeywords(Collection $keywords): void
31
    {
32
        $this->keywords = $keywords;
33
    }
34
35
    public function addKeyword(KeywordInterface $keyword): void
36
    {
37
        if (!$this->hasKeyword($keyword)) {
38
            $this->keywords->add($keyword);
39
        }
40
    }
41
42
    public function removeKeyword(KeywordInterface $keyword): void
43
    {
44
        if ($this->hasKeyword($keyword)) {
45
            $this->keywords->removeElement($keyword);
46
        }
47
    }
48
49
    public function hasKeyword(KeywordInterface $keyword): bool
50
    {
51
        return $this->keywords->contains($keyword);
52
    }
53
54
    public function getKeywordsNames(): array
55
    {
56
        return array_map(function ($keyword) {
57
            return $keyword->getName();
58
        }, $this->keywords->toArray());
59
    }
60
}
61