Pool   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 6 1
A reset() 0 10 2
A has() 0 8 2
A exists() 0 4 1
A elem() 0 7 2
cast() 0 1 ?
A add() 0 4 1
A count() 0 4 1
1
<?php
2
3
namespace FForattini\Btrieve\Persistence;
4
5
use Exception;
6
7
abstract class Pool implements DatasetInterface
8
{
9
    protected $dataset;
10
11
    /**
12
     * Contructor.
13
     */
14
    public function __construct()
15
    {
16
        $this->init();
17
    }
18
19
    /**
20
     * Initializes a pool of elements.
21
     *
22
     * @return Pool
23
     */
24
    public function init()
25
    {
26
        $this->dataset = [];
27
28
        return $this;
29
    }
30
31
    /**
32
     * Erase all information from dataset.
33
     *
34
     * @return Pool
35
     */
36
    public function reset()
37
    {
38
        foreach ($this->dataset as $index => $element) {
39
            unset($this->dataset[$index]);
40
        }
41
        unset($this->dataset);
42
        $this->initDataset();
0 ignored issues
show
Bug introduced by
The method initDataset() does not seem to exist on object<FForattini\Btrieve\Persistence\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        return $this;
45
    }
46
47
    /**
48
     * Checks if this element exists in this dataset.
49
     *
50
     * @param int $index
51
     *
52
     * @return bool
53
     */
54
    public function has($index)
55
    {
56
        if (isset($this->dataset[$index])) {
57
            return true;
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * @aliasof has
65
     *
66
     * @param int $index
67
     *
68
     * @return bool
69
     */
70
    public function exists($index)
71
    {
72
        return $this->has($index);
73
    }
74
75
    /**
76
     * Returns the element in this index from the dataset.
77
     *
78
     * @param int $index
79
     *
80
     * @return mixed
81
     */
82
    public function elem($index)
83
    {
84
        if ($this->has($index)) {
85
            return $this->dataset[$index];
86
        }
87
        throw new Exception('Element doesnt exist in this dataset.');
88
    }
89
90
    /**
91
     * Function will show how to deal with the element.
92
     *
93
     * @param array $element
94
     *
95
     * @return mixed
96
     */
97
    abstract public function cast($element);
98
99
    /**
100
     * Adds an element to the dataset.
101
     *
102
     * @param array $element
103
     */
104
    public function add($element)
105
    {
106
        $this->dataset[] = $this->cast($element);
107
    }
108
109
    /**
110
     * Return number of elements.
111
     *
112
     * @return int
113
     */
114
    public function count()
115
    {
116
        return count($this->dataset);
117
    }
118
}
119