Twitter   F
last analyzed

Complexity

Total Complexity 371

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 2
c 0
b 0
f 0
wmc 371
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B update() 0 48 11

How to fix   Complexity   

Complex Class

Complex classes like Twitter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Twitter, and based on these observations, apply Extract Interface, too.

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