Completed
Pull Request — master (#3)
by
unknown
01:27
created

Volumes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
6
class Volumes
7
{
8
    protected $client;
9
    protected $chunk = false;
10
11
    public function __construct(GoogleBooks $client)
12
    {
13
        $this->client = $client;
14
    }
15
16
    /**
17
     * @param $query
18
     * @return \Generator|Volume || array
19
     */
20
    public function search($query)
21
    {
22
        if ($this->chunk) {
23
            return $this->client->chunk($this->fetchSearch($query), $this->chunk);
0 ignored issues
show
Documentation introduced by
$this->chunk is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
        }
25
        return $this->fetchSearch($query);
26
    }
27
28
    /**
29
     * @param $query
30
     * @return \Generator|Volume
31
     */
32
    private function fetchSearch($query)
33
    {
34
        foreach ($this->client->listItems('volumes', ['q' => $query]) as $item) {
35
            yield new Volume($this->client, $item);
36
        }
37
    }
38
39
    /**
40
     * @param $chunk
41
     * @return Volumes
42
     */
43
    public function chunk(int $chunk)
44
    {
45
        $this->chunk = $chunk;
0 ignored issues
show
Documentation Bug introduced by
The property $chunk was declared of type boolean, but $chunk is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
        return $this;
47
    }
48
49
    public function firstOrNull($query)
50
    {
51
        $res = $this->search($query)->current();
52
        return $res;
53
    }
54
55
    public function get($id)
56
    {
57
        return new Volume($this->client, $this->client->getItem("volumes/$id"), true);
58
    }
59
60
    public function byIsbn($isbn)
61
    {
62
        $isbn = preg_replace('/[^0-9Xx]/', '', $isbn);
63
64
        return $this->firstOrNull('isbn:' . $isbn);
65
    }
66
}
67