Skip to content

Commit 8d79110

Browse files
committed
refact: Get rid of Properties trait
1 parent 827e6ee commit 8d79110

File tree

4 files changed

+97
-156
lines changed

4 files changed

+97
-156
lines changed

src/classes/Instance.php

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Kirby\Exception\Exception;
77
use Kirby\Exception\LogicException;
88
use Kirby\Toolkit\I18n;
9-
use Kirby\Toolkit\Properties;
109

1110
/**
1211
* Instance
@@ -20,8 +19,6 @@
2019
*/
2120
class Instance
2221
{
23-
use Properties;
24-
2522
/**
2623
* Cache for the changes object for this instance
2724
*/
@@ -40,7 +37,7 @@ class Instance
4037
/**
4138
* Current commit of this instance
4239
*/
43-
protected string|null $currentCommit = null;
40+
protected string|null $currentCommit;
4441

4542
/**
4643
* Instance name that is displayed in the Panel
@@ -59,7 +56,11 @@ class Instance
5956
*/
6057
public function __construct(array $props)
6158
{
62-
$this->setProperties($props);
59+
$this->color = $props['color'];
60+
$this->contentRoot = rtrim($props['contentRoot'], '/\\');
61+
$this->currentCommit = $props['currentCommit'] ?? null;
62+
$this->name = $props['name'];
63+
$this->plugin = $props['plugin'];
6364

6465
// validate that the instance has a Git repo set up
6566
try {
@@ -279,16 +280,18 @@ public function setCurrentCommit(string|null $currentCommit = null): self
279280
*/
280281
public function toArray(): array
281282
{
282-
$array = $this->propertiesToArray();
283-
$array['changes'] = $this->changes()->overall();
284-
$array['isCurrent'] = $this->isCurrent();
285-
286283
$version = $this->version();
287-
$array['version'] = ($version !== null)? $version->name() : null;
288-
$array['versionLabel'] = ($version !== null)? $version->label() : null;
289284

290-
ksort($array);
291-
return $array;
285+
return [
286+
'changes' => $this->changes()->overall(),
287+
'color' => $this->color(),
288+
'contentRoot' => $this->contentRoot(),
289+
'currentCommit' => $this->currentCommit(),
290+
'isCurrent' => $this->isCurrent(),
291+
'name' => $this->name(),
292+
'version' => ($version !== null) ? $version->name() : null,
293+
'versionLabel' => ($version !== null) ? $version->label() : null,
294+
];
292295
}
293296

294297
/**
@@ -302,40 +305,4 @@ public function version(): Version|null
302305

303306
return $this->plugin->versions()->findBy('commit', $this->currentCommit());
304307
}
305-
306-
/**
307-
* Sets the CSS color for display in the Panel
308-
*/
309-
protected function setColor(string $color): self
310-
{
311-
$this->color = $color;
312-
return $this;
313-
}
314-
315-
/**
316-
* Sets the path to the content directory
317-
*/
318-
protected function setContentRoot(string $contentRoot): self
319-
{
320-
$this->contentRoot = rtrim($contentRoot, '/\\');
321-
return $this;
322-
}
323-
324-
/**
325-
* Sets the instance name that is displayed in the Panel
326-
*/
327-
protected function setName(string $name): self
328-
{
329-
$this->name = $name;
330-
return $this;
331-
}
332-
333-
/**
334-
* Sets the instance of the Plugin class
335-
*/
336-
protected function setPlugin(Plugin $plugin): self
337-
{
338-
$this->plugin = $plugin;
339-
return $this;
340-
}
341308
}

src/classes/Version.php

Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Kirby\Filesystem\F;
99
use Kirby\Toolkit\Collection;
1010
use Kirby\Toolkit\I18n;
11-
use Kirby\Toolkit\Properties;
1211

1312
/**
1413
* Version
@@ -22,8 +21,6 @@
2221
*/
2322
class Version
2423
{
25-
use Properties;
26-
2724
/**
2825
* Commit hash of this version
2926
*/
@@ -32,17 +29,17 @@ class Version
3229
/**
3330
* Creation timestamp
3431
*/
35-
protected int|null $created = null;
32+
protected int|null $created;
3633

3734
/**
3835
* Email address of the creator
3936
*/
40-
protected string|null $creatorEmail = null;
37+
protected string|null $creatorEmail;
4138

4239
/**
4340
* Name of the creator
4441
*/
45-
protected string|null $creatorName = null;
42+
protected string|null $creatorName;
4643

4744
/**
4845
* Custom label
@@ -58,7 +55,7 @@ class Version
5855
* Name of the instance where the version was
5956
* originally created
6057
*/
61-
protected string|null $originInstance = null;
58+
protected string|null $originInstance;
6259

6360
/**
6461
* Instance of the Plugin class
@@ -70,7 +67,14 @@ class Version
7067
*/
7168
public function __construct(array $props)
7269
{
73-
$this->setProperties($props);
70+
$this->commit = $props['commit'];
71+
$this->created = $this->normalizeCreated($props['created'] ?? null);
72+
$this->creatorEmail = $this->normalizeCreatorEmail($props['creatorEmail'] ?? null);
73+
$this->creatorName = $this->normalizeCreatorName($props['creatorName'] ?? null);
74+
$this->label = $props['label'];
75+
$this->name = $props['name'];
76+
$this->originInstance = $props['originInstance'] ?? null;
77+
$this->plugin = $props['plugin'];
7478
}
7579

7680
/**
@@ -229,26 +233,22 @@ public function originInstance(): string|null
229233
*/
230234
public function toArray(): array
231235
{
232-
$array = $this->propertiesToArray();
233-
$array['instances'] = $this->instances()->keys();
234-
235-
ksort($array);
236-
return $array;
237-
}
238-
239-
/**
240-
* Sets the commit hash of this version
241-
*/
242-
protected function setCommit(string $commit): self
243-
{
244-
$this->commit = $commit;
245-
return $this;
236+
return [
237+
'commit' => $this->commit(),
238+
'created' => $this->created(),
239+
'creatorEmail' => $this->creatorEmail(),
240+
'creatorName' => $this->creatorName(),
241+
'instances' => $this->instances()->keys(),
242+
'label' => $this->label(),
243+
'name' => $this->name(),
244+
'originInstance' => $this->originInstance(),
245+
];
246246
}
247247

248248
/**
249-
* Sets the creation timestamp
249+
* Normalizes the creation timestamp for the property
250250
*/
251-
protected function setCreated(int|string|null $created = null): self
251+
protected function normalizeCreated(int|string|null $created): int|null
252252
{
253253
if (is_string($created) === true) {
254254
$created = $created ? strtotime($created) : null;
@@ -261,76 +261,34 @@ protected function setCreated(int|string|null $created = null): self
261261
]);
262262
}
263263

264-
$this->created = $created;
265-
return $this;
264+
return $created;
266265
}
267266

268267
/**
269-
* Sets the email address of the creator
268+
* Normalizes the email address of the creator for the property
270269
*/
271-
protected function setCreatorEmail(string|null $creatorEmail = null): self
270+
protected function normalizeCreatorEmail(string|null $creatorEmail): string|null
272271
{
273272
// Git will output an empty string if the value is not available
274-
if ($creatorEmail === '') {
275-
$creatorEmail = null;
276-
} elseif ($creatorEmail !== null) {
277-
// trim the angle brackets around the email address
278-
// that come from Git's output
279-
$creatorEmail = trim($creatorEmail, '<>');
273+
if ($creatorEmail === '' || $creatorEmail === null) {
274+
return null;
280275
}
281276

282-
$this->creatorEmail = $creatorEmail;
283-
return $this;
277+
// trim the angle brackets around the email address
278+
// that come from Git's output
279+
return trim($creatorEmail, '<>');
284280
}
285281

286282
/**
287-
* Sets the name of the creator
283+
* Normalizes the name of the creator for the property
288284
*/
289-
protected function setCreatorName(string|null $creatorName = null): self
285+
protected function normalizeCreatorName(string|null $creatorName): string|null
290286
{
291287
// Git will output an empty string if the value is not available
292288
if ($creatorName === '') {
293-
$creatorName = null;
289+
return null;
294290
}
295291

296-
$this->creatorName = $creatorName;
297-
return $this;
298-
}
299-
300-
/**
301-
* Sets the custom label
302-
*/
303-
protected function setLabel(string $label): self
304-
{
305-
$this->label = $label;
306-
return $this;
307-
}
308-
309-
/**
310-
* Sets the unique version name
311-
*/
312-
protected function setName(string $name): self
313-
{
314-
$this->name = $name;
315-
return $this;
316-
}
317-
318-
/**
319-
* Sets the name of the instance where
320-
* the version was originally created
321-
*/
322-
protected function setOriginInstance(string|null $originInstance = null): self
323-
{
324-
$this->originInstance = $originInstance;
325-
return $this;
326-
}
327-
328-
/**
329-
* Sets the instance of the Plugin class
330-
*/
331-
protected function setPlugin(Plugin $plugin): self
332-
{
333-
$this->plugin = $plugin;
334-
return $this;
292+
return $creatorName;
335293
}
336294
}

tests/Versions/InstanceTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public function testConstructErrorNoGitRepo()
4747
'Git repo, please either initialize a new repo or connect it as a worktree'
4848
);
4949

