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

FileHandlerTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A releaseHandle() 0 9 2
A __destruct() 0 3 1
A trimBom() 0 3 1
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