Users   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get() 0 4 1
A findById() 0 12 1
A query() 0 14 4
1
<?php
2
3
namespace Shrikeh\PagerDuty\Repository\Users;
4
5
use Shrikeh\PagerDuty\Client;
6
use Shrikeh\PagerDuty\Repository\Users as UsersRepositoryInterface;
7
use Shrikeh\PagerDuty\Collection\Users as UserCollection;
8
use GuzzleHttp\Psr7\Uri;
9
10
use Shrikeh\PagerDuty\Hydrator;
11
12
final class Users implements UsersRepositoryInterface
13
{
14
    private $client;
15
16
    private $hydrator;
17
18
    public function __construct(
19
        Client $client,
20
        Hydrator $hydrator
21
    ) {
22
        $this->client   = $client;
23
        $this->hydrator = $hydrator;
24
    }
25
26
    public function get()
27
    {
28
29
    }
30
31
    public function findById($id, array $extras = array())
32
    {
33
      $response = $this->client->request(
34
          'GET',
35
          sprintf('%s/%s', static::ENDPOINT, $id),
36
          ['query' => $this->query($extras)]
37
      );
38
39
      $dto = json_decode($response->getBody());
40
      $users[] = $this->hydrator->hydrate($dto->user);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$users was never initialized. Although not strictly required by PHP, it is generally a good practice to add $users = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
41
      return UserCollection::fromArray($users);
42
    }
43
44
    private function query(array $extras = array())
45
    {
46
        $query = [];
47
        foreach ($extras as $extra) {
48
            $query[] = ['include[]' => $extra];
49
        }
50
        $newQuery = [];
51
        foreach ($query as $part) {
52
          foreach ($part as $key => $value) {
53
              $newQuery[] = "$key=$value";
54
          }
55
        }
56
        return implode('&', $newQuery);
57
    }
58
}
59