Test Setup Failed
Push — test ( 528f91...abcbcc )
by Jonathan
03:20
created

SplFileInfoRepresentation::__construct()   F

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 95
Code Lines 70

Duplication

Lines 15
Ratio 15.79 %

Importance

Changes 0
Metric Value
cc 30
eloc 70
nc 221185
nop 1
dl 15
loc 95
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kint\Object\Representation;
4
5
use SplFileInfo;
6
7
class SplFileInfoRepresentation extends Representation
8
{
9
    public $perms = null;
10
    public $flags = null;
11
    public $path = null;
12
    public $realpath = null;
13
    public $linktarget = null;
14
    public $size = null;
15
    public $is_dir = false;
0 ignored issues
show
Coding Style introduced by
$is_dir does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
16
    public $is_file = false;
0 ignored issues
show
Coding Style introduced by
$is_file does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
    public $is_link = false;
0 ignored issues
show
Coding Style introduced by
$is_link does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
    public $owner = null;
19
    public $group = null;
20
    public $ctime = null;
21
    public $mtime = null;
22
    public $typename = 'Unknown file';
23
    public $typeflag = '-';
24
    public $hints = array('fspath');
25
26
    public function __construct(SplFileInfo $fileInfo)
27
    {
28
        if (!file_exists($fileInfo->getPathname())) {
29
            return;
30
        }
31
32
        $this->perms = $fileInfo->getPerms();
33
        $this->size = $fileInfo->getSize();
34
        $this->is_dir = $fileInfo->isDir();
35
        $this->is_file = $fileInfo->isFile();
36
        $this->is_link = $fileInfo->isLink();
37
        $this->owner = $fileInfo->getOwner();
38
        $this->group = $fileInfo->getGroup();
39
        $this->ctime = $fileInfo->getCTime();
40
        $this->mtime = $fileInfo->getMTime();
41
42
        if (($this->perms & 0xC000) === 0xC000) {
43
            $this->typename = 'File socket';
44
            $this->typeflag = 's';
45
        } elseif ($this->is_file) {
46
            if ($this->is_link) {
47
                $this->typename = 'File symlink';
48
                $this->typeflag = 'l';
49
            } else {
50
                $this->typename = 'File';
51
                $this->typeflag = '-';
52
            }
53
        } elseif (($this->perms & 0x6000) === 0x6000) {
54
            $this->typename = 'Block special file';
55
            $this->typeflag = 'b';
56
        } elseif ($this->is_dir) {
57
            if ($this->is_link) {
58
                $this->typename = 'Directory symlink';
59
                $this->typeflag = 'l';
60
            } else {
61
                $this->typename = 'Directory';
62
                $this->typeflag = 'd';
63
            }
64
        } elseif (($this->perms & 0x2000) === 0x2000) {
65
            $this->typename = 'Character special file';
66
            $this->typeflag = 'c';
67
        } elseif (($this->perms & 0x1000) === 0x1000) {
68
            $this->typename = 'FIFO pipe file';
69
            $this->typeflag = 'p';
70
        }
71
72
        parent::__construct('SplFileInfo');
73
74
        $this->path = $fileInfo->getPathname();
75
        $this->realpath = realpath($this->path);
76
77
        if ($this->is_link && method_exists($fileInfo, 'getLinktarget')) {
78
            $this->linktarget = $fileInfo->getLinkTarget();
79
        }
80
81
        $this->flags = array($this->typeflag);
82
83
        // User
84
        $this->flags[] = (($this->perms & 0400) ? 'r' : '-');
85
        $this->flags[] = (($this->perms & 0200) ? 'w' : '-');
86 View Code Duplication
        if ($this->perms & 0100) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            $this->flags[] = ($this->perms & 04000) ? 's' : 'x';
88
        } else {
89
            $this->flags[] = ($this->perms & 04000) ? 'S' : '-';
90
        }
91
92
        // Group
93
        $this->flags[] = (($this->perms & 0040) ? 'r' : '-');
94
        $this->flags[] = (($this->perms & 0020) ? 'w' : '-');
95 View Code Duplication
        if ($this->perms & 0010) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $this->flags[] = ($this->perms & 02000) ? 's' : 'x';
97
        } else {
98
            $this->flags[] = ($this->perms & 02000) ? 'S' : '-';
99
        }
100
101
        // Other
102
        $this->flags[] = (($this->perms & 0004) ? 'r' : '-');
103
        $this->flags[] = (($this->perms & 0002) ? 'w' : '-');
104 View Code Duplication
        if ($this->perms & 0001) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
            $this->flags[] = ($this->perms & 01000) ? 's' : 'x';
106
        } else {
107
            $this->flags[] = ($this->perms & 01000) ? 'S' : '-';
108
        }
109
110
        $this->contents = implode($this->flags).' '.$this->owner.' '.$this->group;
0 ignored issues
show
Documentation Bug introduced by
It seems like implode($this->flags) . ...er . ' ' . $this->group of type string is incompatible with the declared type array of property $contents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
        $this->contents .= ' '.$this->getSize().' '.$this->getMTime().' ';
112
113
        if ($this->is_link && $this->linktarget) {
114
            $this->contents .= $this->path.' -> '.$this->linktarget;
115
        } elseif (strlen($this->realpath) < strlen($this->path)) {
116
            $this->contents .= $this->realpath;
117
        } else {
118
            $this->contents .= $this->path;
119
        }
120
    }
121
122
    public function getLabel()
123
    {
124
        return $this->typename.' ('.$this->getSize().')';
125
    }
126
127
    public function getSize()
128
    {
129
        static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
130
131
        $size = $this->size;
132
133
        if ($this->size) {
134
            $i = floor(log($this->size, 1024));
135
            $size = round($this->size / pow(1024, $i), 2).$unit[$i];
136
        }
137
138
        return $size;
139
    }
140
141
    public function getMTime()
142
    {
143
        $year = date('Y', $this->mtime);
144
145
        if ($year !== date('Y')) {
146
            return date('M d Y', $this->mtime);
147
        } else {
148
            return date('M d H:i', $this->mtime);
149
        }
150
    }
151
}
152