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

Volumes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 48
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A search() 0 4 1
A fetchSearch() 0 9 2
A firstOrNull() 0 4 1
A find() 0 4 1
A byIsbn() 0 5 1
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
use Scriptotek\GoogleBooks\LibraryBuilder;
6
7
class Volumes
8
{
9
    protected $client;
10
11
    public function __construct(GoogleBooks $client)
12
    {
13
        $this->client = $client;
14
    }
15
16
    /**
17
     * @param $query
18
     * @return stdclass | array
19
     */
20
    public function search($query)
21
    {
22
        return (new LibraryBuilder($this->client))->search($query);
23
    }
24
25
    /**
26
     * @param $query
27
     * @return array
28
     */
29
    public function fetchSearch($query)
30
    {
31
        $return = [];
32
        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...
33
            $return[] = new Volume($this->client, $item);
34
        }
35
36
        return $return;
37
    }
38
39
    public function firstOrNull($query)
40
    {
41
        return $this->search($query)->first();
42
    }
43
44
    public function find($id)
45
    {
46
        return (new LibraryBuilder($this->client))->find($id);
47
    }
48
49
    public function byIsbn($isbn)
50
    {
51
        $isbn = preg_replace('/[^0-9Xx]/', '', $isbn);
52
        return $this->search('isbn:' . $isbn)->first();
53
    }
54
}
55