Issues (6)

src/SftpFilesystem.php (1 issue)

1
<?php
2
/**
3
 * @link https://github.com/acidwave/yii2-flysystem
4
 * @copyright Copyright (c) 2021 Acid Wave
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace Acidwave\Flysystem;
9
10
use League\Flysystem\PhpseclibV2\SftpAdapter;
11
use League\Flysystem\PhpseclibV2\SftpConnectionProvider;
12
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
13
use Yii;
14
use yii\base\InvalidConfigException;
15
16
/**
17
 * SftpFilesystem
18
 *
19
 * @author Acid Wave <[email protected]>
20
 */
21
class SftpFilesystem extends Filesystem
22
{
23
    /**
24
     * @var string
25
     */
26
    public $host;
27
    /**
28
     * @var integer
29
     */
30
    public $port = 22;
31
    /**
32
     * @var string
33
     */
34
    public $username;
35
    /**
36
     * @var string
37
     */
38
    public $password;
39
    /**
40
     * @var integer
41
     */
42
    public $timeout = 10;
43
    /**
44
     * @var string
45
     */
46
    public $root;
47
    /**
48
     * @var string
49
     */
50
    public $privateKey;
51
    /**
52
     * @var string
53
     */
54
    public $passphrase;
55
    /**
56
     * @var integer
57
     */
58
    public $dirPrivate = 700;
59
    /**
60
     * @var integer
61
     */
62
    public $dirPublic = 755;
63
    /**
64
     * @var integer
65
     */
66
    public $filePrivate = 600;
67
    /**
68
     * @var integer
69
     */
70
    public $filePublic = 644;
71
    /**
72
     * @var bool
73
     */
74
    public $useAgent = false;
75
    /**
76
     * @inheritdoc
77
     */
78
    public function init()
79
    {
80
        if ($this->host === null) {
81
            throw new InvalidConfigException('The "host" property must be set.');
82
        }
83
84
        if ($this->username === null) {
85
            throw new InvalidConfigException('The "username" property must be set.');
86
        }
87
88
        if ($this->password === null && $this->privateKey === null) {
89
            throw new InvalidConfigException('Either "password" or "privateKey" property must be set.');
90
        }
91
92
        if ($this->root === null) {
93
            throw new InvalidConfigException('The "root" property must be set.');
94
        }
95
96
        $this->root = Yii::getAlias($this->root);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::getAlias($this->root) can also be of type false. However, the property $root is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
97
98
        parent::init();
99
    }
100
101
    /**
102
     * @return SftpAdapter
103
     */
104
    protected function prepareAdapter()
105
    {
106
        $provider = new SftpConnectionProvider(
107
            $this->host,
108
            $this->username,
109
            $this->password,
110
            $this->privateKey,
111
            $this->passphrase,
112
            $this->port,
113
            $this->useAgent,
114
            $this->timeout
115
        );
116
        $visibility = PortableVisibilityConverter::fromArray([
117
            'file' => [
118
                'public' => $this->filePublic,
119
                'private' => $this->filePrivate,
120
            ],
121
            'dir' => [
122
                'public' => $this->dirPublic,
123
                'private' => $this->dirPrivate,
124
            ],
125
        ]);
126
        return new SftpAdapter($provider, $this->root, $visibility);
127
    }
128
}
129