more type hints

This commit is contained in:
Robin Appelman 2021-03-09 18:01:42 +01:00
commit e9f6d00a93
28 changed files with 343 additions and 530 deletions

View file

@ -8,9 +8,16 @@
namespace Icewind\SMB\Native;
use Icewind\SMB\AbstractShare;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\AuthenticationException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\InvalidHostException;
use Icewind\SMB\Exception\InvalidPathException;
use Icewind\SMB\Exception\InvalidResourceException;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\IFileInfo;
use Icewind\SMB\INotifyHandler;
use Icewind\SMB\IServer;
use Icewind\SMB\Wrapped\Server;
@ -32,22 +39,18 @@ class NativeShare extends AbstractShare {
*/
private $state;
/**
* @param IServer $server
* @param string $name
*/
public function __construct($server, $name) {
public function __construct(IServer $server, string $name) {
parent::__construct();
$this->server = $server;
$this->name = $name;
}
/**
* @throws \Icewind\SMB\Exception\ConnectionException
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
* @throws ConnectionException
* @throws AuthenticationException
* @throws InvalidHostException
*/
protected function getState() {
protected function getState(): NativeState {
if ($this->state and $this->state instanceof NativeState) {
return $this->state;
}
@ -62,11 +65,11 @@ class NativeShare extends AbstractShare {
*
* @return string
*/
public function getName() {
public function getName(): string {
return $this->name;
}
private function buildUrl($path) {
private function buildUrl(string $path): string {
$this->verifyPath($path);
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
if ($path) {
@ -81,16 +84,16 @@ class NativeShare extends AbstractShare {
* List the content of a remote folder
*
* @param string $path
* @return \Icewind\SMB\IFileInfo[]
* @return IFileInfo[]
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function dir($path) {
public function dir(string $path): array {
$files = [];
$dh = $this->getState()->opendir($this->buildUrl($path));
while ($file = $this->getState()->readdir($dh)) {
while ($file = $this->getState()->readdir($dh, $path)) {
$name = $file['name'];
if ($name !== '.' and $name !== '..') {
$fullPath = $path . '/' . $name;
@ -98,15 +101,15 @@ class NativeShare extends AbstractShare {
}
}
$this->getState()->closedir($dh);
$this->getState()->closedir($dh, $path);
return $files;
}
/**
* @param string $path
* @return \Icewind\SMB\IFileInfo
* @return IFileInfo
*/
public function stat($path) {
public function stat(string $path): IFileInfo {
$info = new NativeFileInfo($this, $path, self::mb_basename($path));
// trigger attribute loading
@ -122,7 +125,7 @@ class NativeShare extends AbstractShare {
* @link http://php.net/manual/en/function.basename.php#121405
* @return string
*/
protected static function mb_basename($path) {
protected static function mb_basename(string $path): string {
if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
return $matches[1];
} elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
@ -138,10 +141,10 @@ class NativeShare extends AbstractShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
* @throws NotFoundException
* @throws AlreadyExistsException
*/
public function mkdir($path) {
public function mkdir(string $path): bool {
return $this->getState()->mkdir($this->buildUrl($path));
}
@ -151,10 +154,10 @@ class NativeShare extends AbstractShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function rmdir($path) {
public function rmdir(string $path): bool {
return $this->getState()->rmdir($this->buildUrl($path));
}
@ -164,10 +167,10 @@ class NativeShare extends AbstractShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function del($path) {
public function del(string $path): bool {
return $this->getState()->unlink($this->buildUrl($path));
}
@ -178,10 +181,10 @@ class NativeShare extends AbstractShare {
* @param string $to
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
* @throws NotFoundException
* @throws AlreadyExistsException
*/
public function rename($from, $to) {
public function rename(string $from, string $to): bool{
return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
}
@ -192,10 +195,10 @@ class NativeShare extends AbstractShare {
* @param string $target remove file
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function put($source, $target) {
public function put(string $source, string $target): bool {
$sourceHandle = fopen($source, 'rb');
$targetUrl = $this->buildUrl($target);
@ -215,20 +218,18 @@ class NativeShare extends AbstractShare {
* @param string $target local file
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws \Icewind\SMB\Exception\InvalidPathException
* @throws \Icewind\SMB\Exception\InvalidResourceException
* @throws AuthenticationException
* @throws ConnectionException
* @throws InvalidHostException
* @throws InvalidPathException
* @throws InvalidResourceException
*/
public function get($source, $target) {
public function get(string $source, string $target): bool {
if (!$target) {
throw new InvalidPathException('Invalid target path: Filename cannot be empty');
}
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
if (!$sourceHandle) {
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
}
$targetHandle = @fopen($target, 'wb');
if (!$targetHandle) {
@ -242,7 +243,7 @@ class NativeShare extends AbstractShare {
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
}
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE, $source)) {
fwrite($targetHandle, $data);
}
$this->getState()->close($sourceHandle, $this->buildUrl($source));
@ -255,10 +256,10 @@ class NativeShare extends AbstractShare {
* @param string $source
* @return resource a read only stream with the contents of the remote file
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function read($source) {
public function read(string $source) {
$url = $this->buildUrl($source);
$handle = $this->getState()->open($url, 'r');
return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
@ -271,10 +272,10 @@ class NativeShare extends AbstractShare {
* @param string $source
* @return resource a writeable stream
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function write($source) {
public function write(string $source) {
$url = $this->buildUrl($source);
$handle = $this->getState()->create($url);
return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
@ -286,10 +287,10 @@ class NativeShare extends AbstractShare {
* @param string $source
* @return resource a writeable stream
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws NotFoundException
* @throws InvalidTypeException
*/
public function append($source) {
public function append(string $source) {
$url = $this->buildUrl($source);
$handle = $this->getState()->open($url, "a+");
return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
@ -302,7 +303,7 @@ class NativeShare extends AbstractShare {
* @param string $attribute attribute to get the info
* @return string the attribute value
*/
public function getAttribute($path, $attribute) {
public function getAttribute(string $path, string $attribute): string {
return $this->getState()->getxattr($this->buildUrl($path), $attribute);
}
@ -314,7 +315,7 @@ class NativeShare extends AbstractShare {
* @param string|int $value
* @return mixed the attribute value
*/
public function setAttribute($path, $attribute, $value) {
public function setAttribute(string $path, string $attribute, $value) {
if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
$value = '0x' . dechex($value);
}
@ -329,7 +330,7 @@ class NativeShare extends AbstractShare {
* @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
* @return mixed
*/
public function setMode($path, $mode) {
public function setMode(string $path, int $mode) {
return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
}
@ -340,7 +341,7 @@ class NativeShare extends AbstractShare {
* @param string $path
* @return INotifyHandler
*/
public function notify($path) {
public function notify(string $path): INotifyHandler {
// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
// so we use the smbclient based backend for this
if (!Server::available($this->server->getSystem())) {