Passed
Pull Request — master (#1486)
by Michael
08:32
created

SplFileInfoRepresentation   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 102
dl 0
loc 145
rs 9.28
c 1
b 0
f 0
wmc 39

2 Methods

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