Completed
Push — master ( 74c577...c1ce98 )
by Kirill
06:10
created

Fetch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getIterator() 0 15 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * This file is part of Ai package.
5
 *
6
 * @author Serafim <[email protected]>
7
 * @date 28.03.2016 14:02
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
14
namespace Core\Lazy;
15
16
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Database\Eloquent\Builder;
18
19
/**
20
 * Class Fetch
21
 * @package Core\Lazy
22
 */
23
class Fetch implements \IteratorAggregate
24
{
25
    const DEFAULT_CHUNK_SIZE = 1000;
26
27
    /**
28
     * @var int
29
     */
30
    private $chunk = self::DEFAULT_CHUNK_SIZE;
31
32
    /**
33
     * @var Builder
34
     */
35
    private $builder;
36
37
    /**
38
     * Fetch constructor.
39
     * @param Builder $builder
40
     * @param int $chunk
41
     */
42
    public function __construct(Builder $builder, $chunk = self::DEFAULT_CHUNK_SIZE)
43
    {
44
        $this->builder = $builder;
45
        $this->chunk = $chunk;
46
    }
47
48
    /**
49
     * @return \Generator
50
     */
51
    public function getIterator()
52
    {
53
        /** @var Builder|\Illuminate\Database\Query\Builder|Model $query */
54
        $query = clone $this->builder;
55
56
        $skip  = 0;
57
        $count = $query->count();
0 ignored issues
show
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
        do {
59
            $items = $query->skip($skip)->take($this->chunk)->get();
0 ignored issues
show
Bug introduced by
The method skip does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
60
            foreach ($items as $item) {
61
                yield $item;
62
            }
63
            $skip += $this->chunk;
64
        } while ($skip < $count + $this->chunk);
65
    }
66
}
67