Completed
Push — master ( d6f0bd...41a21b )
by Ivannis Suárez
05:43
created

DataSourceCollectionTrait::slice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Collections;
12
13
use Cubiche\Core\Collections\DataSource\DataSourceInterface;
14
use Cubiche\Core\Specification\SpecificationInterface;
15
16
/**
17
 * DataSourceCollection Trait.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
trait DataSourceCollectionTrait
23
{
24
    /**
25
     * @var DataSourceInterface
26
     */
27
    protected $dataSource;
28
29
    /**
30
     * @param DataSourceInterface $dataSource
31
     */
32
    public function __construct(DataSourceInterface $dataSource)
33
    {
34
        $this->dataSource = $dataSource;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function count()
41
    {
42
        if ($this->isInitialized()) {
0 ignored issues
show
Bug introduced by
It seems like isInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
            return parent::count();
44
        }
45
46
        return $this->dataSource->count();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getIterator()
53
    {
54
        if ($this->isInitialized()) {
0 ignored issues
show
Bug introduced by
It seems like isInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
            return parent::getIterator();
56
        }
57
58
        return $this->dataSource->getIterator();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function slice($offset, $length = null)
65
    {
66
        if ($this->isInitialized()) {
0 ignored issues
show
Bug introduced by
It seems like isInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
            return parent::slice($offset, $length);
68
        }
69
70
        return new self($this->dataSource->slicedDataSource($offset, $length));
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function find(SpecificationInterface $criteria)
77
    {
78
        if ($this->isInitialized()) {
0 ignored issues
show
Bug introduced by
It seems like isInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79
            return parent::find($criteria);
80
        }
81
82
        return new self($this->dataSource->filteredDataSource($criteria));
83
    }
84
}
85