Completed
Push — master ( e610ee...c79300 )
by Arne
04:09
created

IndexObject::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 15
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Storeman\Index;
4
5
use Storeman\Hash\HashContainer;
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 int
52
     */
53
    protected $inode;
54
55
    /**
56
     * @var string
57
     */
58
    protected $linkTarget;
59
60
    /**
61
     * Content hashes for file index objects.
62
     *
63
     * @var HashContainer
64
     */
65
    protected $hashes;
66
67
    /**
68
     * @var string
69
     */
70
    protected $blobId;
71
72
    public function __construct(string $relativePath, int $type, int $mtime, int $ctime, int $mode, ?int $size, ?int $inode, ?string $linkTarget, ?string $blobId, ?HashContainer $hashContainer)
73
    {
74
        assert(($type === static::TYPE_FILE) ^ ($size === null));
75
        assert(($type === static::TYPE_FILE) || ($blobId === null));
76
        assert(($type === static::TYPE_FILE) ^ ($hashContainer === null));
77
        assert(($type === static::TYPE_LINK) ^ ($linkTarget === null));
78
79
        $this->relativePath = $relativePath;
80
        $this->type = $type;
81
        $this->mtime = $mtime;
82
        $this->ctime = $ctime;
83
        $this->mode = $mode;
84
        $this->size = $size;
85
        $this->inode = $inode;
86
        $this->linkTarget = $linkTarget;
87
        $this->blobId = $blobId;
88
        $this->hashes = $hashContainer;
89
    }
90
91
    public function getRelativePath(): string
92
    {
93
        return $this->relativePath;
94
    }
95
96
    public function getBasename(): string
97
    {
98
        $pos = strrpos($this->relativePath, DIRECTORY_SEPARATOR);
99
100
        return ($pos === false) ? $this->relativePath : substr($this->relativePath, $pos + 1);
101
    }
102
103
    public function getType(): int
104
    {
105
        return $this->type;
106
    }
107
108
    public function isDirectory(): bool
109
    {
110
        return $this->type === static::TYPE_DIR;
111
    }
112
113
    public function isFile(): bool
114
    {
115
        return $this->type === static::TYPE_FILE;
116
    }
117
118
    public function isLink(): bool
119
    {
120
        return $this->type === static::TYPE_LINK;
121
    }
122
123
    public function getMtime(): int
124
    {
125
        return $this->mtime;
126
    }
127
128
    public function getCtime(): int
129
    {
130
        return $this->ctime;
131
    }
132
133
    public function getMode(): int
134
    {
135
        return $this->mode;
136
    }
137
138
    public function getSize(): ?int
139
    {
140
        return $this->size;
141
    }
142
143
    public function getInode(): ?int
144
    {
145
        return $this->inode;
146
    }
147
148
    public function getLinkTarget(): ?string
149
    {
150
        return $this->linkTarget;
151
    }
152
153
    public function getHashes(): ?HashContainer
154
    {
155
        return $this->hashes;
156
    }
157
158
    public function getBlobId(): ?string
159
    {
160
        return $this->blobId;
161
    }
162
163
    public function setBlobId(string $blobId): IndexObject
164
    {
165
        $this->blobId = $blobId;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Equality check with all attributes.
172
     *
173
     * @param IndexObject $other
174
     * @return bool
175
     */
176
    public function equals(?IndexObject $other): bool
177
    {
178
        if ($other === null)
179
        {
180
            return false;
181
        }
182
183
        $equals = true;
184
        $equals = $equals && ($this->relativePath === $other->relativePath);
185
        $equals = $equals && ($this->type === $other->type);
186
        $equals = $equals && ($this->mtime === $other->mtime);
187
        $equals = $equals && ($this->ctime === $other->ctime);
188
        $equals = $equals && ($this->mode === $other->mode);
189
        $equals = $equals && ($this->size === $other->size);
190
        $equals = $equals && ($this->inode === $other->inode);
191
        $equals = $equals && ($this->linkTarget === $other->linkTarget);
192
        $equals = $equals && ($this->blobId === $other->blobId);
193
194
        if ($this->hashes)
195
        {
196
            $equals = $equals && $this->hashes->equals($other->hashes);
197
        }
198
199
        return $equals;
200
    }
201
}
202