Passed
Push — master ( 1f33ad...fba3db )
by Petr
08:38
created

TStreamToPos::addStreamToPosition()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.7998

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
nc 13
nop 3
dl 0
loc 52
rs 7.6666
c 1
b 0
f 0
ccs 20
cts 25
cp 0.8
crap 10.7998

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\kw_files\Traits;
4
5
6
use kalanis\kw_files\FilesException;
7
8
9
/**
10
 * Trait TStreamToPos
11
 * @package kalanis\kw_menu\Traits
12
 * Write stream at expected position
13
 */
14
trait TStreamToPos
15
{
16
    use TLang;
17
18
    /**
19
     * @param resource $original stream with original content, usually file handler
20
     * @param resource $added stream with what will be added, also file handler
21
     * @param int|null $offset where it will be added
22
     * @throws FilesException
23
     * @return resource
24
     */
25 12
    public function addStreamToPosition($original, $added, ?int $offset = null)
26
    {
27 12
        if (!is_null($offset)) {
28
            // put it somewhere, left the rest intact
29 10
            $originalLength = fstat($original)['size'];
30 10
            $addedLength = fstat($added)['size'];
31
            // original data in the beginning
32 10
            $destination = fopen('php://temp', 'rb+'); // the target storage
33 10
            if ((0 !== $offset) && (false === stream_copy_to_stream($original, $destination, $offset, 0))) {
34
                // @codeCoverageIgnoreStart
35
                throw new FilesException($this->getLang()->flCannotWriteFile('temp'));
36
            }
37
            // @codeCoverageIgnoreEnd
38
39
            // filler - when the size is too much
40 10
            if ($originalLength < $offset) {
41 6
                $begin = fopen('php://temp', 'rb+');
42 6
                /** @scrutinizer ignore-unhandled */@fwrite($begin, str_repeat("\0", intval(abs($offset - $originalLength))));
43 6
                /** @scrutinizer ignore-unhandled */@rewind($begin);
44 6
                if (false === stream_copy_to_stream($begin, $destination, -1, 0)) {
45
                    // @codeCoverageIgnoreStart
46
                    throw new FilesException($this->getLang()->flCannotWriteFile('temp'));
47
                }
48
                // @codeCoverageIgnoreEnd
49 6
                $originalLength += fstat($begin)['size'];
50
            }
51
52
            // amended data itself
53 10
            if (false === stream_copy_to_stream($added, $destination, -1, 0)) {
54
                // @codeCoverageIgnoreStart
55
                throw new FilesException($this->getLang()->flCannotWriteFile('temp'));
56
            }
57
            // @codeCoverageIgnoreEnd
58
59
            // the rest from the original
60 10
            if ($originalLength > $offset + $addedLength) {
61 4
                $rest = fopen('php://temp', 'rb+'); // the temporary storage for the end
62 4
                if (false === stream_copy_to_stream($original, $rest, -1, $offset + $addedLength)) {
63
                    // @codeCoverageIgnoreStart
64
                    throw new FilesException($this->getLang()->flCannotWriteFile('temp'));
65
                }
66 4
                /** @scrutinizer ignore-unhandled */@rewind($rest);
67
                // @codeCoverageIgnoreEnd
68 4
                if (false === stream_copy_to_stream($rest, $destination, -1, 0)) {
69
                    // @codeCoverageIgnoreStart
70
                    throw new FilesException($this->getLang()->flCannotWriteFile('temp'));
71
                }
72
                // @codeCoverageIgnoreEnd
73
            }
74 10
            return $destination;
75
        } else {
76 2
            return $added;
77
        }
78
    }
79
}
80