Completed
Push — master ( aff56e...1ae453 )
by Beñat
03:31
created

UserApi::usersByIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Api;
15
16
use BenatEspina\StackExchangeApiClient\Api\User\ElectedModerators;
17
use BenatEspina\StackExchangeApiClient\Api\User\Me;
18
use BenatEspina\StackExchangeApiClient\Api\User\Moderators;
19
use BenatEspina\StackExchangeApiClient\Api\User\Users;
20
use BenatEspina\StackExchangeApiClient\Api\User\UsersByIds;
21
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
22
use BenatEspina\StackExchangeApiClient\Authentication\AuthenticationIsRequired;
23
use BenatEspina\StackExchangeApiClient\Http\HttpClient;
24
use BenatEspina\StackExchangeApiClient\Serializer\Serializer;
25
26
/**
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
final class UserApi
30
{
31
    public const QUERY_PARAMS = [
32
        'order'  => 'desc',
33
        'sort'   => 'reputation',
34
        'site'   => 'stackoverflow',
35
        'filter' => HttpClient::FILTER_ALL,
36
    ];
37
38
    private $client;
39
    private $serializer;
40
    private $authentication;
41
42
    public function __construct(HttpClient $client, Serializer $serializer, Authentication $authentication = null)
43
    {
44
        $this->client = $client;
45
        $this->serializer = $serializer;
46
        $this->authentication = $authentication;
47
    }
48
49
    public function electedModerators(array $parameters = self::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        return (new ElectedModerators(
52
            $this->client,
53
            $this->serializer
54
        ))->__invoke($parameters);
55
    }
56
57
    public function me(array $parameters = self::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58
    {
59
        $this->checkAuthenticationIsEnabled();
60
61
        return (new Me(
62
            $this->client,
63
            $this->serializer,
64
            $this->authentication
0 ignored issues
show
Bug introduced by
It seems like $this->authentication can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
65
        ))->__invoke($parameters);
66
    }
67
68
    public function moderators(array $parameters = self::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
69
    {
70
        return (new Moderators(
71
            $this->client,
72
            $this->serializer
73
        ))->__invoke($parameters);
74
    }
75
76
    public function users(array $parameters = self::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
    {
78
        return (new Users(
79
            $this->client,
80
            $this->serializer
81
        ))->__invoke($parameters);
82
    }
83
84
    public function usersByIds($ids, array $parameters = self::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
85
    {
86
        return (new UsersByIds(
87
            $this->client,
88
            $this->serializer
89
        ))->__invoke($ids, $parameters);
90
    }
91
92
    private function checkAuthenticationIsEnabled() : void
93
    {
94
        if (!$this->authentication instanceof Authentication) {
95
            throw new AuthenticationIsRequired();
96
        }
97
    }
98
}
99