Skip to content

Commit 58c3b0c

Browse files
committed
Switch from mixed return to Exception
1 parent ce9e4a4 commit 58c3b0c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Filename.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,19 @@ public function delete() {
133133
/**
134134
* @param string $newName
135135
*
136-
* @return Filename|false
136+
* @return Filename
137+
* @throws FilenameException
137138
*/
138139
public function rename($newName) {
139140
$newNameFile = $this->getDirectory()->file($newName);
140141
$result = @rename($this->asString(), $newNameFile->asString());
141142
if ($result === false) {
142-
return false;
143+
$lastError = error_get_last();
144+
$nativeError = new \RuntimeException(
145+
sprintf('%s (line %d): %s', $lastError['file'], $lastError['line'], $lastError['message']),
146+
$lastError['type']
147+
);
148+
throw new FilenameException('Unable to rename the file.', 0, $nativeError);
143149
}
144150
return $newNameFile;
145151
}

tests/FilenameTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public function testFailedRename() {
106106

107107
$this->assertFalse($filename->exists());
108108
$this->assertTrue($filename->isWritable());
109+
$this->expectException(FilenameException::class);
110+
$this->expectExceptionMessage('Unable to rename the file.');
109111

110112
try {
111113
touch($filename->asString());
@@ -116,8 +118,7 @@ public function testFailedRename() {
116118
$mode = fileperms($filename->getDirectory());
117119
chmod($filename->getDirectory(), 0000);
118120

119-
$renamed = $filename->rename('bar2.txt');
120-
$this->assertFalse($renamed);
121+
$filename->rename('bar2.txt');
121122
} finally {
122123
if (isset($mode)) {
123124
chmod($filename->getDirectory(), $mode);

0 commit comments

Comments
 (0)