GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 93e474...c8a7da )
by masaru
11:11
created

TwoChanDriver::isError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Localdisk\Monar;
4
5
use GuzzleHttp\Cookie\CookieJar;
6
use GuzzleHttp\Cookie\SetCookie;
7
use Illuminate\Support\Collection;
8
use Localdisk\Monar\Exceptions\MonarException;
9
10
class TwoChanDriver extends AbstractDriver
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $encoding = 'Shift_JIS';
16
17
    /**
18
     * get threads.
19
     *
20
     * @return \Illuminate\Support\Collection
21
     * @throws MonarException
22
     * @throws \GuzzleHttp\Exception\GuzzleException
23
     */
24
    public function threads(): Collection
25
    {
26
        $body = $this->request('GET', $this->threadsUrl());
27
28
        return $this->parseThreadsCollection($body);
29
    }
30
31
    /**
32
     * get messages.
33
     *
34
     * @param int|null $start
35
     * @param int|null $end
36
     *
37
     * @return \Illuminate\Support\Collection
38
     * @throws MonarException
39
     * @throws \GuzzleHttp\Exception\GuzzleException
40
     */
41
    public function messages(?int $start = null, ?int $end = null): Collection
42
    {
43
        $body = $this->request('GET', $this->messagesUrl($start, $end));
44
45
        return $this->parseDatCollection($body, $start, $end);
46
    }
47
48
    /**
49
     * post message.
50
     *
51
     * @param string $name
52
     * @param string $email
53
     * @param string|null $text
54
     *
55
     * @return string
56
     * @throws MonarException
57
     * @throws \GuzzleHttp\Exception\GuzzleException
58
     */
59
    public function post(string $name = '', string $email = 'sage', ?string $text = null): string
60
    {
61
        mb_convert_variables('Shift_JIS', 'UTF-8', $name, $email, $text);
62
        $params = [
63
            'bbs' => $this->board,
64
            'key' => $this->thread,
65
            'time' => time(),
66
            'FROM' => $name,
67
            'mail' => $email,
68
            'MESSAGE' => $text,
69
            'submit' => $this->encode('書き込む', 'Shift_JIS', 'UTF-8'),
70
        ];
71
        $headers = [
72
            'Host' => parse_url($this->url, PHP_URL_HOST),
73
            'Referer' => $this->url,
74
            'User-Agent' => 'Monazilla/1.00',
75
        ];
76
77
        $cookie = new CookieJar();
78
        $response = $this->request('POST', $this->postUrl(), [
79
            'headers' => $headers,
80
            'form_params' => $params,
81
            'cookies' => $cookie,
82
        ]);
83
84
        if ($this->isError($response)) {
85
            throw new MonarException($response);
86
        }
87
88
        if ($this->confirm($response)) {
89
            $cookie->setCookie(SetCookie::fromString('IS_COOKIE=1'));
90
            $response = $this->request('POST', $this->postUrl(), [
91
                'headers' => $headers,
92
                'form_params' => $params,
93
                'cookies' => $cookie,
94
            ]);
95
        }
96
97
        return $response;
98
    }
99
100
    /**
101
     * parse url.
102
     *
103
     * @return void
104
     */
105
    protected function parse(): void
106
    {
107
        $parsed = parse_url($this->url);
108
        $paths = $this->renewArray(explode('/', parse_url($this->url, PHP_URL_PATH)));
109
110
        $this->baseUrl = $parsed['scheme'].'://'.$parsed['host'];
111
        $this->category = '';
112
113
        if (\count($paths) === 1) {
114
            $this->board = $paths[0];
115
            $this->thread = '';
116
        } else {
117
            $this->board = $paths[2];
118
            $this->thread = $paths[3];
119
        }
120
    }
121
122
    /**
123
     * parse dat collection.
124
     *
125
     * @param string $body
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    protected function parseDatCollection(string $body, ?int $start = null, ?int $end = null): Collection
130
    {
131
        $lines = array_filter(explode("\n", $body), '\strlen');
132
133
        $lineCount = count($lines);
134
135
        if (null === $start) {
136
            $start = 1;
137
        }
138
139
        if (null !== $end) {
140
            $end = $end - $start + 1;
141
        }
142
143
        if ($end > $lineCount) {
144
            $end = $lineCount;
145
        }
146
147
        $messages = array_slice($lines, $start - 1, $end);
148
149
        $collection = collect();
150
151
        $number = $start;
152
        foreach ($messages as $message) {
153
            [$name, $email, $date, $body] = explode('<>', $message);
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $email does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $date does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
154
            $name = trim(strip_tags($name));
155
            $body = strip_tags($body, '<br>');
156
            $resid = mb_substr($date, strpos($date, ' ID:') + 2);
157
            $date = mb_substr($date, 0, strpos($date, ' ID:') - 2);
158
159
            $collection->push(compact('number', 'name', 'email', 'date', 'body', 'resid'));
160
161
            $number++;
162
        }
163
164
        return $collection;
165
    }
166
167
    /**
168
     * parse threads collection.
169
     *
170
     * @param string $body
171
     *
172
     * @return \Illuminate\Support\Collection
173
     */
174
    protected function parseThreadsCollection(string $body): Collection
175
    {
176
        $threads = array_filter(explode("\n", $body), '\strlen');
177
178
        return collect(array_map(function ($elem) {
179
            [$id, $tmp] = explode('.dat<>', $elem);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $tmp does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
180
            preg_match('/^(.*)\((\d+)\)\z/', $tmp, $matches);
181
182
            return [
183
                'url' => vsprintf('http://%s/test/read.cgi/%s/%d', [
184
                    parse_url($this->url, PHP_URL_HOST),
185
                    $this->board,
186
                    $id,
187
                ]),
188
                'id' => $id,
189
                'title' => trim($matches[1]),
190
                'count' => $matches[2],
191
            ];
192
        }, $threads));
193
    }
194
195
    /**
196
     * build message url.
197
     *
198
     * @param int $start
199
     * @param int|null $end
200
     *
201
     * @return string
202
     */
203
    protected function messagesUrl(?int $start = null, ?int $end = null): string
204
    {
205
        return "{$this->baseUrl}/{$this->board}/dat/{$this->thread}.dat";
206
    }
207
208
    /**
209
     * build thread url.
210
     *
211
     * @return string
212
     */
213
    protected function threadsUrl(): string
214
    {
215
        return "{$this->baseUrl}/{$this->board}/subject.txt";
216
    }
217
218
    /**
219
     * build post url.
220
     *
221
     * @return string
222
     */
223
    protected function postUrl(): string
224
    {
225
        return "{$this->baseUrl}/test/bbs.cgi";
226
    }
227
228
    /**
229
     * 書き込み確認かどうか.
230
     *
231
     * @param  string $html
232
     *
233
     * @return bool
234
     */
235
    private function confirm(string $html): bool
236
    {
237
        return strpos($html, '書き込み確認') !== false;
238
    }
239
240
    /**
241
     * @param string $html
242
     *
243
     * @return bool
244
     */
245
    private function isError(string $html): bool
246
    {
247
        return strpos($html, '<!-- 2ch_X:error -->') !== false;
248
    }
249
}
250