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.
Test Failed
Push — master ( 4dafe1...276f10 )
by masaru
02:11
created

TwoChanDriver::parseDatCollection()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 2
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, $end);
46
    }
47
48
    /**
49
     * post message.
50
     *
51
     * @param string $name
52
     * @param string $email
53
     * @param string|null $text
54
     *
55
     * @return mixed|string
56
     * @throws MonarException
57
     * @throws \GuzzleHttp\Exception\GuzzleException
58
     */
59
    public function post(string $name = '', string $email = 'sage', ?string $text = null)
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
        $cookie = new CookieJar();
77
        $response = $this->request('POST', $this->postUrl(), [
78
            'headers' => $headers,
79
            'form_params' => $params,
80
            'cookies' => $cookie,
81
        ]);
82
83
        if ($this->confirm($response)) {
84
            $cookie->setCookie(SetCookie::fromString('IS_COOKIE=1'));
85
            $response = $this->request('POST', $this->postUrl(), [
86
                'headers' => $headers,
87
                'form_params' => $params,
88
                'cookies' => $cookie,
89
            ]);
90
        }
91
92
        return $response;
93
    }
94
95
    /**
96
     * parse url.
97
     *
98
     * @return void
99
     */
100
    protected function parse(): void
101
    {
102
        $parsed = parse_url($this->url);
103
        $paths = $this->renewArray(explode('/', parse_url($this->url, PHP_URL_PATH)));
104
105
        $this->baseUrl = $parsed['scheme'].'://'.$parsed['host'];
106
        $this->category = '';
107
108
        if (\count($paths) === 1) {
109
            $this->board = $paths[0];
110
            $this->thread = '';
111
        } else {
112
            $this->board = $paths[2];
113
            $this->thread = $paths[3];
114
        }
115
    }
116
117
    /**
118
     * parse dat collection.
119
     *
120
     * @param string $body
121
     *
122
     * @return \Illuminate\Support\Collection
123
     */
124
    protected function parseDatCollection(string $body, ?int $end = null): Collection
125
    {
126
        $lines = array_filter(explode("\n", $body), '\strlen');
127
        $number = 0;
0 ignored issues
show
Unused Code introduced by
$number is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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