NativeWriteStream   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 14.63 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.8%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 12
loc 82
ccs 36
cts 41
cp 0.878
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A stream_open() 0 3 1
A wrap() 0 3 1
A stream_seek() 12 12 3
A flushWrite() 0 3 1
A stream_write() 0 10 2
A stream_close() 0 9 3
A stream_tell() 0 3 1
A stream_read() 0 3 1
A stream_truncate() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\StringBuffer;
11
12
/**
13
 * Stream optimized for write only usage
14
 */
15
class NativeWriteStream extends NativeStream {
16
	const CHUNK_SIZE = 1048576; // 1MB chunks
17
18
	/** @var StringBuffer */
19
	private $writeBuffer;
20
21
	/** @var int */
22
	private $pos = 0;
23
24 42
	public function __construct() {
25 42
		$this->writeBuffer = new StringBuffer();
26
	}
27 42
28
	public function stream_open($path, $mode, $options, &$opened_path): bool {
29
		return parent::stream_open($path, $mode, $options, $opened_path);
30
	}
31
32
	/**
33
	 * Wrap a stream from libsmbclient-php into a regular php stream
34
	 *
35
	 * @param NativeState $state
36
	 * @param resource $smbStream
37
	 * @param string $mode
38
	 * @param string $url
39 42
	 * @return resource
40 42
	 */
41 42
	public static function wrap(NativeState $state, $smbStream, string $mode, string $url) {
42
		return parent::wrapClass($state, $smbStream, $mode, $url, NativeWriteStream::class);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (wrapClass() instead of wrap()). Are you sure this is correct? If so, you might want to change this to $this->wrapClass().

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...
43 42
	}
44 42
45 42 View Code Duplication
	public function stream_seek($offset, $whence = SEEK_SET) {
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...
46
		$this->flushWrite();
47
		$result = parent::stream_seek($offset, $whence);
48 42
		if ($result) {
49 42
			$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...
50 42
			if ($pos === false) {
51
				return false;
52
			}
53 2
			$this->pos = $pos;
54 2
		}
55 2
		return $result;
56 2
	}
57 2
58
	private function flushWrite(): void {
59 2
		parent::stream_write($this->writeBuffer->flush());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (stream_write() instead of flushWrite()). Are you sure this is correct? If so, you might want to change this to $this->stream_write().

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...
60
	}
61
62 42
	public function stream_write($data) {
63 42
		$written = $this->writeBuffer->push($data);
64 42
		$this->pos += $written;
65 42
66 42
		if ($this->writeBuffer->remaining() >= self::CHUNK_SIZE) {
67 42
			$this->flushWrite();
68
		}
69 38
70 38
		return $written;
71 38
	}
72 38
73
	public function stream_close() {
74 38
		try {
75
			$this->flushWrite();
76
			$flushResult = true;
77
		} catch (\Exception $e) {
78 38
			$flushResult = false;
79
		}
80
		return parent::stream_close() && $flushResult;
81 42
	}
82
83 42
	public function stream_tell() {
84 42
		return $this->pos;
85
	}
86
87
	public function stream_read($count) {
88 42
		return false;
89
	}
90
91 2
	public function stream_truncate($size) {
92 2
		$this->flushWrite();
93
		$this->pos = $size;
94
		return parent::stream_truncate($size);
95
	}
96
}
97