Passed
Push — master ( b48dfc...b2a627 )
by Robin
02:30
created

NativeStream::stream_tell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 */
7
8
namespace Icewind\SMB\Native;
9
10
use Icewind\SMB\Exception\Exception;
11
use Icewind\SMB\Exception\InvalidRequestException;
12
use Icewind\Streams\File;
13
14
class NativeStream implements File {
15
	/**
16
	 * @var resource
17
	 */
18
	public $context;
19
20
	/**
21
	 * @var NativeState
22
	 */
23
	protected $state;
24
25
	/**
26
	 * @var resource
27
	 */
28
	protected $handle;
29
30
	/**
31
	 * @var bool
32
	 */
33
	protected $eof = false;
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $url;
39
40
	/**
41
	 * Wrap a stream from libsmbclient-php into a regular php stream
42
	 *
43
	 * @param \Icewind\SMB\NativeState $state
0 ignored issues
show
Bug introduced by
The type Icewind\SMB\NativeState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
	 * @param resource $smbStream
45
	 * @param string $mode
46
	 * @param string $url
47
	 * @return resource
48
	 */
49
	public static function wrap($state, $smbStream, $mode, $url) {
50
		stream_wrapper_register('nativesmb', NativeStream::class);
51
		$context = stream_context_create([
52
			'nativesmb' => [
53
				'state'  => $state,
54
				'handle' => $smbStream,
55
				'url'    => $url
56
			]
57
		]);
58
		$fh = fopen('nativesmb://', $mode, false, $context);
59
		stream_wrapper_unregister('nativesmb');
60
		return $fh;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fh could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
61
	}
62
63 78
	public function stream_close() {
64
		try {
65 78
			return $this->state->close($this->handle);
66
		} catch (\Exception $e) {
67
			return false;
68
		}
69
	}
70
71 38
	public function stream_eof() {
72 38
		return $this->eof;
73
	}
74
75 38
	public function stream_flush() {
76 38
	}
77
78
79 78
	public function stream_open($path, $mode, $options, &$opened_path) {
80 78
		$context = stream_context_get_options($this->context);
81 78
		$this->state = $context['nativesmb']['state'];
82 78
		$this->handle = $context['nativesmb']['handle'];
83 78
		$this->url = $context['nativesmb']['url'];
84 78
		return true;
85
	}
86
87 38
	public function stream_read($count) {
88 38
		$result = $this->state->read($this->handle, $count);
89 38
		if (strlen($result) < $count) {
90 38
			$this->eof = true;
91
		}
92 38
		return $result;
93
	}
94
95 4
	public function stream_seek($offset, $whence = SEEK_SET) {
96 4
		$this->eof = false;
97
		try {
98 4
			return $this->state->lseek($this->handle, $offset, $whence) !== false;
99
		} catch (InvalidRequestException $e) {
100
			return false;
101
		}
102
	}
103
104 36
	public function stream_stat() {
105
		try {
106 36
			return $this->state->stat($this->url);
107
		} catch (Exception $e) {
108
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Icewind\Streams\File::stream_stat() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
109
		}
110
	}
111
112 4
	public function stream_tell() {
113 4
		return $this->state->lseek($this->handle, 0, SEEK_CUR);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->state->lse...nd\SMB\Native\SEEK_CUR) also could return the type boolean which is incompatible with the return type mandated by Icewind\Streams\File::stream_tell() of integer.
Loading history...
114
	}
115
116
	public function stream_write($data) {
117
		return $this->state->write($this->handle, $data);
118
	}
119
120 2
	public function stream_truncate($size) {
121 2
		return $this->state->ftruncate($this->handle, $size);
122
	}
123
124 2
	public function stream_set_option($option, $arg1, $arg2) {
125 2
		return false;
126
	}
127
128 2
	public function stream_lock($operation) {
129 2
		return false;
130
	}
131
}
132