AbstractCollection::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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