BuilderResultset   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initialize() 0 15 2
A getPrototype() 0 4 1
A current() 0 10 2
1
<?php
2
3
namespace NwLaravel\Resultset;
4
5
use RuntimeException;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
8
/**
9
 * Class BuilderResultset
10
 */
11
class BuilderResultset extends AbstractResultset
12
{
13
    /**
14
     * @var \Illuminate\Database\Eloquent\Builder
15
     */
16
    protected $builder = null;
17
18
    /**
19
     * @var \Illuminate\Database\Eloquent\Model
20
     */
21
    private $prototype = null;
22
23
    /**
24
     * Initialize
25
     *
26
     * @param \Illuminate\Database\Eloquent\Builder $builder Builder
27
     */
28 4
    public function __construct(EloquentBuilder $builder)
29
    {
30 4
        $this->builder = $builder;
31
32 4
        $this->initialize();
33 4
    }
34
35
    /**
36
     * Initialize
37
     *
38
     * @return void
39
     */
40 4
    public function initialize()
41
    {
42 4
        if (! $this->initialized) {
43 4
            $query = $this->builder->toBase();
44 4
            $sql = $query->toSql();
45 4
            $bindings = $query->getBindings();
46 4
            $conn = $query->getConnection();
47 4
            $this->statement = $conn->getPdo()->prepare($sql);
48 4
            $this->statement->execute($conn->prepareBindings($bindings));
49
50 4
            $this->prototype = $this->builder->getModel()->newFromBuilder();
0 ignored issues
show
Bug introduced by
The method newFromBuilder does only exist in Illuminate\Database\Eloquent\Model, 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...
51 4
        }
52
53 4
        $this->initialized = true;
54 4
    }
55
56
    /**
57
     * Get Prototype
58
     *
59
     * @return \Illuminate\Database\Eloquent\Model
60
     */
61 1
    public function getPrototype()
62
    {
63 1
        return $this->prototype;
64
    }
65
66
    /**
67
     * Get the data
68
     *
69
     * @return object|bool
70
     */
71 2
    public function current()
72
    {
73 2
        if (!$data = parent::current()) {
74 1
            return false;
75
        }
76
77 1
        $newInstance = clone $this->getPrototype();
78 1
        $newInstance->setRawAttributes((array) $data, true);
79 1
        return $newInstance;
80
    }
81
}
82