Completed
Push — master ( 26e625...88c602 )
by Timo
02:58
created

HasTopicsTrait::removeTopic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tidal/WampWatch package.
5
 *   (c) 2016 Timo Michna <timomichna/yahoo.de>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Tidal\WampWatch\Model\Property\Collection;
13
14
use Tidal\WampWatch\Model\Contract;
15
use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface;
16
17
/**
18
 * Class HasTopicsTrait.
19
 *
20
 * Important! Classes using this trait have to also use trait
21
 * Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait
22
 * for this trait to work;
23
 */
24 View Code Duplication
trait HasTopicsTrait
0 ignored issues
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...
25
{
26
    protected $topicsPropertyName = 'topics';
27
28
    /**
29
     * @var ObjectCollectionInterface
30
     */
31
    private $topics;
32
33
    public function addTopic(Contract\TopicInterface $topic)
34
    {
35
        $this->getTopics()->set($topic->getUri(), $topic);
36
    }
37
38
    /**
39
     * @return ObjectCollectionInterface
40
     */
41
    private function getTopics()
42
    {
43
        return $this->getCollection($this->topicsPropertyName);
0 ignored issues
show
Bug introduced by
It seems like getCollection() 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...
44
    }
45
46
    public function hasTopic($uri)
47
    {
48
        return $this->getTopics()->has($uri);
49
    }
50
51
    /**
52
     * @param string $uri
53
     *
54
     * @return Contract\TopicInterface
55
     */
56
    public function getTopic($uri)
57
    {
58
        return $this->getTopics()->get($uri);
59
    }
60
61
    /**
62
     * @param string $uri
63
     */
64
    public function removeTopic($uri)
65
    {
66
        $this->getTopics()->offsetUnset($uri);
67
    }
68
69
    /**
70
     * @return \Generator
71
     */
72
    public function listTopics()
73
    {
74
        foreach ($this->getTopics()->getIterator() as $uri => $topic) {
75
            yield $uri => $topic;
76
        }
77
    }
78
79
    /**
80
     * @param ObjectCollectionInterface $topics
81
     */
82
    private function setTopics(ObjectCollectionInterface $topics)
83
    {
84
        $this->initCollection($this->topicsPropertyName, $topics);
0 ignored issues
show
Bug introduced by
It seems like initCollection() 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...
85
        $topics->setObjectConstrain(Contract\TopicInterface::class);
86
    }
87
}
88