Issues (15)

src/Stream/PassiveDataStream.php (3 issues)

Labels
1
<?php
2
3
/**
4
 * This file is part of the Lazzard/ftp-bridge package.
5
 *
6
 * (c) El Amrani Chakir <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lazzard\FtpBridge\Stream;
13
14
use Lazzard\FtpBridge\Response\Response;
15
use Lazzard\FtpBridge\Error\ErrorTrigger;
16
17
/**
18
 * @since  1.0
19
 * @author El Amrani Chakir <[email protected]>
20
 * 
21
 * @internal
22
 */
23
class PassiveDataStream extends DataStream
24
{
25
    /**
26
     * Opens the data connection stream to the server port sent via the FTP server after
27
     * sending the PASV command.
28
     *
29
     *  {@inheritDoc}
30
     */
31
    public function open()
32
    {
33
        $this->commandStream->write('PASV');
34
        $response = new Response($this->commandStream->read());
0 ignored issues
show
It seems like $this->commandStream->read() can also be of type false; however, parameter $reply of Lazzard\FtpBridge\Response\Response::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $response = new Response(/** @scrutinizer ignore-type */ $this->commandStream->read());
Loading history...
35
        if ($response->getCode() !== 227) {
36
            return !ErrorTrigger::raise($response->getMessage());
37
        }
38
39
        if (!preg_match('/(\d+,){4}+/', $response->getMessage(), $ipMatches)
40
            || !preg_match('/\d+,\d+\)/', $response->getMessage(), $portMatches)) {
41
            return !ErrorTrigger::raise("Unable to get the passive IP & PORT from the reply message.");
42
        }
43
44
        $ip    = rtrim(str_replace(",", ".", $ipMatches[0]), ".");
45
        $ports = explode(",", rtrim($portMatches[0], ")"));
46
        $port  = ($ports[0] * 256) + $ports[1];
47
48
        return $this->openStreamSocket($ip, $port, $this->commandStream->timeout, $this->commandStream->blocking);
0 ignored issues
show
Accessing timeout on the interface Lazzard\FtpBridge\Stream\StreamInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Accessing blocking on the interface Lazzard\FtpBridge\Stream\StreamInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function read()
55
    {
56
        $data = '';
57
        while (!feof($this->stream)) {
58
            $data .= fread($this->stream, 8192);
59
        }
60
61
        $this->log($data);
62
        
63
        return $data;
64
    }
65
}