• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / recaptcha / 25248698889

02 May 2026 09:16AM UTC coverage: 94.737% (-5.3%) from 100.0%
25248698889

Pull #629

github

web-flow
Merge 4f41dba1c into 6721ab8bf
Pull Request #629: Restore 1.x API compatibility while keeping 1.5 hardening

89 of 101 new or added lines in 8 files covered. (88.12%)

6 existing lines in 3 files now uncovered.

216 of 228 relevant lines covered (94.74%)

25.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.42
/src/ReCaptcha/RequestMethod/Socket.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This is a PHP library that handles calling reCAPTCHA.
7
 *
8
 * BSD 3-Clause License
9
 *
10
 * @copyright (c) 2019, Google Inc.
11
 *
12
 * @see https://www.google.com/recaptcha
13
 * All rights reserved.
14
 *
15
 * Redistribution and use in source and binary forms, with or without
16
 * modification, are permitted provided that the following conditions are met:
17
 * 1. Redistributions of source code must retain the above copyright notice, this
18
 *    list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright notice,
21
 *    this list of conditions and the following disclaimer in the documentation
22
 *    and/or other materials provided with the distribution.
23
 *
24
 * 3. Neither the name of the copyright holder nor the names of its
25
 *    contributors may be used to endorse or promote products derived from
26
 *    this software without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
 */
39

40
namespace ReCaptcha\RequestMethod;
41

42
/**
43
 * Convenience wrapper around native socket and stream functions to allow mocking.
44
 */
45
class Socket
46
{
47
    /**
48
     * @var mixed
49
     */
50
    private $handle;
51

52
    /**
53
     * @param string     $hostname
54
     * @param int        $port
55
     * @param int        $errno
56
     * @param string     $errstr
57
     * @param null|float $timeout
58
     *
59
     * @return mixed
60
     */
61
    public function fsockopen($hostname, $port = -1, &$errno = 0, &$errstr = '', $timeout = null)
62
    {
63
        $timeout = is_null($timeout) ? floatval(ini_get('default_socket_timeout')) : floatval($timeout);
28✔
64
        $this->handle = fsockopen($hostname, $port, $errno, $errstr, $timeout);
28✔
65

66
        if (false != $this->handle && 0 === $errno && '' === $errstr) {
28✔
67
            return $this->handle;
24✔
68
        }
69

70
        if (false !== $this->handle) {
4✔
71
            $this->fclose();
2✔
72
        }
73

74
        return false;
4✔
75
    }
76

77
    /**
78
     * @param int $seconds
79
     * @param int $microseconds
80
     *
81
     * @return bool
82
     */
83
    public function streamSetTimeout($seconds, $microseconds = 0)
84
    {
85
        // @phpstan-ignore argument.type
86
        return stream_set_timeout($this->handle, $seconds, $microseconds);
24✔
87
    }
88

89
    /**
90
     * @param string   $string
91
     * @param null|int $length
92
     *
93
     * @return false|int
94
     */
95
    public function fwrite($string, $length = null)
96
    {
97
        $string = (string) $string;
22✔
98

99
        // @phpstan-ignore argument.type
100
        return fwrite($this->handle, $string, is_null($length) ? strlen($string) : $length);
22✔
101
    }
102

103
    /**
104
     * @param null|int $length
105
     * @param int      $offset
106
     *
107
     * @return false|string
108
     */
109
    public function streamGetContents($length = null, $offset = -1)
110
    {
111
        if (is_null($length)) {
22✔
112
            // @phpstan-ignore argument.type
113
            return stream_get_contents($this->handle);
22✔
114
        }
115

116
        // @phpstan-ignore argument.type
NEW
117
        return stream_get_contents($this->handle, $length, $offset);
×
118
    }
119

120
    /**
121
     * @param null|int $length
122
     *
123
     * @return false|string
124
     */
125
    public function fgets($length = null)
126
    {
NEW
127
        if (is_null($length)) {
×
128
            // @phpstan-ignore argument.type
NEW
129
            return fgets($this->handle);
×
130
        }
131

NEW
132
        $length = max(0, intval($length));
×
133

134
        // @phpstan-ignore argument.type
NEW
135
        return fgets($this->handle, $length);
×
136
    }
137

138
    /**
139
     * @return bool
140
     */
141
    public function feof()
142
    {
143
        // @phpstan-ignore argument.type
NEW
144
        return feof($this->handle);
×
145
    }
146

147
    /**
148
     * @return bool
149
     */
150
    public function fclose()
151
    {
152
        // @phpstan-ignore argument.type
153
        return fclose($this->handle);
26✔
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc