ArrayAccessTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 32
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
get() 0 1 ?
delete() 0 1 ?
A offsetSet() 0 11 3
A offsetExists() 0 7 2
A offsetUnset() 0 3 1
A offsetGet() 0 3 1
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists\Traits;
10
11
use OutOfBoundsException;
12
13
/**
14
 * ArrayAccessTrait
15
 *
16
 * ArrayAccessTrait is a trait that implements the ArrayAccess methods
17
 * to avoid repeating code in the List hierarchy classes.
18
 *
19
 * @author Siro Diaz Palazon <[email protected]>
20
 */
21
trait ArrayAccessTrait {
22
    abstract public function get($index);
23
    abstract public function delete($index);
24
    
25
    public function offsetSet($offset, $value) {
26
        if (is_null($offset)) {
27
            $offset = $this->size;
28
            if($this->size === 0) {
29
                $offset = 0;
30
            }
31
            $this->insert($offset, $value);
32
        } else {
33
            $this->insert($offset, $value);
34
        }
35
    }
36
    
37
    public function offsetExists($offset) {
38
        try {
39
            return $this->get($offset);
40
        } catch(OutOfBoundsException $e) {
41
            return false;
42
        }
43
    }
44
45
    public function offsetUnset($offset) {
46
        $this->delete($offset);
47
    }
48
49
    public function offsetGet($offset) {
50
        return $this->get($offset);
51
    }
52
}