Passed
Push — 1.x ( 7bfd3e...73990a )
by Ulises Jeremias
02:55
created

Builder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection\Traits\Sequenceable;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use SplFixedArray;
12
use Traversable;
13
14
trait Builder
15
{
16
    protected $sfa = null;
17
18
    /**
19
     * Create an fixed array
20
     *
21
     * @param Traversable $array data
22
     */
23
    protected function __construct(Traversable $array)
24
    {
25
        $this->sfa = $array;
26
        $this->checkCapacity();
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public static function fromArray(array $array)
33
    {
34
        return new static(SplFixedArray::fromArray($array));
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public static function fromItems(Traversable $array)
41
    {
42
        if (!$array instanceof Countable) {
43
            return static::fromArray(iterator_to_array($array));
44
        }
45
46
        $sfa = new SplFixedArray(count($array));
0 ignored issues
show
Bug introduced by
$array of type Traversable&Mbh\Collecti...\Sequenceable\Countable is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $sfa = new SplFixedArray(count(/** @scrutinizer ignore-type */ $array));
Loading history...
47
48
        foreach ($array as $i => $elem) {
49
            $sfa[$i] = $elem;
50
        }
51
52
        return new static($sfa);
53
    }
54
55
    abstract protected function checkCapacity();
56
}
57