50-
$this->instance->clone(['contentRoot' => '/dev/null']);
50+
new Instance([
51+
'name' => 'Test Instance',
52+
'contentRoot' => '/dev/null',
53+
'color' => 'black',
54+
'plugin' => $this->plugin
55+
]);
5156
}
5257

5358
/**
@@ -123,8 +128,11 @@ public function testIsCurrent()
123128
$this->assertTrue($this->instance->isCurrent());
124129

125130
mkdir($this->contentRoot1 . '/dir');
126-
$instance = $this->instance->clone([
127-
'contentRoot' => $this->contentRoot1 . '/dir'
131+
$instance = new Instance([
132+
'name' => 'Test Instance',
133+
'contentRoot' => $this->contentRoot1 . '/dir',
134+
'color' => 'black',
135+
'plugin' => $this->plugin
128136
]);
129137
$this->assertFalse($instance->isCurrent());
130138
}
@@ -135,11 +143,7 @@ public function testIsCurrent()
135143
* @covers ::contentRoot
136144
* @covers ::currentCommit
137145
* @covers ::name
138-
* @covers ::setColor
139-
* @covers ::setContentRoot
140146
* @covers ::setCurrentCommit
141-
* @covers ::setName
142-
* @covers ::setPlugin
143147
*/
144148
public function testMeta()
145149
{
@@ -149,8 +153,12 @@ public function testMeta()
149153
$this->assertSame('Test Instance', $this->instance->name());
150154

151155
// optional property
152-
$instance = $this->instance->clone([
153-
'currentCommit' => 'abcdefg'
156+
$instance = new Instance([
157+
'name' => 'Test Instance',
158+
'contentRoot' => $this->contentRoot1,
159+
'currentCommit' => 'abcdefg',
160+
'color' => 'black',
161+
'plugin' => $this->plugin
154162
]);
155163
$this->assertSame('abcdefg', $instance->currentCommit());
156164
}

0 commit comments

Comments
 (0)