Completed
Pull Request — master (#4)
by
unknown
01:37
created

Volumes::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
use Scriptotek\GoogleBooks\LibraryBuilder;
6
7
class Volumes
8
{
9
    protected $client;
10
    protected $chunk = false;
11
12
    public function __construct(GoogleBooks $client)
13
    {
14
        $this->client = $client;
15
    }
16
17
    /**
18
     * @param $query
19
     * @return \Generator|Volume || array
20
     */
21
    public function search($query)
22
    {
23
        return (new LibraryBuilder($this->client))->search($query);
24
    }
25
26
    /**
27
     * @param $query
28
     * @return \Generator|Volume
29
     */
30
    public function fetchSearch($query)
31
    {
32
        $return = [];
33
        foreach ($this->client->listItems('volumes', ['q' => $query]) as $item) {
0 ignored issues
show
Bug introduced by
The expression $this->client->listItems..., array('q' => $query)) of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
34
            $return[] = new Volume($this->client, $item);
35
        }
36
37
        return $return;
38
    }
39
40
    public function firstOrNull($query)
41
    {
42
        return $this->search($query)->first();
43
    }
44
45
    public function find($id)
46
    {
47
        return (new LibraryBuilder($this->client))->find($id);
48
    }
49
50
    public function byIsbn($isbn)
51
    {
52
        $isbn = preg_replace('/[^0-9Xx]/', '', $isbn);
53
        return $this->search('isbn:' . $isbn)->first();
54
    }
55
}
56