File   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 0 28 4
B size() 0 26 5
A type() 0 4 1
A encoding() 0 4 1
A _info() 0 18 2
1
<?php
2
3
/**
4
 * Working with files
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Files
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\files;
17
18
/**
19
 * Working with files
20
 *
21
 * @category  Core
22
 * @package   Files
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class File
30
{
31
    /**
32
     * Content of a directory as an array
33
     *
34
     * @param string  $path   Target directory with full path
35
     * @param boolean $desc   Whether to list files in descending order
36
     * @param array   $remove List of files to remove
37
     *
38
     * @return array
39
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
40
41
    public static function search($path, $desc = false, array $remove = [])
42
    {
43
        // Directories should always end with a slash
44
        $dir = rtrim($path, '\/') . '/';
45
46
        // If it is not a directory use an empty array
47
        $scandir = is_dir($dir) ? scandir($dir) : [];
48
49
        $scandir = array_flip($scandir);
50
51
        // Remove given entries
52
        unset($scandir['.'], $scandir['..'], $scandir['.DS_Store'], $scandir['.svn']);
53
54
        foreach ($remove AS $name) {
55
56
            unset($scandir[$name]);
57
        }
58
59
        // Reverse order if requested
60
        if ($desc == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
61
62
            krsort($scandir);
63
        }
64
65
        $scandir = array_keys($scandir);
66
67
        return $scandir;
68
    }
69
70
    /**
71
     * Size of a file as a readable string number from Byte to TiB
72
     *
73
     * @param int     $size  Size of file in Bytes
74
     * @param int     $float Digits after the dot in resulting float
75
     * @param boolean $short Defaults to true which shortens high values
76
     *
77
     * @return string
78
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
79
80
    public static function size($size, $float = 3, $short = true)
81
    {
82
        // Appended to the reduced float to make it more readable
83
        $size_names = ['Byte', 'KiB', 'MiB', 'GiB', 'TiB'];
84
85
        $digits = 0;
86
87
        // Determine the best size name
88
        while ($size >= 1024 && $digits < 4) {
89
90
            $size = $size / 1024;
91
92
            $digits++;
93
        }
94
95
        // Size above 100 should not be a float
96
        if (!empty($short) && $size > 100) {
97
98
            $result = round($size);
99
        } else {
100
101
            $result = round($size, $float);
102
        }
103
104
        return $result . ' ' . $size_names[$digits];
105
    }
106
107
    /**
108
     * Mimetype of a file or buffer
109
     *
110
     * @param string  $content File with path or string variable
111
     * @param boolean $buffer  Use string instead of a file
112
     *
113
     * @return string
114
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
115
116
    public static function type($content, $buffer = false)
117
    {
118
        return self::_info($content, $buffer, FILEINFO_MIME_TYPE);
119
    }
120
121
    /**
122
     * Encoding of a file or buffer
123
     *
124
     * @param string  $content File with path or string variable
125
     * @param boolean $buffer  Use string instead of a file
126
     *
127
     * @return string
128
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
129
130
    public static function encoding($content, $buffer = false)
131
    {
132
        return self::_info($content, $buffer, FILEINFO_MIME_ENCODING);
133
    }
134
135
    /**
136
     * Mime info of a file or buffer
137
     *
138
     * @param string  $content File with path or string variable
139
     * @param boolean $buffer  Use string instead of a file
140
     * @param integer $option  Fileinfo constant
141
     *
142
     * @return string
143
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
144
145
    private static function _info($content, $buffer, $option)
146
    {
147
        // Use fileinfo extension for this
148
        $flp = finfo_open($option);
149
150
        if ($buffer === false) {
151
152
            $return = finfo_file($flp, $content);
153
154
        } else {
155
156
            $return = finfo_buffer($flp, $content);
157
        }
158
159
        finfo_close($flp);
160
161
        return $return;
162
    }
163
}
164