Completed
Push — master ( 2290e5...2a17e7 )
by Arne
02:13
created

IndexObject::equals()   C

Complexity

Conditions 10
Paths 257

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 6
c 0
b 0
f 0
cc 10
eloc 13
nc 257
nop 1

How to fix   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 Storeman\Index;
4
5
use Storeman\Exception;
6
7
/**
8
 * An index object is the representation of one of this filesystem primitives contained in the index.
9
 */
10
class IndexObject
11
{
12
    public const TYPE_DIR = 1;
13
    public const TYPE_FILE = 2;
14
    public const TYPE_LINK = 3;
15
16
17
    /**
18
     * @var string
19
     */
20
    protected $relativePath;
21
22
    /**
23
     * @var int
24
     */
25
    protected $type;
26
27
    /**
28
     * @var int
29
     */
30
    protected $mtime;
31
32
    /**
33
     * @var int
34
     */
35
    protected $ctime;
36
37
    /**
38
     * Full file mode.
39
     * May include additional modes like setuid, guid, etc.
40
     *
41
     * @var int
42
     */
43
    protected $mode;
44
45
    /**
46
     * @var int
47
     */
48
    protected $size;
49
50
    /**
51
     * @var string
52
     */
53
    protected $linkTarget;
54
55
    /**
56
     * @var string
57
     */
58
    protected $blobId;
59
60
    /**
61
     * Prevent construction not using static factory methods.
62
     */
63
    protected function __construct() {}
64
65
    public function getRelativePath(): string
66
    {
67
        return $this->relativePath;
68
    }
69
70
    public function getBasename(): string
71
    {
72
        $pos = strrpos($this->relativePath, DIRECTORY_SEPARATOR);
73
74
        return ($pos === false) ? $this->relativePath : substr($this->relativePath, $pos + 1);
75
    }
76
77
    public function getType(): int
78
    {
79
        return $this->type;
80
    }
81
82
    public function isDirectory(): bool
83
    {
84
        return $this->type === static::TYPE_DIR;
85
    }
86
87
    public function isFile(): bool
88
    {
89
        return $this->type === static::TYPE_FILE;
90
    }
91
92
    public function isLink(): bool
93
    {
94
        return $this->type === static::TYPE_LINK;
95
    }
96
97
    public function getMtime(): int
98
    {
99
        return $this->mtime;
100
    }
101
102
    public function getCtime(): int
103
    {
104
        return $this->ctime;
105
    }
106
107
    public function getMode(): int
108
    {
109
        return $this->mode;
110
    }
111
112
    public function getSize(): ?int
113
    {
114
        return $this->size;
115
    }
116
117
    public function getLinkTarget(): ?string
118
    {
119
        return $this->linkTarget;
120
    }
121
122
    public function getBlobId(): ?string
123
    {
124
        return $this->blobId;
125
    }
126
127
    public function setBlobId(string $blobId): IndexObject
128
    {
129
        $this->blobId = $blobId;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Equality check with all attributes.
136
     *
137
     * @param IndexObject $other
138
     * @return bool
139
     */
140
    public function equals(?IndexObject $other): bool
141
    {
142
        if ($other === null)
143
        {
144
            return false;
145
        }
146
147
        $equals = true;
148
        $equals = $equals && ($this->relativePath === $other->relativePath);
149
        $equals = $equals && ($this->type === $other->type);
150
        $equals = $equals && ($this->mtime === $other->mtime);
151
        $equals = $equals && ($this->ctime === $other->ctime);
152
        $equals = $equals && ($this->mode === $other->mode);
153
        $equals = $equals && ($this->size === $other->size);
154
        $equals = $equals && ($this->linkTarget === $other->linkTarget);
155
        $equals = $equals && ($this->blobId === $other->blobId);
156
157
        return $equals;
158
    }
159
160
    public function toScalarArray(): array
161
    {
162
        return [
163
            $this->relativePath,
164
            $this->type,
165
            $this->mtime,
166
            $this->ctime,
167
            $this->mode,
168
            $this->size,
169
            $this->blobId,
170
            $this->linkTarget,
171
        ];
172
    }
173
174
    public static function fromScalarArray(array $array): IndexObject
175
    {
176
        $object = new static;
177
        $object->relativePath = $array[0];
178
        $object->type = (int)$array[1];
179
        $object->mtime = (int)$array[2];
180
        $object->ctime = (int)$array[3];
181
        $object->mode = (int)$array[4];
182
        $object->size = (int)$array[5];
183
        $object->blobId = $array[6];
184
        $object->linkTarget = $array[7];
185
186
        return $object;
187
    }
188
189
    /**
190
     * Returns an instance representing the filesystem object that can be found under the given path.
191
     *
192
     * @param string $basePath
193
     * @param string $relativePath
194
     * @return IndexObject
195
     */
196
    public static function fromPath(string $basePath, string $relativePath): IndexObject
197
    {
198
        $absolutePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath;
199
200
        clearstatcache(null, $absolutePath);
201
202
        if (!($stat = @lstat($absolutePath)))
203
        {
204
            throw new Exception("lstat() failed for {$absolutePath}");
205
        }
206
207
        $object = new static;
208
        $object->relativePath = $relativePath;
209
        $object->mtime = $stat['mtime'];
210
        $object->ctime = $stat['ctime'];
211
        $object->mode = $stat['mode'];
212
213
        if (is_link($absolutePath))
214
        {
215
            $object->type = static::TYPE_LINK;
216
            $object->linkTarget = str_replace("{$basePath}/", '', readlink($absolutePath));
217
        }
218
        elseif (is_file($absolutePath))
219
        {
220
            $object->type = static::TYPE_FILE;
221
            $object->size = (int)$stat['size'];
222
        }
223
        elseif (is_dir($absolutePath))
224
        {
225
            $object->type = static::TYPE_DIR;
226
        }
227
        else
228
        {
229
            throw new \LogicException();
230
        }
231
232
        return $object;
233
    }
234
}
235