Passed
Push — master ( bb35d3...bc1c68 )
by Fabrice
03:52
created

FileHandlerTrait::releaseHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Traits;
11
12
/**
13
 * Trait FileHandlerTrait
14
 */
15
trait FileHandlerTrait
16
{
17
    /**
18
     * @var string UTF8 | UTF16_BE | UTF16_LE | UTF32_BE | UTF32_LE
19
     */
20
    protected $bomRegEx = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00';
21
22
    /**
23
     * @var resource|false|null
24
     */
25
    protected $handle;
26
27
    /**
28
     * make sure we do not hold un-necessary handles
29
     */
30
    public function __destruct()
31
    {
32
        $this->releaseHandle();
33
    }
34
35
    /**
36
     * @param $string
37
     *
38
     * @return string
39
     */
40
    public function trimBom($string)
41
    {
42
        return preg_replace('`^' . $this->bomRegEx . '`', '', $string);
43
    }
44
45
    /**
46
     * release handle
47
     *
48
     * @return $this
49
     */
50
    public function releaseHandle()
51
    {
52
        if (is_resource($this->handle)) {
53
            fclose($this->handle);
54
        }
55
56
        $this->handle = null;
57
58
        return $this;
59
    }
60
}
61