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

NativeWriteStream::flushWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 5
cts 5
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
/**
11
 * Stream optimized for write only usage
12
 */
13
class NativeWriteStream extends NativeStream {
14
	const CHUNK_SIZE = 1048576; // 1MB chunks
15
	/**
16
	 * @var resource
17
	 */
18
	private $writeBuffer = null;
19
20
	private $bufferSize = 0;
21
22
	private $pos = 0;
23
24 42
	public function stream_open($path, $mode, $options, &$opened_path) {
25 42
		$this->writeBuffer = fopen('php://memory', 'r+');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen('php://memory', 'r+') can also be of type false. However, the property $writeBuffer is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
26
27 42
		return parent::stream_open($path, $mode, $options, $opened_path);
28
	}
29
30
	/**
31
	 * Wrap a stream from libsmbclient-php into a regular php stream
32
	 *
33
	 * @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...
34
	 * @param resource $smbStream
35
	 * @param string $mode
36
	 * @param string $url
37
	 * @return resource
38
	 */
39 42
	public static function wrap($state, $smbStream, $mode, $url) {
40 42
		stream_wrapper_register('nativesmb', NativeWriteStream::class);
41 42
		$context = stream_context_create([
42
			'nativesmb' => [
43 42
				'state'  => $state,
44 42
				'handle' => $smbStream,
45 42
				'url'    => $url
46
			]
47
		]);
48 42
		$fh = fopen('nativesmb://', $mode, false, $context);
49 42
		stream_wrapper_unregister('nativesmb');
50 42
		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...
51
	}
52
53 2
	public function stream_seek($offset, $whence = SEEK_SET) {
54 2
		$this->flushWrite();
55 2
		$result = parent::stream_seek($offset, $whence);
56 2
		if ($result) {
57 2
			$this->pos = parent::stream_tell();
58
		}
59 2
		return $result;
60
	}
61
62 42
	private function flushWrite() {
63 42
		rewind($this->writeBuffer);
64 42
		$this->state->write($this->handle, stream_get_contents($this->writeBuffer));
65 42
		$this->writeBuffer = fopen('php://memory', 'r+');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen('php://memory', 'r+') can also be of type false. However, the property $writeBuffer is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66 42
		$this->bufferSize = 0;
67 42
	}
68
69 38
	public function stream_write($data) {
70 38
		$written = fwrite($this->writeBuffer, $data);
71 38
		$this->bufferSize += $written;
72 38
		$this->pos += $written;
73
74 38
		if ($this->bufferSize >= self::CHUNK_SIZE) {
75
			$this->flushWrite();
76
		}
77
78 38
		return $written;
79
	}
80
81 42
	public function stream_close() {
82
		try {
83 42
			$this->flushWrite();
84 42
			$flushResult = true;
85
		} catch (\Exception $e) {
86
			$flushResult = false;
87
		}
88 42
		return parent::stream_close() && $flushResult;
89
	}
90
91 2
	public function stream_tell() {
92 2
		return $this->pos;
93
	}
94
95
	public function stream_read($count) {
96
		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_read() of string.

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...
97
	}
98
99 2
	public function stream_truncate($size) {
100 2
		$this->flushWrite();
101 2
		$this->pos = $size;
102 2
		return parent::stream_truncate($size);
103
	}
104
}
105