From 91f914ab2cd882f2efb42e2e8b9486bbc0473e57 Mon Sep 17 00:00:00 2001 From: Benjamin Aerni Date: Wed, 25 Aug 2021 10:35:00 -0700 Subject: [PATCH] Wave of code cleanup / formatting * Some code cleanup * More code cleanup / formatting Now I'm gonna go to bed * Fix typo This is why we have tests * Cleanup literal booleans * Some more general code cleanup * Github's GUI for merge conflicts is not very clear.. * Update PDF_ImageAlpha.php * Revert potentially behavior changing changes that aren't tested --- bin/prepare.php | 2 +- src/bb-library/Api/Handler.php | 8 +- src/bb-library/Box/AppClient.php | 4 +- src/bb-library/Box/Curl.php | 3 +- src/bb-library/Box/Database.php | 16 ++- src/bb-library/Box/Ftp.php | 123 +++++++++++------- src/bb-library/Box/Mail.php | 5 +- src/bb-library/Box/Tools.php | 7 +- src/bb-library/Box/Translate.php | 12 +- src/bb-library/PDF_ImageAlpha.php | 3 +- src/bb-library/Registrar/Adapter/Email.php | 7 +- src/bb-library/Server/Manager/Whm.php | 10 +- src/bb-modules/Activity/Service.php | 2 +- .../class_HTTPRetriever.php | 2 +- src/bb-modules/Servicecustom/Service.php | 2 +- src/bb-modules/Theme/Model/Theme.php | 2 +- tests/bb-modules/Currency/ServiceTest.php | 1 - 17 files changed, 129 insertions(+), 80 deletions(-) diff --git a/bin/prepare.php b/bin/prepare.php index b5c9f429c..ae86c81c5 100644 --- a/bin/prepare.php +++ b/bin/prepare.php @@ -22,7 +22,7 @@ echo sprintf("Connecting to database %s@%s/%s", $user, $host, $dbname) . PHP_EOL $iter = 30; $connected = false; -while ($connected == false && $iter > 0) { +while (!$connected && $iter > 0) { try { $dbh = new PDO($type . ':host=' . $host, $user, $password, array( diff --git a/src/bb-library/Api/Handler.php b/src/bb-library/Api/Handler.php index b7d7fe764..3215d9018 100644 --- a/src/bb-library/Api/Handler.php +++ b/src/bb-library/Api/Handler.php @@ -79,7 +79,9 @@ final class Api_Handler implements InjectionAwareInterface if($this->_acl_exception) { throw new \Box_Exception('You do not have access to :mod module', array(':mod'=>$mod), 725); } else { - if(BB_DEBUG) error_log('You do not have access to '.$mod. ' module'); + if(BB_DEBUG) { + error_log('You do not have access to '.$mod. ' module'); + } return null; } } @@ -110,7 +112,9 @@ final class Api_Handler implements InjectionAwareInterface } } $res = $api->{$method_name}($arguments); - if($this->_enable_cache) $this->_cache[$cache_key] = $res; + if($this->_enable_cache){ + $this->_cache[$cache_key] = $res; + } return $res; } } \ No newline at end of file diff --git a/src/bb-library/Box/AppClient.php b/src/bb-library/Box/AppClient.php index 44b3e9eb7..8a66c64a9 100644 --- a/src/bb-library/Box/AppClient.php +++ b/src/bb-library/Box/AppClient.php @@ -55,7 +55,9 @@ class Box_AppClient extends Box_App try { return $this->render($tpl, array('post'=>$_POST), $ext); } catch(Exception $e) { - if(BB_DEBUG) error_log($e); + if(BB_DEBUG){ + error_log($e); + } } // throw new \Box_Exception('Page :url not found', array(':url'=>$page), 404); $e = new \Box_Exception('Page :url not found', array(':url'=>$this->url), 404); diff --git a/src/bb-library/Box/Curl.php b/src/bb-library/Box/Curl.php index b202c0323..68f0ec6d9 100644 --- a/src/bb-library/Box/Curl.php +++ b/src/bb-library/Box/Curl.php @@ -74,8 +74,9 @@ class Box_Curl { public function useAuth($use) { $this->authentication = 0; - if ($use == true) + if ($use){ $this->authentication = 1; + } } public function setName($name) { diff --git a/src/bb-library/Box/Database.php b/src/bb-library/Box/Database.php index 4a48403b9..cbe93a5ff 100644 --- a/src/bb-library/Box/Database.php +++ b/src/bb-library/Box/Database.php @@ -38,7 +38,9 @@ class Box_Database implements InjectionAwareInterface { $type = $this->_getTypeFromModelName($modelName); $bean = $this->orm->dispense($type); - if($type == $modelName) return $bean; + if($type == $modelName){ + return $bean; + } return $bean->box(); } @@ -76,7 +78,9 @@ class Box_Database implements InjectionAwareInterface { $type = $this->_getTypeFromModelName($modelName); $bean = $this->orm->findOne($type, $sql, $values); - if($type == $modelName) return $bean; + if($type == $modelName) { + return $bean; + } if($bean && $bean->id) { return $bean->box(); } @@ -87,7 +91,9 @@ class Box_Database implements InjectionAwareInterface { $type = $this->_getTypeFromModelName($modelName); $beans = $this->orm->find($type, $sql, $values); - if($type == $modelName) return $beans; + if($type == $modelName){ + return $beans; + } foreach ($beans as &$bean) { $bean = $bean->box(); } @@ -110,7 +116,9 @@ class Box_Database implements InjectionAwareInterface $type = $this->_getTypeFromModelName($modelName); $bean = $this->orm->load($type, $id); - if ($type == $modelName) return $bean; + if ($type == $modelName){ + return $bean; + } if ($bean && $bean->id) { return $bean->box(); } diff --git a/src/bb-library/Box/Ftp.php b/src/bb-library/Box/Ftp.php index e5a2b0df0..7200abb6b 100644 --- a/src/bb-library/Box/Ftp.php +++ b/src/bb-library/Box/Ftp.php @@ -35,41 +35,48 @@ class Box_Ftp } // Set defaults: - if ( empty($opt['port']) ) + if ( empty($opt['port']) ) { $this->options['port'] = 21; - else + } else { $this->options['port'] = $opt['port']; + } - if ( empty($opt['hostname']) ) + if ( empty($opt['hostname']) ) { throw new \Box_Exception('FTP hostname is required'); - else + } else { $this->options['hostname'] = $opt['hostname']; + } - if ( isset($opt['root']) && ! empty($opt['root']) ) + if ( isset($opt['root']) && ! empty($opt['root']) ) { $this->bb_root = $opt['root']; + } // Check if the options provided are OK. - if ( empty($opt['username']) ) + if ( empty($opt['username']) ) { throw new \Box_Exception('FTP username is required'); - else + } else { $this->options['username'] = $opt['username']; + } - if ( empty($opt['password']) ) + if ( empty($opt['password']) ) { throw new \Box_Exception('FTP password is required'); - else + } else { $this->options['password'] = $opt['password']; + } $this->options['ssl'] = false; - if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] ) + if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] ) { $this->options['ssl'] = true; + } } public function connect() { - if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') ) + if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') ) { $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], $this->timeout); - else + } else { $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], $this->timeout); + } if ( ! $this->link ) { throw new \Box_Exception('Failed to connect to FTP Server :host : :port',array(':host'=>$this->options['hostname'], ':port'=>$this->options['port'])); @@ -97,21 +104,25 @@ class Box_Ftp public function get_contents($file, $type = '', $resumepos = 0 ) { - if( empty($type) ) + if( empty($type) ) { $type = FTP_BINARY; + } $temp = tmpfile(); - if ( ! $temp ) + if ( ! $temp ) { return false; + } - if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) ) + if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) ) { return false; + } fseek($temp, 0); //Skip back to the start of the file being written to $contents = ''; - while ( ! feof($temp) ) + while ( ! feof($temp) ) { $contents .= fread($temp, 8192); + } fclose($temp); return $contents; @@ -127,12 +138,14 @@ class Box_Ftp */ public function put_contents($file, $contents, $type = '' ) { - if( empty($type) ) + if( empty($type) ) { $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII; + } $temp = tmpfile(); - if ( ! $temp ) + if ( ! $temp ) { return false; + } fwrite($temp, $contents); fseek($temp, 0); //Skip back to the start of the file being written to @@ -198,15 +211,19 @@ class Box_Ftp public function chmod($file, $mode = false, $recursive = false) { - if( ! $mode ) + if( ! $mode ) { $mode = $this->permission; - if( ! $mode ) return false; - if ( ! $this->exists($file) && ! $this->is_dir($file) ) + } + + if ( ! $this->exists($file) && ! $this->is_dir($file) ) { return false; + } + if ( ! $recursive || ! $this->is_dir($file) ) { - if ( ! function_exists('ftp_chmod') ) + if ( ! function_exists('ftp_chmod') ) { return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file)); + } return @ftp_chmod($this->link, $mode, $file); } //Is a directory, and we want recursive @@ -254,8 +271,9 @@ class Box_Ftp */ public function copyDir($source, $destination, $overwrite = false ) { - if( ! $overwrite && !$this->is_dir($destination) ) + if( ! $overwrite && !$this->is_dir($destination) ) { return false; + } if( $overwrite && !$this->is_dir($destination) ) { if(!$this->mkdir($destination)) { @@ -270,7 +288,9 @@ class Box_Ftp $res = false; $this->chdir($source); $dir_contents = $this->dirlist($source, false, true); - if (empty($dir_contents)) { return true; } + if (empty($dir_contents)) { + return true; + } foreach($dir_contents as $name=>$params) { if ($params['isdir']) { @@ -289,11 +309,13 @@ class Box_Ftp */ public function copy($source, $destination, $overwrite = false ) { - if( ! $overwrite && $this->exists($destination) ) + if( ! $overwrite && $this->exists($destination) ) { return false; + } $content = $this->get_contents($source); - if( false === $content) + if( false === $content) { return false; + } return $this->put_contents($destination, $content); } @@ -304,17 +326,22 @@ class Box_Ftp public function delete($file, $recursive = false ) { - if ( empty($file) ) + if ( empty($file) ) { return false; - if ( $this->is_file($file) ) + } + if ( $this->is_file($file) ) { return @ftp_delete($this->link, $file); - if ( !$recursive ) + } + if ( !$recursive ) { return @ftp_rmdir($this->link, $file); + } $filelist = $this->dirlist( $this->trailingslashit($file) ); - if ( !empty($filelist) ) - foreach ( $filelist as $delete_file ) + if ( !empty($filelist) ) { + foreach ( $filelist as $delete_file ) { $this->delete( $this->trailingslashit($file) . $delete_file['name'], $recursive); + } + } return @ftp_rmdir($this->link, $file); } @@ -376,14 +403,18 @@ class Box_Ftp */ public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { - if( !ftp_mkdir($this->link, $path) ) + if( !ftp_mkdir($this->link, $path) ) { return false; - if( $chmod ) + } + if( $chmod ) { $this->chmod($path, $chmod); - if( $chown ) + } + if( $chown ) { $this->chown($path, $chown); - if( $chgrp ) + } + if( $chgrp ) { $this->chgrp($path, $chgrp); + } return true; } @@ -397,8 +428,9 @@ class Box_Ftp static $is_windows; $b = array(); - if ( is_null($is_windows) ) + if ( is_null($is_windows) ) { $is_windows = strpos( strtolower(ftp_systype($this->link)), 'win') !== false; + } if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|) +(.+)/", $line, $lucifer)) { if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix @@ -470,25 +502,29 @@ class Box_Ftp $list = ftp_rawlist($this->link, '-a ' . $path, false); - if ( $list === false ) + if ( $list === false ) { return false; + } $dirlist = array(); foreach ( $list as $k => $v ) { $entry = $this->parselisting($v); - if ( empty($entry) ) + if ( empty($entry) ) { continue; - - if ( '.' == $entry["name"] || '..' == $entry["name"] ) + } + if ( '.' == $entry["name"] || '..' == $entry["name"] ) { continue; + } $dirlist[ $entry['name'] ] = $entry; } - if ( ! $dirlist ) + if ( ! $dirlist ) { return false; - if ( empty($dirlist) ) + } + if ( empty($dirlist) ) { return array(); + } $ret = array(); foreach ( $dirlist as $struc ) { @@ -519,8 +555,7 @@ class Box_Ftp */ public function dirContents() { - $contents = ftp_nlist($this->link, "."); - return $contents; + return ftp_nlist($this->link, "."); } protected function trailingslashit($string) diff --git a/src/bb-library/Box/Mail.php b/src/bb-library/Box/Mail.php index 7149de20c..3c6b8f9a8 100644 --- a/src/bb-library/Box/Mail.php +++ b/src/bb-library/Box/Mail.php @@ -160,9 +160,10 @@ class Box_Mail $mail->Host = $host; $mail->SMTPDebug = 0; - if($port) + if($port) { $mail->Port = (int)$port; - + } + if($user) { $mail->SMTPAuth = true; $mail->SMTPSecure = $security; diff --git a/src/bb-library/Box/Tools.php b/src/bb-library/Box/Tools.php index 9190d4d95..5719b9e15 100644 --- a/src/bb-library/Box/Tools.php +++ b/src/bb-library/Box/Tools.php @@ -179,8 +179,7 @@ class Box_Tools */ public function getReferer($default = '/') { - $r = empty($_SERVER['HTTP_REFERER']) ? $default : $_SERVER['HTTP_REFERER']; - return $r; + return empty($_SERVER['HTTP_REFERER']) ? $default : $_SERVER['HTTP_REFERER']; } /** @@ -242,9 +241,7 @@ class Box_Tools } shuffle($passOrder); - $password = implode('', $passOrder); - - return $password; + return implode('', $passOrder); } public function autoLinkText($text) diff --git a/src/bb-library/Box/Translate.php b/src/bb-library/Box/Translate.php index 9be1c1478..196a8cf7c 100644 --- a/src/bb-library/Box/Translate.php +++ b/src/bb-library/Box/Translate.php @@ -71,8 +71,12 @@ class Box_Translate implements \Box\InjectionAwareInterface @putenv('LANG='.$locale.'.'.$codeset); @putenv('LANGUAGE='.$locale.'.'.$codeset); // set locale - if (!defined('LC_MESSAGES')) define('LC_MESSAGES', 5); - if (!defined('LC_TIME')) define('LC_TIME', 2); + if (!defined('LC_MESSAGES')) { + define('LC_MESSAGES', 5); + } + if (!defined('LC_TIME')) { + define('LC_TIME', 2); + } setlocale(LC_MESSAGES, $locale.'.'.$codeset); setlocale(LC_TIME, $locale.'.'.$codeset); bindtextdomain($this->domain, BB_PATH_LANGS); @@ -82,7 +86,9 @@ class Box_Translate implements \Box\InjectionAwareInterface if (!function_exists('__')) { function __($msgid, array $values = NULL) { - if (empty($msgid)) return null; + if (empty($msgid)) { + return null; + } $string = gettext($msgid); return empty($values) ? $string : strtr($string, $values); } diff --git a/src/bb-library/PDF_ImageAlpha.php b/src/bb-library/PDF_ImageAlpha.php index 9f1b2c32d..85a3d43f5 100644 --- a/src/bb-library/PDF_ImageAlpha.php +++ b/src/bb-library/PDF_ImageAlpha.php @@ -27,8 +27,9 @@ function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='', $isMask= $type=strtolower($type); if($type=='png'){ $info=$this->_parsepng($file); - if($info=='alpha') + if($info=='alpha'){ return $this->ImagePngWithAlpha($file,$x,$y,$w,$h,$link); + } } else { diff --git a/src/bb-library/Registrar/Adapter/Email.php b/src/bb-library/Registrar/Adapter/Email.php index 649a6ba44..c9cf08312 100644 --- a/src/bb-library/Registrar/Adapter/Email.php +++ b/src/bb-library/Registrar/Adapter/Email.php @@ -708,11 +708,8 @@ class Whois2 { return false; } }else { - if(preg_match("/".$not_found_string."/i",$whois_string)) { - return true; - }else { - return false; - } + return preg_match("/".$not_found_string."/i",$whois_string); + } } } } diff --git a/src/bb-library/Server/Manager/Whm.php b/src/bb-library/Server/Manager/Whm.php index 48c3dc5f3..d675d48c2 100644 --- a/src/bb-library/Server/Manager/Whm.php +++ b/src/bb-library/Server/Manager/Whm.php @@ -1072,7 +1072,6 @@ class xmlapi { // Set the $auth string - $authstr; if ( $this->auth_type == 'hash' ) { $authstr = 'Authorization: WHM ' . $this->user . ':' . $this->auth . "\r\n"; } elseif ($this->auth_type == 'pass' ) { @@ -1087,7 +1086,6 @@ class xmlapi { // Perform the query (or pass the info to the functions that actually do perform the query) - $response; if ( $this->http_client == 'curl' ) { $response = $this->curl_query($url, $args, $authstr); } elseif ( $this->http_client == 'fopen' ) { @@ -1114,12 +1112,12 @@ class xmlapi { // The only time a response should contain is in the case of authentication error // cPanel 11.25 fixes this issue, but if is in the response, we'll error out. - if (stristr($response, '') == true) { - if (stristr($response, 'Login Attempt Failed') == true) { + if (stristr($response, '')) { + if (stristr($response, 'Login Attempt Failed')) { error_log("Login Attempt Failed"); return; } - if (stristr($response, 'action="/login/"') == true) { + if (stristr($response, 'action="/login/"')) { error_log("Authentication Error"); return; } @@ -1180,7 +1178,7 @@ class xmlapi { curl_setopt($curl, CURLOPT_POSTFIELDS, ""); $result = curl_exec($curl); - if ($result == false) { + if (!$result) { throw new Exception("curl_exec threw error \"" . curl_error($curl) . "\" for " . $url . "?" . $postdata ); } curl_close($curl); diff --git a/src/bb-modules/Activity/Service.php b/src/bb-modules/Activity/Service.php index 40eaae7ed..35cb31bec 100644 --- a/src/bb-modules/Activity/Service.php +++ b/src/bb-modules/Activity/Service.php @@ -121,7 +121,7 @@ class Service implements InjectionAwareInterface $params[':search2'] = $search; } - if (count ($where) > 0){ + if (!empty($where)){ $whereStatment = implode(' and ', $where); $sql .= ' WHERE '.$whereStatment; } diff --git a/src/bb-modules/Servicecentovacast/class_HTTPRetriever.php b/src/bb-modules/Servicecentovacast/class_HTTPRetriever.php index 57ab050de..6a567a281 100644 --- a/src/bb-modules/Servicecentovacast/class_HTTPRetriever.php +++ b/src/bb-modules/Servicecentovacast/class_HTTPRetriever.php @@ -13,7 +13,7 @@ namespace Box\Mod\Servicecentovacast; * requests are supported. * * This can be used in place of something like CURL or WGET for HTTP requests. - * Native SSL (HTTPS) requests are also supported if the OpenSSL extension is 0 + * Native SSL (HTTPS) requests are also supported if the OpenSSL extension is * installed under PHP v4.3.0 or greater. * * If native SSL support is not available, the class will also check for the diff --git a/src/bb-modules/Servicecustom/Service.php b/src/bb-modules/Servicecustom/Service.php index 337e4119e..366e3d404 100644 --- a/src/bb-modules/Servicecustom/Service.php +++ b/src/bb-modules/Servicecustom/Service.php @@ -41,7 +41,7 @@ class Service implements \Box\InjectionAwareInterface foreach ($form['fields'] as $field) { if ($field['required'] == 1) { $field_name = $field['name']; - if ((!isset($data[$field_name]) || empty($data[$field_name]))) { + if (!isset($data[$field_name]) || empty($data[$field_name])) { throw new \Box_Exception("You must fill in all required fields. " . $field['label'] . " is missing", null, 9684); } } diff --git a/src/bb-modules/Theme/Model/Theme.php b/src/bb-modules/Theme/Model/Theme.php index 97ea1e1fd..75b13e805 100644 --- a/src/bb-modules/Theme/Model/Theme.php +++ b/src/bb-modules/Theme/Model/Theme.php @@ -182,7 +182,7 @@ class Theme preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); $tags = array_unique($tags[1]); - if(is_array($tags) && count($tags) > 0) { + if(is_array($tags) && !empty($tags)) { if($invert === FALSE) { return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?@si', '', $text); } diff --git a/tests/bb-modules/Currency/ServiceTest.php b/tests/bb-modules/Currency/ServiceTest.php index 1cb46ef18..881e50b0c 100644 --- a/tests/bb-modules/Currency/ServiceTest.php +++ b/tests/bb-modules/Currency/ServiceTest.php @@ -229,7 +229,6 @@ class ServiceTest extends \BBTestCase $di['db'] = $db; $service->setDi($di); - $code = $code; $result = $service->getRateByCode($code); $this->assertEquals($expected, $result);