Channel   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 178
Duplicated Lines 10.11 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 18
loc 178
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A who() 0 4 1
A onPart() 0 11 4
A destroy() 0 4 1
A setChanType() 0 4 1
A exportNicksArray() 0 8 2
A setTopic() 0 4 1
A addMode() 10 11 3
A removeMode() 8 9 2
A join() 0 4 1
A part() 0 4 1
A setType() 0 5 1
A detach() 0 5 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
namespace PHPDaemon\Clients\IRC;
3
4
use PHPDaemon\Structures\ObjectStorage;
5
use PHPDaemon\Utils\IRC;
6
7
/**
8
 * @package    NetworkClients
9
 * @subpackage IRCClient
10
 * @author     Vasily Zorin <[email protected]>
11
 */
12
class Channel extends ObjectStorage
13
{
14
    use \PHPDaemon\Traits\EventHandlers;
15
16
    /**
17
     * @var Connection
18
     */
19
    public $irc;
20
21
    /**
22
     * @var string
23
     */
24
    public $name;
25
26
    /**
27
     * @var array
28
     */
29
    public $nicknames = [];
30
31
    /**
32
     * @var
33
     */
34
    public $self;
35
36
    /**
37
     * @var string
38
     */
39
    public $type;
40
41
    /**
42
     * @var string
43
     */
44
    public $topic;
45
46
    /**
47
     * @param Connection $irc
48
     * @param string $name
49
     */
50
    public function __construct($irc, $name)
51
    {
52
        $this->irc = $irc;
53
        $this->name = $name;
54
    }
55
56
    /**
57
     * @TODO DESCR
58
     */
59
    public function who()
60
    {
61
        $this->irc->command('WHO', $this->name);
62
    }
63
64
    /**
65
     * @TODO DESCR
66
     * @param array|string $mask
67
     * @param mixed $msg
68
     */
69
    public function onPart($mask, $msg = null)
0 ignored issues
show
Unused Code introduced by
The parameter $msg is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        if (is_string($mask)) {
72
            $mask = IRC::parseUsermask($mask);
73
        }
74
        if (($mask['nick'] === $this->irc->nick) && ($mask['user'] === $this->irc->user)) {
0 ignored issues
show
Documentation introduced by
The property $user is declared protected in PHPDaemon\Clients\IRC\Connection. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
            $this->destroy();
76
        } else {
77
            unset($this->nicknames[$mask['nick']]);
78
        }
79
    }
80
81
    /**
82
     * @TODO DESCR
83
     */
84
    public function destroy()
85
    {
86
        unset($this->irc->channels[$this->name]);
87
    }
88
89
    /**
90
     * @TODO DESCR
91
     * @param string $type
92
     */
93
    public function setChanType($type)
94
    {
95
        $this->type = $type;
96
    }
97
98
    /**
99
     * @TODO DESCR
100
     * @return array
101
     */
102
    public function exportNicksArray()
103
    {
104
        $nicks = [];
105
        foreach ($this as $participant) {
106
            $nicks[] = $participant->flag . $participant->nick;
107
        }
108
        return $nicks;
109
    }
110
111
    /**
112
     * @TODO DESCR
113
     * @param string $msg
114
     */
115
    public function setTopic($msg)
116
    {
117
        $this->topic = $msg;
118
    }
119
120
    /**
121
     * @TODO DESCR
122
     * @param string $nick
123
     * @param string $mode
124
     */
125 View Code Duplication
    public function addMode($nick, $mode)
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...
126
    {
127
        if (!isset($this->nicknames[$nick])) {
128
            return;
129
        }
130
        $participant = $this->nicknames[$nick];
131
        if (mb_orig_strpos($participant->mode, $mode) === false) {
132
            $participant->mode .= $mode;
133
        }
134
        $participant->onModeUpdate();
135
    }
136
137
    /**
138
     * @TODO DESCR
139
     * @param string $target
140
     * @param string $mode
141
     */
142 View Code Duplication
    public function removeMode($target, $mode)
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...
143
    {
144
        if (!isset($this->nicknames[$target])) {
145
            return;
146
        }
147
        $participant = $this->nicknames[$target];
148
        $participant->mode = str_replace($mode, '', $participant->mode);
149
        $participant->onModeUpdate();
150
    }
151
152
    /**
153
     * @TODO DESCR
154
     */
155
    public function join()
156
    {
157
        $this->irc->join($this->name);
0 ignored issues
show
Documentation introduced by
$this->name is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
    }
159
160
    /**
161
     * @TODO DESCR
162
     * @param mixed $msg
163
     */
164
    public function part($msg = null)
165
    {
166
        $this->irc->part($this->name, $msg);
0 ignored issues
show
Documentation introduced by
$this->name is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
167
    }
168
169
    /**
170
     * @TODO DESCR
171
     * @param  string $type
172
     * @return $this
173
     */
174
    public function setType($type)
175
    {
176
        $this->type = $type;
177
        return $this;
178
    }
179
180
    /**
181
     * @TODO DESCR
182
     * @param object $obj
183
     */
184
    public function detach($obj)
185
    {
186
        parent::detach($obj);
187
        unset($this->nicknames[$obj->nick]);
188
    }
189
}
190