Completed
Push — stable3.0 ( bd43cd...55b7d1 )
by Robin
08:57 queued 07:06
created

NativeWriteStream::wrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1.0007

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 10
cts 11
cp 0.9091
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1.0007
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 20
	public function stream_open($path, $mode, $options, &$opened_path) {
25 20
		$this->writeBuffer = fopen('php://memory', 'r+');
26
27 20
		return parent::stream_open($path, $mode, $options, $opened_path);
28
29
	}
30
31
	/**
32
	 * Wrap a stream from libsmbclient-php into a regular php stream
33
	 *
34
	 * @param \Icewind\SMB\NativeState $state
35
	 * @param resource $smbStream
36
	 * @param string $mode
37
	 * @param string $url
38
	 * @return resource
39
	 */
40 20 View Code Duplication
	public static function wrap($state, $smbStream, $mode, $url) {
41 20
		stream_wrapper_register('nativesmb', NativeWriteStream::class);
42 20
		$context = stream_context_create(array(
43
			'nativesmb' => array(
44 20
				'state'  => $state,
45 20
				'handle' => $smbStream,
46
				'url'    => $url
47 20
			)
48 20
		));
49 20
		$fh = fopen('nativesmb://', $mode, false, $context);
50 20
		stream_wrapper_unregister('nativesmb');
51 20
		return $fh;
52
	}
53
54
	public function stream_seek($offset, $whence = SEEK_SET) {
55
		$this->flushWrite();
56
		$result = parent::stream_seek($offset, $whence);
57
		if ($result) {
58
			$this->pos = parent::stream_tell();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (stream_tell() instead of stream_seek()). Are you sure this is correct? If so, you might want to change this to $this->stream_tell().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Documentation Bug introduced by
It seems like parent::stream_tell() can also be of type boolean. However, the property $pos is declared as type integer. 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...
59
		}
60
		return $result;
61
	}
62
63 20
	private function flushWrite() {
64 20
		rewind($this->writeBuffer);
65 20
		$this->state->write($this->handle, stream_get_contents($this->writeBuffer));
66 20
		$this->writeBuffer = fopen('php://memory', 'r+');
67 20
		$this->bufferSize = 0;
68 20
	}
69
70 18
	public function stream_write($data) {
71 18
		$written = fwrite($this->writeBuffer, $data);
72 18
		$this->bufferSize += $written;
73 18
		$this->pos += $written;
74
75 18
		if ($this->bufferSize >= self::CHUNK_SIZE) {
76
			$this->flushWrite();
77
		}
78
79 18
		return $written;
80
	}
81
82 20
	public function stream_close() {
83 20
		$this->flushWrite();
84 20
		return parent::stream_close();
85
	}
86
87
	public function stream_tell() {
88
		return $this->pos;
89
	}
90
91
	public function stream_read($count) {
92
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Icewind\Streams\File::stream_read of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93
	}
94
95 1
	public function stream_truncate($size) {
96 1
		$this->flushWrite();
97 1
		$this->pos = $size;
98 1
		return parent::stream_truncate($size);
99
	}
100
}
101