Permalink
Browse files

added PHP 7.1 scalar and return type hints

  • Loading branch information...
dg committed Feb 5, 2019
1 parent 282908f commit cc6a1af61f90735bbae91969cf9ad31ede27c108
Showing with 14 additions and 38 deletions.
  1. +14 −38 src/SafeStream/SafeStream.php
@@ -46,9 +46,8 @@ class SafeStream
/**
* Registers protocol 'nette.safe://'.
* @return bool
*/
public static function register()
public static function register(): bool
{
foreach (array_intersect(stream_get_wrappers(), ['safe', self::PROTOCOL]) as $name) {
stream_wrapper_unregister($name);
@@ -60,12 +59,8 @@ public static function register()
/**
* Opens file.
* @param string file name with stream protocol
* @param string mode - see fopen()
* @param int STREAM_USE_PATH, STREAM_REPORT_ERRORS
* @return bool true on success or false on failure
*/
public function stream_open($path, $mode, $options)
public function stream_open(string $path, string $mode, int $options): bool
{
$path = substr($path, strpos($path, ':') + 3); // trim protocol nette.safe://
@@ -130,9 +125,8 @@ public function stream_open($path, $mode, $options)
/**
* Checks handle and locks file.
* @return bool
*/
private function checkAndLock($handle, $lock)
private function checkAndLock($handle, int $lock): bool
{
if (!$handle) {
return false;
@@ -149,7 +143,7 @@ private function checkAndLock($handle, $lock)
/**
* Error destructor.
*/
private function clean()
private function clean(): void
{
flock($this->handle, LOCK_UN);
fclose($this->handle);
@@ -165,9 +159,8 @@ private function clean()
/**
* Closes file.
* @return void
*/
public function stream_close()
public function stream_close(): void
{
if (!$this->tempFile) { // 'r' mode
flock($this->tempHandle, LOCK_UN);
@@ -190,21 +183,17 @@ public function stream_close()
/**
* Reads up to length bytes from the file.
* @param int length
* @return string
*/
public function stream_read($length)
public function stream_read(int $length)
{
return fread($this->tempHandle, $length);
}
/**
* Writes the string to the file.
* @param string data to write
* @return int number of bytes that were successfully stored
*/
public function stream_write($data)
public function stream_write(string $data): int
{
$len = strlen($data);
$res = fwrite($this->tempHandle, $data, $len);
@@ -219,50 +208,42 @@ public function stream_write($data)
/**
* Truncates a file to a given length.
* @param int The size to truncate to.
* @return bool
*/
public function stream_truncate($size)
public function stream_truncate(int $size): bool
{
return ftruncate($this->tempHandle, $size);
}
/**
* Returns the position of the file.
* @return int
*/
public function stream_tell()
public function stream_tell(): int
{
return ftell($this->tempHandle);
}
/**
* Returns true if the file pointer is at end-of-file.
* @return bool
*/
public function stream_eof()
public function stream_eof(): bool
{
return feof($this->tempHandle);
}
/**
* Sets the file position indicator for the file.
* @param int position
* @param int see fseek()
* @return int Return true on success
*/
public function stream_seek($offset, $whence)
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
{
return fseek($this->tempHandle, $offset, $whence) === 0; // ???
return fseek($this->tempHandle, $offset, $whence) === 0;
}
/**
* Gets information about a file referenced by $this->tempHandle.
* @return array
*/
public function stream_stat()
{
@@ -272,11 +253,8 @@ public function stream_stat()
/**
* Gets information about a file referenced by filename.
* @param string file name
* @param int STREAM_URL_STAT_LINK, STREAM_URL_STAT_QUIET
* @return array
*/
public function url_stat($path, $flags)
public function url_stat(string $path, int $flags)
{
// This is not thread safe
$path = substr($path, strpos($path, ':') + 3);
@@ -287,10 +265,8 @@ public function url_stat($path, $flags)
/**
* Deletes a file.
* On Windows unlink is not allowed till file is opened
* @param string file name with stream protocol
* @return bool true on success or false on failure
*/
public function unlink($path)
public function unlink(string $path): bool
{
$path = substr($path, strpos($path, ':') + 3);
return unlink($path);

0 comments on commit cc6a1af

Please sign in to comment.