Skip to content

Commit

Permalink
add some more type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Sep 26, 2017
1 parent ee7d2ef commit 8bdc520
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 240 deletions.
6 changes: 3 additions & 3 deletions src/main/php/org/bovigo/vfs/DotDirectory.php
Expand Up @@ -18,17 +18,17 @@ class DotDirectory extends vfsStreamDirectory
*
* @return vfsStreamContainerIterator
*/
public function getIterator()
public function getIterator(): \Iterator
{
return new \ArrayIterator(array());
return new \ArrayIterator([]);
}

/**
* checks whether dir is a dot dir
*
* @return bool
*/
public function isDot()
public function isDot(): bool
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/php/org/bovigo/vfs/Quota.php
Expand Up @@ -34,7 +34,7 @@ class Quota
*
* @param int $amount quota in bytes
*/
public function __construct($amount)
public function __construct(int $amount)
{
$this->amount = $amount;
}
Expand All @@ -44,7 +44,7 @@ public function __construct($amount)
*
* @return Quota
*/
public static function unlimited()
public static function unlimited(): self
{
return new self(self::UNLIMITED);
}
Expand All @@ -54,7 +54,7 @@ public static function unlimited()
*
* @return bool
*/
public function isLimited()
public function isLimited(): bool
{
return self::UNLIMITED < $this->amount;
}
Expand All @@ -66,7 +66,7 @@ public function isLimited()
* @param int $usedSpace
* @return int
*/
public function spaceLeft($usedSpace)
public function spaceLeft(int $usedSpace): int
{
if (self::UNLIMITED === $this->amount) {
return $usedSpace;
Expand Down
14 changes: 7 additions & 7 deletions src/main/php/org/bovigo/vfs/content/FileContent.php
Expand Up @@ -20,22 +20,22 @@ interface FileContent
*
* @return string
*/
public function content();
public function content(): string;

/**
* returns size of content
*
* @return int
*/
public function size();
public function size(): int;

/**
* reads the given amount of bytes from content
*
* @param int $count
* @return string
*/
public function read($count);
public function read(int $count): string;

/**
* seeks to the given offset
Expand All @@ -44,28 +44,28 @@ public function read($count);
* @param int $whence
* @return bool
*/
public function seek($offset, $whence);
public function seek(int $offset, int $whence): bool;

/**
* checks whether pointer is at end of file
*
* @return bool
*/
public function eof();
public function eof(): bool;

/**
* writes an amount of data
*
* @param string $data
* @return amount of written bytes
*/
public function write($data);
public function write(string $data): int;

/**
* Truncates a file to a given length
*
* @param int $size length to truncate file to
* @return bool
*/
public function truncate($size);
public function truncate(int $size): bool;
}
21 changes: 11 additions & 10 deletions src/main/php/org/bovigo/vfs/content/LargeFileContent.php
Expand Up @@ -53,7 +53,7 @@ public function __construct($size)
* @param int $kilobyte
* @return LargeFileContent
*/
public static function withKilobytes($kilobyte)
public static function withKilobytes(int $kilobyte): self
{
return new self($kilobyte * 1024);
}
Expand All @@ -64,7 +64,7 @@ public static function withKilobytes($kilobyte)
* @param int $megabyte
* @return LargeFileContent
*/
public static function withMegabytes($megabyte)
public static function withMegabytes(int $megabyte): self
{
return self::withKilobytes($megabyte * 1024);
}
Expand All @@ -75,7 +75,7 @@ public static function withMegabytes($megabyte)
* @param int $gigabyte
* @return LargeFileContent
*/
public static function withGigabytes($gigabyte)
public static function withGigabytes(int $gigabyte): self
{
return self::withMegabytes($gigabyte * 1024);
}
Expand All @@ -85,7 +85,7 @@ public static function withGigabytes($gigabyte)
*
* @return string
*/
public function content()
public function content(): string
{
return $this->doRead(0, $this->size);
}
Expand All @@ -95,18 +95,19 @@ public function content()
*
* @return int
*/
public function size()
public function size(): int
{
return $this->size;
}

/**
* actual reading of given byte count starting at given offset
*
* @param int $offset
* @param int $count
* @param int $offset
* @param int $count
* @return string
*/
protected function doRead($offset, $count)
protected function doRead(int $offset, int $count): string
{
if (($offset + $count) > $this->size) {
$count = $this->size - $offset;
Expand All @@ -131,7 +132,7 @@ protected function doRead($offset, $count)
* @param int $offset
* @param int $length
*/
protected function doWrite($data, $offset, $length)
protected function doWrite(string $data, int $offset, int $length)
{
for ($i = 0; $i < $length; $i++) {
$this->content[$i + $offset] = $data{$i};
Expand All @@ -150,7 +151,7 @@ protected function doWrite($data, $offset, $length)
* @param int $size length to truncate file to
* @return bool
*/
public function truncate($size)
public function truncate(int $size): bool
{
$this->size = $size;
foreach (array_filter(array_keys($this->content),
Expand Down
26 changes: 14 additions & 12 deletions src/main/php/org/bovigo/vfs/content/SeekableFileContent.php
Expand Up @@ -28,7 +28,7 @@ abstract class SeekableFileContent implements FileContent
* @param int $count
* @return string
*/
public function read($count)
public function read(int $count): string
{
$data = $this->doRead($this->offset, $count);
$this->offset += $count;
Expand All @@ -38,10 +38,11 @@ public function read($count)
/**
* actual reading of given byte count starting at given offset
*
* @param int $offset
* @param int $count
* @param int $offset
* @param int $count
* @return string
*/
protected abstract function doRead($offset, $count);
protected abstract function doRead(int $offset, int $count): string;

/**
* seeks to the given offset
Expand All @@ -50,7 +51,7 @@ protected abstract function doRead($offset, $count);
* @param int $whence
* @return bool
*/
public function seek($offset, $whence)
public function seek(int $offset, int $whence): bool
{
$newOffset = $this->offset;
switch ($whence) {
Expand All @@ -69,10 +70,11 @@ public function seek($offset, $whence)
default:
return false;
}
if ($newOffset<0) {

if ($newOffset < 0) {
return false;
}

$this->offset = $newOffset;
return true;
}
Expand All @@ -82,7 +84,7 @@ public function seek($offset, $whence)
*
* @return bool
*/
public function eof()
public function eof(): bool
{
return $this->size() <= $this->offset;
}
Expand All @@ -93,7 +95,7 @@ public function eof()
* @param string $data
* @return amount of written bytes
*/
public function write($data)
public function write(string $data): int
{
$dataLength = strlen($data);
$this->doWrite($data, $this->offset, $dataLength);
Expand All @@ -108,15 +110,15 @@ public function write($data)
* @param int $offset
* @param int $length
*/
protected abstract function doWrite($data, $offset, $length);
protected abstract function doWrite(string $data, int $offset, int $length);

/**
* for backwards compatibility with vfsStreamFile::bytesRead()
*
* @return int
* @deprecated
*/
public function bytesRead()
public function bytesRead(): int
{
return $this->offset;
}
Expand All @@ -127,7 +129,7 @@ public function bytesRead()
* @return string
* @deprecated
*/
public function readUntilEnd()
public function readUntilEnd(): string
{
return substr($this->content(), $this->offset);
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/php/org/bovigo/vfs/content/StringBasedFileContent.php
Expand Up @@ -27,7 +27,7 @@ class StringBasedFileContent extends SeekableFileContent implements FileContent
*
* @param string $content
*/
public function __construct($content)
public function __construct(string $content)
{
$this->content = $content;
}
Expand All @@ -37,7 +37,7 @@ public function __construct($content)
*
* @return string
*/
public function content()
public function content(): string
{
return $this->content;
}
Expand All @@ -47,18 +47,19 @@ public function content()
*
* @return int
*/
public function size()
public function size(): int
{
return strlen($this->content);
}

/**
* actual reading of length starting at given offset
*
* @param int $offset
* @param int $count
* @param int $offset
* @param int $count
* @return string
*/
protected function doRead($offset, $count)
protected function doRead(int $offset, int $count): string
{
return substr($this->content, $offset, $count);
}
Expand All @@ -70,7 +71,7 @@ protected function doRead($offset, $count)
* @param int $offset
* @param int $length
*/
protected function doWrite($data, $offset, $length)
protected function doWrite(string $data, int $offset, int $length)
{
$this->content = substr($this->content, 0, $offset)
. $data
Expand All @@ -83,7 +84,7 @@ protected function doWrite($data, $offset, $length)
* @param int $size length to truncate file to
* @return bool
*/
public function truncate($size)
public function truncate(int $size): bool
{
if ($size > $this->size()) {
// Pad with null-chars if we're "truncating up"
Expand Down

0 comments on commit 8bdc520

Please sign in to comment.