@@ -27,6 +27,7 @@ class Ftp
2727 protected function checkConnected ()
2828 {
2929 if (!$ this ->_conn ) {
30+ // phpcs:ignore Magento2.Exceptions.DirectThrow
3031 throw new \Exception (__CLASS__ . " - no connection established with server " );
3132 }
3233 }
@@ -53,7 +54,7 @@ public function mdkir($name)
5354 public function mkdirRecursive ($ path , $ mode = 0777 )
5455 {
5556 $ this ->checkConnected ();
56- $ dir = explode ("/ " , $ path );
57+ $ dir = explode ("/ " , ( string ) $ path );
5758 $ path = "" ;
5859 $ ret = true ;
5960 for ($ i = 0 , $ count = count ($ dir ); $ i < $ count ; $ i ++) {
@@ -84,6 +85,7 @@ public function login($login = "anonymous", $password = "test@gmail.com")
8485 $ this ->checkConnected ();
8586 $ res = @ftp_login ($ this ->_conn , $ login , $ password );
8687 if (!$ res ) {
88+ // phpcs:ignore Magento2.Exceptions.DirectThrow
8789 throw new \Exception ("Invalid login credentials " );
8890 }
8991 return $ res ;
@@ -100,17 +102,19 @@ public function validateConnectionString($string)
100102 {
101103 $ data = parse_url ($ string );
102104 if (false === $ data ) {
105+ // phpcs:ignore Magento2.Exceptions.DirectThrow
103106 throw new \Exception ("Connection string invalid: ' {$ string }' " );
104107 }
105108 if ($ data ['scheme ' ] != 'ftp ' ) {
109+ // phpcs:ignore Magento2.Exceptions.DirectThrow
106110 throw new \Exception ("Support for scheme unsupported: ' {$ data ['scheme ' ]}' " );
107111 }
108-
112+
109113 // Decode user & password strings from URL
110114 foreach (array_intersect (array_keys ($ data ), ['user ' ,'pass ' ]) as $ key ) {
111115 $ data [$ key ] = urldecode ($ data [$ key ]);
112116 }
113-
117+
114118 return $ data ;
115119 }
116120
@@ -132,6 +136,7 @@ public function connect($string, $timeout = 900)
132136 $ this ->_conn = ftp_connect ($ params ['host ' ], $ port , $ timeout );
133137
134138 if (!$ this ->_conn ) {
139+ // phpcs:ignore Magento2.Exceptions.DirectThrow
135140 throw new \Exception ("Cannot connect to host: {$ params ['host ' ]}" );
136141 }
137142 if (isset ($ params ['user ' ]) && isset ($ params ['pass ' ])) {
@@ -141,6 +146,7 @@ public function connect($string, $timeout = 900)
141146 }
142147 if (isset ($ params ['path ' ])) {
143148 if (!$ this ->chdir ($ params ['path ' ])) {
149+ // phpcs:ignore Magento2.Exceptions.DirectThrow
144150 throw new \Exception ("Cannot chdir after login to: {$ params ['path ' ]}" );
145151 }
146152 }
@@ -151,7 +157,7 @@ public function connect($string, $timeout = 900)
151157 *
152158 * @param string $remoteFile
153159 * @param resource $handle
154- * @param int $mode FTP_BINARY | FTP_ASCII
160+ * @param int $mode FTP_BINARY | FTP_ASCII
155161 * @param int $startPos
156162 * @return bool
157163 */
@@ -184,7 +190,7 @@ public function put($remoteFile, $localFile, $mode = FTP_BINARY, $startPos = 0)
184190 public function getcwd ()
185191 {
186192 $ d = $ this ->raw ("pwd " );
187- $ data = explode (" " , $ d [0 ], 3 );
193+ $ data = explode (" " , $ d [0 ] ?? '' , 3 );
188194 if (empty ($ data [1 ])) {
189195 return false ;
190196 }
@@ -228,19 +234,23 @@ public function upload($remote, $local, $dirMode = 0777, $ftpMode = FTP_BINARY)
228234 $ this ->checkConnected ();
229235
230236 if (!file_exists ($ local )) {
237+ // phpcs:ignore Magento2.Exceptions.DirectThrow
231238 throw new \Exception ("Local file doesn't exist: {$ local }" );
232239 }
233240 if (!is_readable ($ local )) {
241+ // phpcs:ignore Magento2.Exceptions.DirectThrow
234242 throw new \Exception ("Local file is not readable: {$ local }" );
235243 }
236244 if (is_dir ($ local )) {
245+ // phpcs:ignore Magento2.Exceptions.DirectThrow
237246 throw new \Exception ("Directory given instead of file: {$ local }" );
238247 }
239248
240- $ globalPathMode = substr ($ remote , 0 , 1 ) == "/ " ;
249+ $ globalPathMode = substr (( string ) $ remote , 0 , 1 ) == "/ " ;
241250 $ dirname = dirname ($ remote );
242251 $ cwd = $ this ->getcwd ();
243252 if (false === $ cwd ) {
253+ // phpcs:ignore Magento2.Exceptions.DirectThrow
244254 throw new \Exception ("Server returns something awful on PWD command " );
245255 }
246256
@@ -262,7 +272,7 @@ public function upload($remote, $local, $dirMode = 0777, $ftpMode = FTP_BINARY)
262272 *
263273 * @param string $remote
264274 * @param string $local
265- * @param int $ftpMode FTP_BINARY|FTP_ASCII
275+ * @param int $ftpMode FTP_BINARY|FTP_ASCII
266276 * @return bool
267277 */
268278 public function download ($ remote , $ local , $ ftpMode = FTP_BINARY )
@@ -336,7 +346,7 @@ public function cdup()
336346 *
337347 * @param string $localFile
338348 * @param string $remoteFile
339- * @param int $fileMode FTP_BINARY | FTP_ASCII
349+ * @param int $fileMode FTP_BINARY | FTP_ASCII
340350 * @param int $resumeOffset
341351 * @return bool
342352 * @SuppressWarnings(PHPMD.BooleanGetMethodName)
@@ -397,7 +407,7 @@ public static function byteconvert($bytes)
397407 public static function chmodnum ($ chmod )
398408 {
399409 $ trans = ['- ' => '0 ' , 'r ' => '4 ' , 'w ' => '2 ' , 'x ' => '1 ' ];
400- $ chmod = substr (strtr ($ chmod , $ trans ), 1 );
410+ $ chmod = $ chmod !== null ? substr (strtr ($ chmod , $ trans ), 1 ) : '' ;
401411 $ array = str_split ($ chmod , 3 );
402412 return array_sum (str_split ($ array [0 ])) . array_sum (str_split ($ array [1 ])) . array_sum (str_split ($ array [2 ]));
403413 }
@@ -477,6 +487,10 @@ public function ls($dir = "/", $recursive = false)
477487 */
478488 public function correctFilePath ($ str )
479489 {
490+ if ($ str === null ) {
491+ return '' ;
492+ }
493+
480494 $ str = str_replace ("\\" , "/ " , $ str );
481495 $ str = preg_replace ("/^.\// " , "" , $ str );
482496 return $ str ;
0 commit comments