Completed
Push — master ( 0bfbf7...50b91b )
by Jonathan
03:57
created

CursorSubset   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 13
cts 22
cp 0.5909
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getTotal() 0 4 1
B toArray() 0 14 5
A jsonSerialize() 0 5 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Db;
22
23
/**
24
 * A wrapper around a `Traversable` that includes the total count of the superset.
25
 */
26
class CursorSubset extends \IteratorIterator implements \JsonSerializable
27
{
28
    /**
29
     * @var int
30
     */
31
    private $total;
32
33
    /**
34
     * Create a new CursorSubset.
35
     *
36
     * @param \Traversable $iterable The traversable to wrap
37
     * @param int $total The total number of items in the superset
38
     * @throws \RangeException if the total is negative
39
     */
40 4
    public function __construct(\Traversable $iterable, int $total)
41
    {
42 4
        parent::__construct($iterable);
43 4
        if ($total < 0) {
44 1
            throw new \RangeException("Total cannot be a negative number");
45
        }
46 3
        $this->total = $total;
47 3
    }
48
49
    /**
50
     * Gets the superset total.
51
     *
52
     * @return - The total number of items in the superset (never negative).
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54 3
    public function getTotal(): int
55
    {
56 3
        return $this->total;
57
    }
58
59
    /**
60
     * Return data which can be serialized with json_encode.
61
     */
62
    public function jsonSerialize()
63
    {
64
        $it = $this->getInnerIterator();
65
        return $it instanceof \JsonSerializable ? $it : $this->toArray();
66
    }
67
68
    /**
69
     * Converts this thing into an array.
70
     *
71
     * @return array<mixed> The array version of this thing
72
     */
73 3
    public function toArray(): array
74
    {
75 3
        $it = $this->getInnerIterator();
76 3
        if ($it instanceof \MongoDB\Driver\Cursor) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Cursor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
            return $it->toArray();
78
        } elseif ($it instanceof Iterable) {
0 ignored issues
show
Bug introduced by
The class Minotaur\Db\iterable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79
            return $it->toArray();
80
        } elseif ($it instanceof \ArrayObject) {
81
            return $it->getArrayCopy();
82
        } elseif ($it instanceof \ArrayIterator) {
83 2
            return $it->getArrayCopy();
84
        }
85 1
        return iterator_to_array($it, false);
86
    }
87
}
88