Twitter::update()   B
last analyzed

Complexity

Conditions 11
Paths 30

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 30
nop 1
dl 0
loc 48
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * Twitter.class.php - update the status of a twitter user
5
 * Author: Felix Oghina
6
 */
7
8
if (class_exists('Twitter')) {
9
    return true;
10
}
11
12
class Twitter
13
{
14
15
    private $auth  = false;
16
    private $debug = false;
17
    public  $error = false;
18
19
    function __construct($user, $pass, $debug = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        // Store an auth key for the HTTP Authorization: header
22
        $this->auth  = base64_encode($user . ':' . $pass);
0 ignored issues
show
Documentation Bug introduced by
The property $auth was declared of type boolean, but base64_encode($user . ':' . $pass) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
23
        $this->debug = $debug;
24
    }
25
26
    function update($new_status)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        if (strlen($new_status) > 140) {
29
            $this->error = "Status too long: {$new_status}.";
30
            return false;
31
        }
32
        $fp = @fsockopen('twitter.com', 80, $errno, $errstr);
33
        if (!$fp) {
34
            $this->error = "Socket error #{$errno}: {$errstr}";
35
            return false;
36
        }
37
        $post_data = 'status=' . urlencode($new_status);
38
        $to_send   = "POST /statuses/update.xml HTTP/1.1\r\n";
39
        $to_send .= "Host: twitter.com\r\n";
40
        $to_send .= 'Content-Length: ' . strlen($post_data) . "\r\n";
41
        $to_send .= "Authorization: Basic {$this->auth}\r\n\r\n";
42
        $to_send .= $post_data . "\r\n\r\n";
43
        $bytes = fwrite($fp, $to_send);
44
        if ($bytes === false) {
45
            $this->error = 'Socket error: Error sending data.';
0 ignored issues
show
Documentation Bug introduced by
The property $error was declared of type boolean, but 'Socket error: Error sending data.' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
            return false;
47
        } elseif ($bytes < strlen($to_send)) {
48
            $this->error = 'Socket error: Could not send all data.';
0 ignored issues
show
Documentation Bug introduced by
The property $error was declared of type boolean, but 'Socket error: Could not send all data.' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
49
            return false;
50
        }
51
        if ($this->debug) echo "Sent:\n{$to_send}\n\n";
52
        $response = '';
53
        while (!feof($fp)) {
54
            $buf = fread($fp, 1024);
55
            if ($buf === false) {
56
                $this->error = 'Socket error: Error reading data.';
0 ignored issues
show
Documentation Bug introduced by
The property $error was declared of type boolean, but 'Socket error: Error reading data.' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
57
                return false;
58
            }
59
            $response .= $buf;
60
        }
61
        if ($this->debug) echo "Received:\n{$response}";
62
        $was_error = preg_match('#' . preg_quote('<error>') . '(.+)' . preg_quote('</error>') . '#i', $response, $matches);
63
        if ($was_error) {
64
            $this->error = "Twitter error: {$matches[1]}";
65
            return false;
66
        }
67
        list($first_line) = explode("\r\n", $response);
68
        if ($first_line !== 'HTTP/1.1 200 OK') {
69
            $this->error = "Request error: {$first_line}";
70
            return false;
71
        }
72
        return true;
73
    }
74
75
}
76