From 5a69fca406ac5fd220de0aa68c887bc8046eb93c Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 24 Dec 2018 12:10:57 +0100 Subject: [PATCH 1/2] Do not use the last error if it is null --- lib/BlockingDriver.php | 44 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/BlockingDriver.php b/lib/BlockingDriver.php index 265a7c8..1586d7e 100644 --- a/lib/BlockingDriver.php +++ b/lib/BlockingDriver.php @@ -94,9 +94,12 @@ class BlockingDriver implements Driver } if (($size = @\filesize($path)) === false) { - return new Failure(new FilesystemException( - \error_get_last()["message"] - )); + $message = 'Could not open the file.'; + if ($error = \error_get_last()) { + $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); + } + + return new Failure(new FilesystemException($message)); } \clearstatcache(true, $path); @@ -328,15 +331,21 @@ class BlockingDriver implements Driver public function chown(string $path, int $uid, int $gid): Promise { if ($uid !== -1 && !@\chown($path, $uid)) { - return new Failure(new FilesystemException( - \error_get_last()["message"] - )); + $message = 'Could not open the file.'; + if ($error = \error_get_last()) { + $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); + } + + return new Failure(new FilesystemException($message)); } if ($gid !== -1 && !@\chgrp($path, $gid)) { - return new Failure(new FilesystemException( - \error_get_last()["message"] - )); + $message = 'Could not open the file.'; + if ($error = \error_get_last()) { + $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); + } + + return new Failure(new FilesystemException($message)); } return new Success; @@ -358,8 +367,14 @@ class BlockingDriver implements Driver public function get(string $path): Promise { $result = @\file_get_contents($path); + if ($result === false) { + $message = 'Could not open the file.'; + if ($error = \error_get_last()) { + $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); + } + } return ($result === false) - ? new Failure(new FilesystemException(\error_get_last()["message"])) + ? new Failure(new FilesystemException($message)) : new Success($result); } @@ -369,8 +384,15 @@ class BlockingDriver implements Driver public function put(string $path, string $contents): Promise { $result = @\file_put_contents($path, $contents); + if ($result === false) { + $message = 'Could not open the file.'; + if ($error = \error_get_last()) { + $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); + } + } + return ($result === false) - ? new Failure(new FilesystemException(\error_get_last()["message"])) + ? new Failure(new FilesystemException($message)) : new Success($result); } } From 1fb69a836aca37bf1aa106d775535e8b5f6142a9 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 24 Dec 2018 13:51:42 +0100 Subject: [PATCH 2/2] Do not use ternary operator when not needed --- lib/BlockingDriver.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/BlockingDriver.php b/lib/BlockingDriver.php index 1586d7e..831c66e 100644 --- a/lib/BlockingDriver.php +++ b/lib/BlockingDriver.php @@ -372,10 +372,9 @@ class BlockingDriver implements Driver if ($error = \error_get_last()) { $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); } + return new Failure(new FilesystemException($message)); } - return ($result === false) - ? new Failure(new FilesystemException($message)) - : new Success($result); + return new Success($result); } /** @@ -389,10 +388,8 @@ class BlockingDriver implements Driver if ($error = \error_get_last()) { $message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]); } + return new Failure(new FilesystemException($message)); } - - return ($result === false) - ? new Failure(new FilesystemException($message)) - : new Success($result); + return new Success($result); } }