FtpFilesystem::init()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 5
nop 0
dl 0
loc 21
rs 9.6111
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\Ftp\FtpAdapter;
11
use League\Flysystem\Ftp\FtpConnectionOptions;
12
use Yii;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * FtpFilesystem
17
 *
18
 * @author Acid Wave <[email protected]>
19
 */
20
class FtpFilesystem extends Filesystem
21
{
22
    /**
23
     * @var string
24
     */
25
    public $host;
26
    /**
27
     * @var integer
28
     */
29
    public $port;
30
    /**
31
     * @var string
32
     */
33
    public $username;
34
    /**
35
     * @var string
36
     */
37
    public $password;
38
    /**
39
     * @var boolean
40
     */
41
    public $ssl;
42
    /**
43
     * @var integer
44
     */
45
    public $timeout;
46
    /**
47
     * @var string
48
     */
49
    public $root;
50
    /**
51
     * @var integer
52
     */
53
    public $permPrivate;
54
    /**
55
     * @var integer
56
     */
57
    public $permPublic;
58
    /**
59
     * @var boolean
60
     */
61
    public $passive;
62
    /**
63
     * @var integer
64
     */
65
    public $transferMode;
66
    /**
67
     * @var bool
68
     */
69
    public $enableTimestampsOnUnixListings = false;
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function init()
75
    {
76
        if ($this->host === null) {
77
            throw new InvalidConfigException('The "host" property must be set.');
78
        }
79
80
        if ($this->root === null) {
81
            throw new InvalidConfigException('The "root" 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) {
89
            throw new InvalidConfigException('The "password" property must be set.');
90
        }
91
92
        $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...
93
94
        parent::init();
95
    }
96
97
    /**
98
     * @return FtpAdapter
99
     */
100
    protected function prepareAdapter()
101
    {
102
        $config = [];
103
104
        foreach ([
105
            'host',
106
            'port',
107
            'username',
108
            'password',
109
            'ssl',
110
            'timeout',
111
            'root',
112
            'passive',
113
            'transferMode',
114
            'enableTimestampsOnUnixListings',
115
            'systemType',
116
        ] as $name) {
117
            if ($this->$name !== null) {
118
                $config[$name] = $this->$name;
119
            }
120
        }
121
122
        return new FtpAdapter(FtpConnectionOptions::fromArray($config));
123
    }
124
}
125