Completed
Pull Request — master (#3)
by
unknown
12:27
created

FileStream::addFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace pastuhov\FileStream;
3
4
class FileStream extends BaseFileStream
5
{
6
    /**
7
     * File header.
8
     *
9
     * Any file will be started from that string.
10
     *
11
     * @var string
12
     */
13
    protected $header;
14
15
    /**
16
     * File footer.
17
     *
18
     * Any file will be ended at that string.
19
     *
20
     * @var string
21
     */
22
    protected $footer;
23
24
    /**
25
     * Max possible writes to one file.
26
     * @var int
27
     */
28
    protected $maxCount;
29
30
    /**
31
     * File name count placeholder.
32
     * @var string
33
     */
34
    protected $countPlaceHolder = '{count}';
35
36
    /**
37
     * Current writes count.
38
     * @var int
39
     */
40
    protected $currentCount = 0;
41
42
    /**
43
     * Current files count.
44
     * @var int
45
     */
46
    protected $currentFileCount = 0;
47
48
    /**
49
     * List of Files
50
     * @var array
51
     */
52
    protected $files = [];
53
54
    /**
55 9
     * @inheritdoc
56
     * @param string|null $header File header
57 9
     * @param string|null $footer File footer
58
     * @param int|bool|false $maxCount Max possible writes to one file
59 9
     * @throws \Exception
60 9
     */
61 9
    public function __construct($fileName, $header = null, $footer = null, $maxCount = false)
62
    {
63 9
        parent::__construct($fileName);
64 3
65
        $this->header = $header;
66 6
        $this->footer = $footer;
67
        $this->maxCount = $maxCount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $maxCount can also be of type boolean. However, the property $maxCount is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
68
69
        if ($this->maxCount !== false && strpos($this->fileName, $this->countPlaceHolder) === false) {
70
            throw new \Exception('File name ' . $this->countPlaceHolder . ' placeholder is needed');
71 6
        }
72
    }
73 6
74
    /**
75 6
     * Adds the given Filename to the internal Array
76 3
     * @param $filename
77 3
     */
78 6
    protected function addFile($filename)
79
    {
80
        $this->files[] = $filename;
81
    }
82
83
    /**
84
     * Returns the List of written Files
85 6
     * @return array
86
     */
87 6
    public function getFileList()
88
    {
89 6
        return $this->files;
90 6
    }
91 6
92 3
    /**
93 3
     * @inheritdoc
94 6
     */
95 6
    protected function openHandle()
96
    {
97
        parent::openHandle();
98
        $this->addFile($this->getFileName());
99
        if ($this->header !== null) {
100 6
            $this->write($this->header, false);
101
        }
102 6
    }
103 3
104 3
    /**
105
     * @inheritdoc
106 6
     * @param bool $count
107
     * @throws \Exception
108 6
     */
109 6
    public function write($string, $count = true)
110 6
    {
111
        parent::write($string);
112
113
        if ($count) {
114
            $this->currentCount++;
115
            if ($this->currentCount === $this->maxCount) {
116 6
                $this->closeHandle();
117
            }
118 6
        }
119
    }
120 6
121 3
    /**
122 3
     * @inheritdoc
123
     */
124 6
    protected function closeHandle()
125
    {
126
        if ($this->footer !== null) {
127
            $this->write($this->footer, false);
128
        }
129
130
        parent::closeHandle();
131
132
        $this->currentFileCount++;
133
        $this->currentCount = 0;
134
    }
135
136
    /**
137
     * Base file name with replaced count placeholder.
138
     * @return string Base file name with replaced placeholder
139
     */
140
    protected function getFileName()
141
    {
142
        $fileName = parent::getFileName();
143
144
        if ($this->maxCount !== false) {
145
            $fileName = strtr($fileName, [$this->countPlaceHolder => $this->currentFileCount]);
146
        }
147
148
        return $fileName;
149
    }
150
}