AbstractCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 6 1
A contains() 0 4 1
A offsetSet() 11 11 2
A remove() 0 10 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
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection;
16
17
use Ramsey\Collection\Tool\TypeTrait;
18
use Ramsey\Collection\Tool\ValueToStringTrait;
19
20
/**
21
 * This class provides an implementation of the CollectionInterface, to
22
 * minimize the effort required to implement this interface
23
 */
24
abstract class AbstractCollection extends AbstractArray implements CollectionInterface
25
{
26
    use TypeTrait;
27
    use ValueToStringTrait;
28
29
    public function add($element)
30
    {
31
        $this[] = $element;
32
33
        return true;
34
    }
35
36
    public function contains($element, $strict = true)
37
    {
38
        return in_array($element, $this->data, $strict);
39
    }
40
41 View Code Duplication
    public function offsetSet($offset, $value)
42
    {
43
        if ($this->checkType($this->getType(), $value) === false) {
44
            throw new \InvalidArgumentException(
45
                'Value must be of type ' . $this->getType() . '; value is '
46
                . $this->toolValueToString($value)
47
            );
48
        }
49
50
        $this->data[] = $value;
51
    }
52
53
    public function remove($element)
54
    {
55
        if (($position = array_search($element, $this->data, true)) !== false) {
56
            unset($this->data[$position]);
57
58
            return true;
59
        }
60
61
        return false;
62
    }
63
}
64