Completed
Push — v2 ( 68097e...ed5395 )
by Beñat
03:15
created

AnswerApi::addOfQuestionId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 4
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\Answer\AcceptAnswer;
17
use BenatEspina\StackExchangeApiClient\Api\Answer\Answers;
18
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersByIds;
19
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersOnQuestions;
20
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersOnUsers;
21
use BenatEspina\StackExchangeApiClient\Api\Answer\CreateAnswer;
22
use BenatEspina\StackExchangeApiClient\Api\Answer\CreateAnswerFlag;
23
use BenatEspina\StackExchangeApiClient\Api\Answer\DeleteAnswer;
24
use BenatEspina\StackExchangeApiClient\Api\Answer\MeAnswers;
25
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
26
use BenatEspina\StackExchangeApiClient\Authentication\AuthenticationIsRequired;
27
use BenatEspina\StackExchangeApiClient\Http\HttpClient;
28
use BenatEspina\StackExchangeApiClient\Serializer\Serializer;
29
30
/**
31
 * @author Beñat Espiña <[email protected]>
32
 */
33
class AnswerApi
34
{
35
    public const QUERY_PARAMS = [
36
        'order'  => 'desc',
37
        'sort'   => 'activity',
38
        'site'   => 'stackoverflow',
39
        'filter' => HttpClient::FILTER_ALL,
40
    ];
41
42
    private $client;
43
    private $serializer;
44
    private $authentication;
45
46
    public function __construct(HttpClient $client, Serializer $serializer, Authentication $authentication = null)
47
    {
48
        $this->client = $client;
49
        $this->serializer = $serializer;
50
        $this->authentication = $authentication;
51
    }
52
53
    public function acceptAnswer(string $id, 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...
54
    {
55
        $this->checkAuthenticationIsEnabled();
56
57
        return (new AcceptAnswer(
58
            $this->client,
59
            $this->serializer,
60
            $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...
61
        )->__invoke($id, $parameters);
62
    }
63
64
    public function answers(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...
65
    {
66
        return (new Answers(
67
            $this->client,
68
            $this->serializer
69
        ))->__invoke($parameters);
70
    }
71
72
    public function answersByIds($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...
73
    {
74
        return (new AnswersByIds(
75
            $this->client,
76
            $this->serializer
77
        ))->__invoke($ids, $parameters);
78
    }
79
80
    public function answersOnQuestions($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...
81
    {
82
        return (new AnswersOnQuestions(
83
            $this->client,
84
            $this->serializer
85
        ))->__invoke($ids, $parameters);
86
    }
87
88
    public function answersOnUsers($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...
89
    {
90
        return (new AnswersOnUsers(
91
            $this->client,
92
            $this->serializer
93
        ))->__invoke($ids, $parameters);
94
    }
95
96
    public function createAnswer(string $questionId, string $body, 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...
97
    {
98
        $this->checkAuthenticationIsEnabled();
99
100
        return (new CreateAnswer(
101
            $this->client,
102
            $this->serializer,
103
            $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...
104
        ))->__invoke($questionId, $body, $parameters);
105
    }
106
107
    public function createAnswerFlag(string $id, 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...
108
    {
109
        $this->checkAuthenticationIsEnabled();
110
111
        return (new CreateAnswerFlag(
112
            $this->client,
113
            $this->serializer,
114
            $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...
115
        ))->__invoke($id, $parameters);
116
    }
117
118
    public function deleteAnswer(string $id, 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...
119
    {
120
        $this->checkAuthenticationIsEnabled();
121
122
        return (new DeleteAnswer(
123
            $this->client,
124
            $this->serializer,
125
            $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...
126
        ))->__invoke($id, $parameters);
127
    }
128
129
    public function meAnswers(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...
130
    {
131
        $this->checkAuthenticationIsEnabled();
132
133
        return (new MeAnswers(
134
            $this->client,
135
            $this->serializer,
136
            $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...
137
        ))->__invoke($parameters);
138
    }
139
140
    private function checkAuthenticationIsEnabled() : void
141
    {
142
        if (!$this->authentication instanceof Authentication) {
143
            throw new AuthenticationIsRequired();
144
        }
145
    }
146
}
147