Socket::isConnected()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 3
b 0
f 2
1
<?php
2
/**
3
 * @author jyamin
4
 */
5
6
namespace FMUP;
7
8
class Socket
9
{
10
    /** @var resource $socket */
11
    protected $socket;
12
13
    /** @var int $errorNumber */
14
    protected $errorNumber;
15
16
    /** @var string $errorString */
17
    protected $errorString;
18
19
    /**
20
     * @param $host
21
     * @param $port
22
     * @return $this
23
     */
24 2
    public function connect($host, $port, $timeout = null)
25
    {
26 2
        if (!$timeout) {
27 1
            $timeout = ini_get('default_socket_timeout');
28
        }
29 2
        $this->socket = $this->phpFSockOpen($host, $port, $this->errorNumber, $this->errorString, $timeout);
30 2
        return $this;
31
    }
32
33
    /**
34
     * @return bool
35
     */
36 2
    public function isConnected()
37
    {
38 2
        return (bool)$this->socket;
39
    }
40
41
    /**
42
     * @param $str
43
     * @param int|null $length
44
     * @return int
45
     */
46 1
    public function write($str, $length = null)
47
    {
48 1
        if ($length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $length of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
49 1
            return $this->phpFWrite($this->socket, $str, $length);
50
        }
51 1
        return $this->phpFWrite($this->socket, $str);
52
    }
53
54
    /**
55
     * @param $length
56
     * @return string
57
     */
58 1
    public function read($length)
59
    {
60 1
        return $this->phpFRead($this->socket, $length);
61
    }
62
63
    /**
64
     * @return int
65
     */
66 3
    public function getErrorNumber()
67
    {
68 3
        return $this->errorNumber;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 3
    public function getErrorString()
75
    {
76 3
        return $this->errorString;
77
    }
78
79
    /**
80
     * @param string $host
81
     * @param int $port
82
     * @param int $errno
83
     * @param string $errstr
84
     * @param float $timeout
85
     * @return resource
86
     * @codeCoverageIgnore
87
     */
88
    protected function phpFSockOpen($host, $port, $errno, $errstr, $timeout)
89
    {
90
        return fsockopen($host, $port, $errno, $errstr, $timeout);
91
    }
92
93
    /**
94
     * @return int
95
     * @codeCoverageIgnore
96
     */
97
    protected function phpFWrite()
98
    {
99
        return call_user_func_array('fwrite', func_get_args());
100
    }
101
102
    /**
103
     * @param resource $handle
104
     * @param int $length
105
     * @return string
106
     * @codeCoverageIgnore
107
     */
108
    protected function phpFRead($handle, $length)
109
    {
110
        return fread($handle, $length);
111
    }
112
113
    /**
114
     * @param resource $handle
115
     * @return bool
116
     * @codeCoverageIgnore
117
     */
118
    protected function phpFClose($handle)
119
    {
120
        return fclose($handle);
121
    }
122
}
123