Completed
Push — master ( c1afd7...fa4918 )
by Robin
03:41
created

NativeStream   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.49%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 114
ccs 43
cts 47
cp 0.9149
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A wrap() 0 13 1
A stream_close() 0 3 1
A stream_eof() 0 3 1
A stream_flush() 0 2 1
A stream_open() 0 7 1
A stream_read() 0 7 2
A stream_seek() 0 8 2
A stream_tell() 0 3 1
A stream_write() 0 3 1
A stream_truncate() 0 3 1
A stream_set_option() 0 3 1
A stream_lock() 0 3 1
A stream_stat() 0 7 2
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;
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 \Icewind\SMB\NativeState
22
	 */
23
	private $state;
24
25
	/**
26
	 * @var resource
27
	 */
28
	private $handle;
29
30
	/**
31
	 * @var bool
32
	 */
33
	private $eof = false;
34
35
	/**
36
	 * @var string
37
	 */
38
	private $url;
39
40
	/**
41
	 * Wrap a stream from libsmbclient-php into a regular php stream
42
	 *
43
	 * @param \Icewind\SMB\NativeState $state
44
	 * @param resource $smbStream
45
	 * @param string $mode
46
	 * @param string $url
47
	 * @return resource
48
	 */
49 38
	public static function wrap($state, $smbStream, $mode, $url) {
50 38
		stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
51 38
		$context = stream_context_create(array(
52
			'nativesmb' => array(
53 38
				'state' => $state,
54 38
				'handle' => $smbStream,
55
				'url' => $url
56 38
			)
57 38
		));
58 38
		$fh = fopen('nativesmb://', $mode, false, $context);
59 38
		stream_wrapper_unregister('nativesmb');
60 38
		return $fh;
61
	}
62
63 38
	public function stream_close() {
64 38
		return $this->state->close($this->handle);
65
	}
66
67 19
	public function stream_eof() {
68 19
		return $this->eof;
69
	}
70
71 38
	public function stream_flush() {
72 38
	}
73
74
75 38
	public function stream_open($path, $mode, $options, &$opened_path) {
76 38
		$context = stream_context_get_options($this->context);
77 38
		$this->state = $context['nativesmb']['state'];
78 38
		$this->handle = $context['nativesmb']['handle'];
79 38
		$this->url = $context['nativesmb']['url'];
80 38
		return true;
81
	}
82
83 19
	public function stream_read($count) {
84 19
		$result = $this->state->read($this->handle, $count);
85 19
		if (strlen($result) < $count) {
86 19
			$this->eof = true;
87 19
		}
88 19
		return $result;
89
	}
90
91 1
	public function stream_seek($offset, $whence = SEEK_SET) {
92 1
		$this->eof = false;
93
		try {
94 1
			return $this->state->lseek($this->handle, $offset, $whence) !== false;
95
		} catch (InvalidRequestException $e) {
96
			return false;
97
		}
98
	}
99
100 18
	public function stream_stat() {
101
		try {
102 18
			return $this->state->stat($this->url);
103
		} catch (Exception $e) {
104
			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_stat of type array.

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...
105
		}
106
	}
107
108 1
	public function stream_tell() {
109 1
		return $this->state->lseek($this->handle, 0, SEEK_CUR);
110
	}
111
112 18
	public function stream_write($data) {
113 18
		return $this->state->write($this->handle, $data);
114
	}
115
116 1
	public function stream_truncate($size) {
117 1
		return $this->state->ftruncate($this->handle, $size);
118
	}
119
120 1
	public function stream_set_option($option, $arg1, $arg2) {
121 1
		return false;
122
	}
123
124 1
	public function stream_lock($operation) {
125 1
		return false;
126
	}
127
}
128