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 ( 5b5d89...85d868 )
by masaru
02:21
created

ShitarabaDriver::confirm()   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 Illuminate\Support\Collection;
6
use Localdisk\Monar\Exceptions\MonarException;
7
8
class ShitarabaDriver extends AbstractDriver
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $baseUrl = 'http://jbbs.shitaraba.net';
14
15
    /**
16
     * @var string
17
     */
18
    protected $encoding = 'EUC-JP';
19
20
    /**
21
     * get threads.
22
     *
23
     * @return \Illuminate\Support\Collection
24
     * @throws MonarException
25
     */
26
    public function threads()
27
    {
28
        $body = $this->request('GET', $this->threadsUrl());
29
30
        return $this->parseThreadsCollection($body);
31
    }
32
33
    /**
34
     * get messages.
35
     *
36
     * @param int $start
37
     * @param int $end
38
     *
39
     * @return \Illuminate\Support\Collection
40
     * @throws MonarException
41
     * @throws \GuzzleHttp\Exception\GuzzleException
42
     */
43
    public function messages($start = null, $end = null): Collection
44
    {
45
        $body = $this->request('GET', $this->messagesUrl($start, $end));
46
47
        return $this->parseDatCollection($body);
48
    }
49
50
    /**
51
     * post message.
52
     *
53
     * @param string $name
54
     * @param string $email
55
     * @param null $text
56
     *
57
     * @return mixed|string
58
     * @throws MonarException
59
     * @throws \GuzzleHttp\Exception\GuzzleException
60
     */
61
    public function post($name = '', $email = 'sage', $text = null)
62
    {
63
        mb_convert_variables('EUC-JP', 'UTF-8', $name, $email, $text);
64
        $params = [
65
            'submit'  => $this->encode('書き込む', 'EUC-JP', 'UTF-8'),
66
            'DIR'     => $this->category,
67
            'BBS'     => $this->board,
68
            'KEY'     => $this->thread,
69
            'TIME'    => time(),
70
            'MESSAGE' => $text,
71
            'NAME'    => $name,
72
            'MAIL'    => $email,
73
        ];
74
        $bytes = 0;
75
        foreach ($params as $param) {
76
            $bytes += \strlen($param);
77
        }
78
        $headers = [
79
            'Host'           => parse_url($this->url, PHP_URL_HOST),
80
            'Referer'        => $this->url,
81
            'Content-Length' => $bytes,
82
            'User-Agent' => 'Monazilla/1.00',
83
        ];
84
85
        $response = $this->request('POST', $this->postUrl(), [
86
            'headers'     => $headers,
87
            'form_params' => $params,
88
        ]);
89
90
        if ($this->isError($response)) {
91
            throw new MonarException($response);
92
        }
93
94
        return $response;
95
    }
96
97
    /**
98
     * parse url.
99
     *
100
     * @return void
101
     */
102
    protected function parse(): void
103
    {
104
        $paths = $this->renewArray(explode('/', parse_url($this->url, PHP_URL_PATH)));
105
        if ($paths[1] === 'read.cgi' || $paths[1] === 'read_archive.cgi') {
106
            $this->category = $paths[2];
107
            $this->board = $paths[3];
108
            $this->thread = $paths[4];
109
        } else {
110
            $this->category = $paths[0];
111
            $this->board = $paths[1];
112
        }
113
    }
114
115
    /**
116
     * parse dat collection.
117
     *
118
     * @param string $body
119
     *
120
     * @return \Illuminate\Support\Collection
121
     */
122
    protected function parseDatCollection($body): Collection
123
    {
124
        $lines = array_filter(explode("\n", $body), '\strlen');
125
126
        return collect(array_map(function ($line) {
127
            [$number, $name, $email, $date, $body, , $resid] = explode('<>', $line);
0 ignored issues
show
Bug introduced by
The variable $number 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 $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

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 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 $body seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $resid 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...
128
            $name = trim(strip_tags($name));
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
129
            $body = strip_tags($body, '<br>');
0 ignored issues
show
Bug introduced by
The variable $body seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
130
131
            return compact('number', 'name', 'email', 'date', 'body', 'resid');
132
        }, $lines));
133
    }
134
135
    /**
136
     * parse threads collection.
137
     *
138
     * @param $body
139
     *
140
     * @return \Illuminate\Support\Collection
141
     */
142
    protected function parseThreadsCollection($body): Collection
143
    {
144
        $threads = array_filter(explode("\n", $body), '\strlen');
145
146
        return collect(array_map(function ($elem) {
147
            [$id, $tmp] = explode('.cgi,', $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...
148
            preg_match('/^(.*)\((\d+)\)\z/', $tmp, $matches);
149
150
            return [
151
                'url'   => vsprintf('http://%s/bbs/read.cgi/%s/%s/%d', [
152
                    parse_url($this->url, PHP_URL_HOST),
153
                    $this->category,
154
                    $this->board,
155
                    $id,
156
                ]),
157
                'id'    => $id,
158
                'title' => trim($matches[1]),
159
                'count' => $matches[2],
160
            ];
161
        }, $threads));
162
    }
163
164
    /**
165
     * build message url.
166
     *
167
     * @param int $start
168
     * @param int| null $end
169
     *
170
     * @return string
171
     */
172
    protected function messagesUrl($start = 1, $end = null): string
173
    {
174
        $url = "{$this->baseUrl}/bbs/rawmode.cgi/{$this->category}/{$this->board}/{$this->thread}/";
175
        if (null !== $start && null !== $end) {
176
            return $url."{$start}-{$end}";
177
        }
178
        if (null !== $start && null === $end) {
179
            return $url."{$start}-";
180
        }
181
        if (null === $start && null !== $end) {
182
            return $url."-{$end}";
183
        }
184
185
        return $url;
186
    }
187
188
    /**
189
     * build thread url.
190
     *
191
     * @return string
192
     */
193
    protected function threadsUrl(): string
194
    {
195
        return "{$this->baseUrl}/{$this->category}/{$this->board}/subject.txt";
196
    }
197
198
    /**
199
     * build post url.
200
     *
201
     * @return string
202
     */
203
    protected function postUrl(): string
204
    {
205
        return "{$this->baseUrl}/bbs/write.cgi";
206
    }
207
208
    /**
209
     * 書き込み確認かどうか.
210
     *
211
     * @param  string $html
212
     *
213
     * @return bool
214
     */
215
    private function confirm($html): bool
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
216
    {
217
        return strpos($html, '書き込み確認') !== false;
218
    }
219
220
    /**
221
     * @param $html
222
     *
223
     * @return bool
224
     */
225
    private function isError($html): bool
226
    {
227
        return strpos($html, '<!-- 2ch_X:error -->') !== false;
228
    }
229
}
230