@@ -15,8 +15,9 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | public function __construct($path_to_file, $mode='r') |
| 17 | 17 | { |
| 18 | - if(!FileSystem::exists($path_to_file) && self::requires_existing_file($mode)) |
|
| 19 | - throw new \Exception('FILE_MUST_ALREADY_EXIST ('.$this->filepath().', '.$this->mode.')'); |
|
| 18 | + if(!FileSystem::exists($path_to_file) && self::requires_existing_file($mode)) { |
|
| 19 | + throw new \Exception('FILE_MUST_ALREADY_EXIST ('.$this->filepath().', '.$this->mode.')'); |
|
| 20 | + } |
|
| 20 | 21 | |
| 21 | 22 | $this->filepath = new FilePath($path_to_file); |
| 22 | 23 | $this->mode = $mode; |
@@ -25,18 +26,21 @@ discard block |
||
| 25 | 26 | public function open() |
| 26 | 27 | { |
| 27 | 28 | $this->pointer = fopen($this->filepath, $this->mode); |
| 28 | - if($this->pointer === false) |
|
| 29 | - throw new \Exception('FILE_OPEN_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 29 | + if($this->pointer === false) { |
|
| 30 | + throw new \Exception('FILE_OPEN_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 31 | + } |
|
| 30 | 32 | return $this->pointer; |
| 31 | 33 | } |
| 32 | 34 | |
| 33 | 35 | public function close() |
| 34 | 36 | { |
| 35 | - if(!is_resource($this->pointer)) |
|
| 36 | - return true; |
|
| 37 | + if(!is_resource($this->pointer)) { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 37 | 40 | |
| 38 | - if(fclose($this->pointer) === false) |
|
| 39 | - throw new \Exception('FILE_CLOSE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 41 | + if(fclose($this->pointer) === false) { |
|
| 42 | + throw new \Exception('FILE_CLOSE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 43 | + } |
|
| 40 | 44 | |
| 41 | 45 | return true; |
| 42 | 46 | } |
@@ -48,12 +52,14 @@ discard block |
||
| 48 | 52 | |
| 49 | 53 | public function set_content($content) |
| 50 | 54 | { |
| 51 | - if(is_writable($this->filepath) !== true && self::requires_existing_file($this->mode)) |
|
| 52 | - throw new \Exception('FILE_IS_NOT_WRITABLE ('.$this->filepath().', '.$this->mode.')'); |
|
| 55 | + if(is_writable($this->filepath) !== true && self::requires_existing_file($this->mode)) { |
|
| 56 | + throw new \Exception('FILE_IS_NOT_WRITABLE ('.$this->filepath().', '.$this->mode.')'); |
|
| 57 | + } |
|
| 53 | 58 | |
| 54 | 59 | $this->open(); |
| 55 | - if(fwrite($this->pointer, $content) === false) |
|
| 56 | - throw new \Exception('FILE_WRITE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 60 | + if(fwrite($this->pointer, $content) === false) { |
|
| 61 | + throw new \Exception('FILE_WRITE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
| 62 | + } |
|
| 57 | 63 | $this->close(); |
| 58 | 64 | } |
| 59 | 65 | |
@@ -71,8 +77,9 @@ discard block |
||
| 71 | 77 | { |
| 72 | 78 | $res = $this->size_in_bytes; |
| 73 | 79 | |
| 74 | - if(is_null($res) && ($res = filesize("$this->filepath")) !== false) |
|
| 75 | - $this->size_in_bytes = $res; |
|
| 80 | + if(is_null($res) && ($res = filesize("$this->filepath")) !== false) { |
|
| 81 | + $this->size_in_bytes = $res; |
|
| 82 | + } |
|
| 76 | 83 | |
| 77 | 84 | return $res; |
| 78 | 85 | } |
@@ -19,12 +19,14 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function filenames($regex=null) : array |
| 21 | 21 | { |
| 22 | - if(!file_exists($this->root_path) && mkdir($this->root_path) === false) |
|
| 23 | - return []; |
|
| 22 | + if(!file_exists($this->root_path) && mkdir($this->root_path) === false) { |
|
| 23 | + return []; |
|
| 24 | + } |
|
| 24 | 25 | |
| 25 | 26 | $filenames = self::preg_scandir($this->root_path, $regex);// ID_SEQUENCENUMBER.ext |
| 26 | - if(!is_null($filenames)) |
|
| 27 | - sort($filenames); |
|
| 27 | + if(!is_null($filenames)) { |
|
| 28 | + sort($filenames); |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | return $filenames; |
| 30 | 32 | } |
@@ -33,8 +35,9 @@ discard block |
||
| 33 | 35 | { |
| 34 | 36 | $filenames = $this->filenames($regex); |
| 35 | 37 | $filepathes = []; |
| 36 | - foreach($filenames as $filename) |
|
| 37 | - $filepathes[] = $this->path_to($filename); |
|
| 38 | + foreach($filenames as $filename) { |
|
| 39 | + $filepathes[] = $this->path_to($filename); |
|
| 40 | + } |
|
| 38 | 41 | |
| 39 | 42 | return $filepathes; |
| 40 | 43 | } |
@@ -52,8 +55,9 @@ discard block |
||
| 52 | 55 | if(is_link($path)) |
| 53 | 56 | { |
| 54 | 57 | $res = readlink($path); |
| 55 | - if($res === false) |
|
| 56 | - throw new \Exception('readlink failed on '.$path); |
|
| 58 | + if($res === false) { |
|
| 59 | + throw new \Exception('readlink failed on '.$path); |
|
| 60 | + } |
|
| 57 | 61 | |
| 58 | 62 | $path = $res; |
| 59 | 63 | } |
@@ -65,8 +69,10 @@ discard block |
||
| 65 | 69 | if(self::exists($src_path) && self::is_file($src_path)) |
| 66 | 70 | { |
| 67 | 71 | $dst = new FilePath($dst_path); |
| 68 | - if(self::exists($dst->dir()) && self::is_dir($dst->dir())) |
|
| 69 | - return copy($src_path, $dst_path); // Returns TRUE on success or FALSE on failure. |
|
| 72 | + if(self::exists($dst->dir()) && self::is_dir($dst->dir())) { |
|
| 73 | + return copy($src_path, $dst_path); |
|
| 74 | + } |
|
| 75 | + // Returns TRUE on success or FALSE on failure. |
|
| 70 | 76 | |
| 71 | 77 | // vd(__FUNCTION__.'ERR: self::exists('.$dst->dir().') && self::is_dir('.$dst->dir().')'); |
| 72 | 78 | } |
@@ -102,23 +108,29 @@ discard block |
||
| 102 | 108 | $ret = false; |
| 103 | 109 | if(self::exists($src_path)) |
| 104 | 110 | { |
| 105 | - if(self::is_link($src_path) && $follow_link === true) |
|
| 106 | - $ret = self::remove(readlink($src_path)); |
|
| 107 | - elseif(self::is_file($src_path)) |
|
| 108 | - $ret = unlink($src_path); // Returns TRUE on success or FALSE on failure. |
|
| 109 | - else if(self::is_dir($src_path)) |
|
| 110 | - $ret = rmdir($src_path); // Returns TRUE on success or FALSE on failure. |
|
| 111 | + if(self::is_link($src_path) && $follow_link === true) { |
|
| 112 | + $ret = self::remove(readlink($src_path)); |
|
| 113 | + } elseif(self::is_file($src_path)) { |
|
| 114 | + $ret = unlink($src_path); |
|
| 115 | + } |
|
| 116 | + // Returns TRUE on success or FALSE on failure. |
|
| 117 | + else if(self::is_dir($src_path)) { |
|
| 118 | + $ret = rmdir($src_path); |
|
| 119 | + } |
|
| 120 | + // Returns TRUE on success or FALSE on failure. |
|
| 111 | 121 | } |
| 112 | 122 | return $ret; |
| 113 | 123 | } |
| 114 | 124 | |
| 115 | 125 | public static function preg_scandir($dir_path, $regex=null) |
| 116 | 126 | { |
| 117 | - if(!self::exists($dir_path) || !self::is_dir($dir_path)) |
|
| 118 | - return null; |
|
| 127 | + if(!self::exists($dir_path) || !self::is_dir($dir_path)) { |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 119 | 130 | |
| 120 | - if(($filenames = scandir($dir_path, SCANDIR_SORT_ASCENDING)) !== false) |
|
| 121 | - return is_null($regex)? $filenames : preg_grep($regex, $filenames); |
|
| 131 | + if(($filenames = scandir($dir_path, SCANDIR_SORT_ASCENDING)) !== false) { |
|
| 132 | + return is_null($regex)? $filenames : preg_grep($regex, $filenames); |
|
| 133 | + } |
|
| 122 | 134 | |
| 123 | 135 | throw new \Exception("directory path '$dir_path' cannot be scanned"); |
| 124 | 136 | } |
@@ -8,16 +8,18 @@ discard block |
||
| 8 | 8 | public static function identical($filepath_1, $filepath_2, $read_length = 8192) |
| 9 | 9 | { |
| 10 | 10 | //** TEST FOR EXISTENCE |
| 11 | - if(!file_exists($filepath_1) || !file_exists($filepath_2)) |
|
| 12 | - throw new \Exception('file_exists false'); |
|
| 11 | + if(!file_exists($filepath_1) || !file_exists($filepath_2)) { |
|
| 12 | + throw new \Exception('file_exists false'); |
|
| 13 | + } |
|
| 13 | 14 | |
| 14 | 15 | //** TEST FOR SYMLINK |
| 15 | 16 | $filepath_1 = self::resolve_symlink($filepath_1); |
| 16 | 17 | $filepath_2 = self::resolve_symlink($filepath_2); |
| 17 | 18 | |
| 18 | 19 | //** TEST FOR IDENTICAL TYPE AND SIZE |
| 19 | - if(filetype($filepath_1) !== filetype($filepath_2) || filesize($filepath_1) !== filesize($filepath_2)) |
|
| 20 | - return false; |
|
| 20 | + if(filetype($filepath_1) !== filetype($filepath_2) || filesize($filepath_1) !== filesize($filepath_2)) { |
|
| 21 | + return false; |
|
| 22 | + } |
|
| 21 | 23 | |
| 22 | 24 | //** TEST FOR IDENTICAL CONTENT |
| 23 | 25 | return self::compare_content($filepath_1, $filepath_2, $read_length); |
@@ -43,15 +45,14 @@ discard block |
||
| 43 | 45 | $identical = (0 === strcmp($buffer_1, $buffer_2)); |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | - if($identical === true && feof($filepointer_1) !== feof($filepointer_2)) |
|
| 47 | - $identical = false; |
|
| 48 | + if($identical === true && feof($filepointer_1) !== feof($filepointer_2)) { |
|
| 49 | + $identical = false; |
|
| 50 | + } |
|
| 48 | 51 | |
| 49 | - } |
|
| 50 | - catch (\Exception $e) |
|
| 52 | + } catch (\Exception $e) |
|
| 51 | 53 | { |
| 52 | 54 | return false; |
| 53 | - } |
|
| 54 | - finally |
|
| 55 | + } finally |
|
| 55 | 56 | { |
| 56 | 57 | $file_1->close(); |
| 57 | 58 | $file_2->close(); |
@@ -10,8 +10,9 @@ discard block |
||
| 10 | 10 | $ret = json_decode($string, $assoc, $depth, $options); |
| 11 | 11 | |
| 12 | 12 | $error = self::has_errors(); |
| 13 | - if($error !== false) |
|
| 14 | - throw new \Exception("ParsingException: $error"); |
|
| 13 | + if($error !== false) { |
|
| 14 | + throw new \Exception("ParsingException: $error"); |
|
| 15 | + } |
|
| 15 | 16 | |
| 16 | 17 | return $ret; |
| 17 | 18 | } |
@@ -21,16 +22,18 @@ discard block |
||
| 21 | 22 | $ret = json_encode($var, $options, $depth); |
| 22 | 23 | |
| 23 | 24 | $error = self::has_errors(); |
| 24 | - if($error !== false) |
|
| 25 | - throw new \Exception("ParsingException: $error"); |
|
| 25 | + if($error !== false) { |
|
| 26 | + throw new \Exception("ParsingException: $error"); |
|
| 27 | + } |
|
| 26 | 28 | |
| 27 | 29 | return $ret; |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | private static function has_errors() |
| 31 | 33 | { |
| 32 | - if(json_last_error() === JSON_ERROR_NONE) |
|
| 33 | - return false; |
|
| 34 | + if(json_last_error() === JSON_ERROR_NONE) { |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 34 | 37 | |
| 35 | 38 | return json_last_error_msg(); |
| 36 | 39 | } |
@@ -9,18 +9,22 @@ |
||
| 9 | 9 | { |
| 10 | 10 | $ret = '# INI Dump'; |
| 11 | 11 | |
| 12 | - if(is_array(current($array))) // with sections |
|
| 12 | + if(is_array(current($array))) { |
|
| 13 | + // with sections |
|
| 13 | 14 | { |
| 14 | 15 | foreach($array as $section => $data) |
| 15 | 16 | { |
| 16 | 17 | $ret.= PHP_EOL.PHP_EOL.self::section($section); |
| 17 | - foreach($data as $key => $value) |
|
| 18 | - $ret.= PHP_EOL.self::line($key, $value); |
|
| 19 | - } |
|
| 20 | 18 | } |
| 21 | - else // no section |
|
| 19 | + foreach($data as $key => $value) { |
|
| 20 | + $ret.= PHP_EOL.self::line($key, $value); |
|
| 21 | + } |
|
| 22 | + } |
|
| 23 | + } else { |
|
| 24 | + // no section |
|
| 22 | 25 | foreach($array as $key => $value) |
| 23 | 26 | $ret.= self::line($key, $value); |
| 27 | + } |
|
| 24 | 28 | |
| 25 | 29 | return $ret; |
| 26 | 30 | } |