SplFileInfoRepresentation   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 100
c 1
b 0
f 0
dl 0
loc 145
rs 9.6
wmc 35

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabel() 0 3 1
A getMTime() 0 9 2
F __construct() 0 100 30
A getSize() 0 6 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
 * this software and associated documentation files (the "Software"), to deal in
10
 * the Software without restriction, including without limitation the rights to
11
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
 * the Software, and to permit persons to whom the Software is furnished to do so,
13
 * subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
26
namespace Kint\Object\Representation;
27
28
use Kint\Utils;
29
use SplFileInfo;
30
31
class SplFileInfoRepresentation extends Representation
32
{
33
    public $perms;
34
    public $flags;
35
    public $path;
36
    public $realpath;
37
    public $linktarget;
38
    public $size;
39
    public $is_dir = false;
40
    public $is_file = false;
41
    public $is_link = false;
42
    public $owner;
43
    public $group;
44
    public $ctime;
45
    public $mtime;
46
    public $typename = 'Unknown file';
47
    public $typeflag = '-';
48
    public $hints = array('fspath');
49
50
    public function __construct(SplFileInfo $fileInfo)
51
    {
52
        parent::__construct('SplFileInfo');
53
54
        if ($fileInfo->getRealPath()) {
55
            $this->realpath = $fileInfo->getRealPath();
56
            $this->perms = $fileInfo->getPerms();
57
            $this->size = $fileInfo->getSize();
58
            $this->owner = $fileInfo->getOwner();
59
            $this->group = $fileInfo->getGroup();
60
            $this->ctime = $fileInfo->getCTime();
61
            $this->mtime = $fileInfo->getMTime();
62
        }
63
64
        $this->path = $fileInfo->getPathname();
65
66
        $this->is_dir = $fileInfo->isDir();
67
        $this->is_file = $fileInfo->isFile();
68
        $this->is_link = $fileInfo->isLink();
69
70
        if ($this->is_link) {
71
            $this->linktarget = $fileInfo->getLinkTarget();
72
        }
73
74
        switch ($this->perms & 0xF000) {
75
            case 0xC000:
76
                $this->typename = 'Socket';
77
                $this->typeflag = 's';
78
                break;
79
            case 0x6000:
80
                $this->typename = 'Block device';
81
                $this->typeflag = 'b';
82
                break;
83
            case 0x2000:
84
                $this->typename = 'Character device';
85
                $this->typeflag = 'c';
86
                break;
87
            case 0x1000:
88
                $this->typename = 'Named pipe';
89
                $this->typeflag = 'p';
90
                break;
91
            default:
92
                if ($this->is_file) {
93
                    if ($this->is_link) {
94
                        $this->typename = 'File symlink';
95
                        $this->typeflag = 'l';
96
                    } else {
97
                        $this->typename = 'File';
98
                        $this->typeflag = '-';
99
                    }
100
                } elseif ($this->is_dir) {
101
                    if ($this->is_link) {
102
                        $this->typename = 'Directory symlink';
103
                        $this->typeflag = 'l';
104
                    } else {
105
                        $this->typename = 'Directory';
106
                        $this->typeflag = 'd';
107
                    }
108
                }
109
                break;
110
        }
111
112
        $this->flags = array($this->typeflag);
113
114
        // User
115
        $this->flags[] = (($this->perms & 0400) ? 'r' : '-');
116
        $this->flags[] = (($this->perms & 0200) ? 'w' : '-');
117
        if ($this->perms & 0100) {
118
            $this->flags[] = ($this->perms & 04000) ? 's' : 'x';
119
        } else {
120
            $this->flags[] = ($this->perms & 04000) ? 'S' : '-';
121
        }
122
123
        // Group
124
        $this->flags[] = (($this->perms & 0040) ? 'r' : '-');
125
        $this->flags[] = (($this->perms & 0020) ? 'w' : '-');
126
        if ($this->perms & 0010) {
127
            $this->flags[] = ($this->perms & 02000) ? 's' : 'x';
128
        } else {
129
            $this->flags[] = ($this->perms & 02000) ? 'S' : '-';
130
        }
131
132
        // Other
133
        $this->flags[] = (($this->perms & 0004) ? 'r' : '-');
134
        $this->flags[] = (($this->perms & 0002) ? 'w' : '-');
135
        if ($this->perms & 0001) {
136
            $this->flags[] = ($this->perms & 01000) ? 's' : 'x';
137
        } else {
138
            $this->flags[] = ($this->perms & 01000) ? 'S' : '-';
139
        }
140
141
        $this->contents = \implode($this->flags).' '.$this->owner.' '.$this->group;
142
        $this->contents .= ' '.$this->getSize().' '.$this->getMTime().' ';
143
144
        if ($this->is_link && $this->linktarget) {
145
            $this->contents .= $this->path.' -> '.$this->linktarget;
146
        } elseif (null !== $this->realpath && \strlen($this->realpath) < \strlen($this->path)) {
147
            $this->contents .= $this->realpath;
148
        } else {
149
            $this->contents .= $this->path;
150
        }
151
    }
152
153
    public function getLabel()
154
    {
155
        return $this->typename.' ('.$this->getSize().')';
156
    }
157
158
    public function getSize()
159
    {
160
        if ($this->size) {
161
            $size = Utils::getHumanReadableBytes($this->size);
162
163
            return \round($size['value'], 2).$size['unit'];
164
        }
165
    }
166
167
    public function getMTime()
168
    {
169
        $year = \date('Y', $this->mtime);
170
171
        if ($year !== \date('Y')) {
172
            return \date('M d Y', $this->mtime);
173
        }
174
175
        return \date('M d H:i', $this->mtime);
176
    }
177
}
178