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

NativeStream::stream_flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
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
44
	 * @param resource $smbStream
45
	 * @param string $mode
46
	 * @param string $url
47
	 * @return resource
48
	 */
49 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...
50
		stream_wrapper_register('nativesmb', NativeStream::class);
51
		$context = stream_context_create(array(
52
			'nativesmb' => array(
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;
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);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->state->lseek($this->handle, 0, SEEK_CUR); of type integer|boolean adds the type boolean to the return on line 109 which is incompatible with the return type declared by the interface Icewind\Streams\File::stream_tell of type integer.
Loading history...
110
	}
111
112
	public function stream_write($data) {
113
		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