Completed
Push — master ( 228057...4623d7 )
by Robin
05:13
created

NativeReadStream::stream_read()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 1
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 read only usage
12
 */
13
class NativeReadStream extends NativeStream {
14
	const CHUNK_SIZE = 1048576; // 1MB chunks
15
	/**
16
	 * @var resource
17
	 */
18
	private $readBuffer = null;
19
20
	private $bufferSize = 0;
21
22
	private $pos = 0;
23
24
	public function stream_open($path, $mode, $options, &$opened_path) {
25
		$this->readBuffer = fopen('php://memory', 'r+');
26
27
		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 View Code Duplication
	public static function wrap($state, $smbStream, $mode, $url) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
		stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeReadStream');
42
		$context = stream_context_create(array(
43
			'nativesmb' => array(
44
				'state'  => $state,
45
				'handle' => $smbStream,
46
				'url'    => $url
47
			)
48
		));
49
		$fh = fopen('nativesmb://', $mode, false, $context);
50
		stream_wrapper_unregister('nativesmb');
51
		return $fh;
52
	}
53
54
	public function stream_read($count) {
55
		// php reads 8192 bytes at once
56
		// however due to network latency etc, it's faster to read in larger chunks
57
		// and buffer the result
58
		if (!parent::stream_eof() && $this->bufferSize < $count) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (stream_eof() instead of stream_read()). Are you sure this is correct? If so, you might want to change this to $this->stream_eof().

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...
59
			$remaining = $this->readBuffer;
60
			$this->readBuffer = fopen('php://memory', 'r+');
61
			$this->bufferSize = 0;
62
			stream_copy_to_stream($remaining, $this->readBuffer);
63
			$this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE));
64
			fseek($this->readBuffer, 0);
65
		}
66
67
		$result = fread($this->readBuffer, $count);
68
		$this->bufferSize -= $count;
69
70
		$read = strlen($result);
71
		$this->pos += $read;
72
73
		return $result;
74
	}
75
76
	public function stream_seek($offset, $whence = SEEK_SET) {
77
		$result = parent::stream_seek($offset, $whence);
78
		if ($result) {
79
			$this->readBuffer = fopen('php://memory', 'r+');
80
			$this->bufferSize = 0;
81
			$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...
82
		}
83
		return $result;
84
	}
85
86
	public function stream_eof() {
87
		return $this->bufferSize <= 0 && parent::stream_eof();
88
	}
89
90
	public function stream_tell() {
91
		return $this->pos;
92
	}
93
94
	public function stream_write($data) {
95
		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_write of type integer.

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...
96
	}
97
98
	public function stream_truncate($size) {
99
		return false;
100
	}
101
}
102