Skip to content

Commit e3860bb

Browse files
committed
Type handling for PHPStan recommendations.
1 parent c74aa81 commit e3860bb

File tree

8 files changed

+78
-39
lines changed

8 files changed

+78
-39
lines changed

src/Mods/Collection/DataLoader/Type/FileDataLoader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public function getIDsByCategory(BaseCategory $category) : array
6767
$ids = array();
6868

6969
foreach($category->getDataFiles() as $jsonFile) {
70-
if($jsonFile instanceof JSONFile) {
71-
$ids[] = $jsonFile->getBaseName();
72-
}
70+
$ids[] = $jsonFile->getBaseName();
7371
}
7472

7573
return $ids;

src/Mods/Mod/Screenshots/ModScreenshot.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
<?php
2+
/**
3+
* @package Mods
4+
* @subpackage Screenshots
5+
*/
26

37
declare(strict_types=1);
48

59
namespace CPMDB\Mods\Mod\Screenshots;
610

7-
use AppUtils\ArrayDataCollection;
811
use AppUtils\FileHelper\FileInfo;
912

13+
/**
14+
* Information on a single screenshot for a mod.
15+
*
16+
* @package Mods
17+
* @subpackage Screenshots
18+
*/
1019
class ModScreenshot implements ModScreenshotInterface
1120
{
12-
public const META_KEY_TITLE = 'title';
13-
1421
private FileInfo $imageFile;
1522
private string $id;
1623
private ModScreenshotCollection $collection;
1724
private string $imageURL;
18-
private ArrayDataCollection $metaData;
25+
private ScreenshotMetaData $metaData;
1926

20-
public function __construct(ModScreenshotCollection $collection, string $screenshotID, FileInfo $imageFile, string $imageURL, array $metaData)
27+
/**
28+
* @param ModScreenshotCollection $collection
29+
* @param string $screenshotID
30+
* @param FileInfo $imageFile
31+
* @param string $imageURL
32+
* @param ScreenshotMetaData $metaData
33+
*/
34+
public function __construct(ModScreenshotCollection $collection, string $screenshotID, FileInfo $imageFile, string $imageURL, ScreenshotMetaData $metaData)
2135
{
2236
$this->collection = $collection;
2337
$this->imageFile = $imageFile;
2438
$this->id = $screenshotID;
2539
$this->imageURL = $imageURL;
26-
$this->metaData = ArrayDataCollection::create($metaData);
40+
$this->metaData = $metaData;
2741
}
2842

2943
public function getID() : string
@@ -48,15 +62,6 @@ public function getImageFile() : FileInfo
4862

4963
public function getTitle() : string
5064
{
51-
$title = $this->metaData->getString(self::META_KEY_TITLE);
52-
if(!empty($title)) {
53-
return $title;
54-
}
55-
56-
if($this->isDefault()) {
57-
return 'Default mod screenshot';
58-
}
59-
60-
return '';
65+
return $this->metaData->getTitle();
6166
}
6267
}

src/Mods/Mod/Screenshots/ModScreenshotCollection.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
<?php
2+
/**
3+
* @package Mods
4+
* @subpackage Screenshots
5+
*/
26

37
declare(strict_types=1);
48

59
namespace CPMDB\Mods\Mod\Screenshots;
610

11+
use AppUtils\ArrayDataCollection;
712
use AppUtils\Collections\BaseStringPrimaryCollection;
813
use AppUtils\FileHelper\FileInfo;
914
use AppUtils\FileHelper\FolderInfo;
1015
use AppUtils\FileHelper\JSONFile;
1116
use CPMDB\Mods\Mod\ModInfoInterface;
17+
use function AppUtils\t;
18+
use const CPMDB\Assets\KEY_SCREENSHOT_TITLE;
1219

1320
/**
1421
* Collection of screenshot files available for a mod.
1522
*
16-
* @package CPMDB
17-
* @subpackage Mod Collection
23+
* @package Mods
24+
* @subpackage Screenshots
1825
*
1926
* @method ModScreenshotInterface getByID(string $id)
2027
* @method ModScreenshotInterface getDefault()
@@ -23,7 +30,7 @@
2330
class ModScreenshotCollection extends BaseStringPrimaryCollection
2431
{
2532
public const DEFAULT_ID = '000default';
26-
private const KEY_SCREENSHOT_ID = 'screenshotID';
33+
public const KEY_SCREENSHOT_ID = 'screenshotID';
2734

2835
private ModInfoInterface $mod;
2936

@@ -64,18 +71,18 @@ public function getDefaultID() : string
6471

6572
protected function registerItems(): void
6673
{
67-
foreach($this->getDescriptions() as $def) {
74+
foreach($this->getMetaData() as $def) {
6875
$this->registerScreenshot($def);
6976
}
7077
}
7178

7279
/**
73-
* @param array<string,scalar> $def
80+
* @param ScreenshotMetaData $def
7481
* @return void
7582
*/
76-
private function registerScreenshot(array $def) : void
83+
private function registerScreenshot(ScreenshotMetaData $def) : void
7784
{
78-
$fileName = $this->resolveFileName($def[self::KEY_SCREENSHOT_ID]);
85+
$fileName = $this->resolveFileName($def->getScreenshotID());
7986
$file = FileInfo::factory($this->getScreensFolder().'/'.$fileName);
8087

8188
if(!$file->exists()) {
@@ -84,7 +91,7 @@ private function registerScreenshot(array $def) : void
8491

8592
$this->registerItem(new ModScreenshot(
8693
$this,
87-
$def[self::KEY_SCREENSHOT_ID],
94+
$def->getScreenshotID(),
8895
$file,
8996
$this->getScreensURL().'/'.$fileName,
9097
$def
@@ -113,28 +120,42 @@ public function getSidecarFile() : JSONFile
113120
* screenshot. If no sidecar file is present, only the default
114121
* screenshot is returned.
115122
*
116-
* @return array<int,array{string,scalar}>
123+
* @return ScreenshotMetaData[]
117124
*/
118-
public function getDescriptions() : array
125+
public function getMetaData() : array
119126
{
120127
$sidecarFile = $this->getSidecarFile();
121128

122129
$descriptions = array();
123130

124-
$descriptions[] = array(
125-
self::KEY_SCREENSHOT_ID => self::DEFAULT_ID
126-
);
131+
$descriptions[] = new ScreenshotMetaData(self::DEFAULT_ID, t('Default mod screenshot'));
127132

128133
if($sidecarFile->exists()) {
129134
foreach($sidecarFile->getData() as $suffix => $def) {
130-
$def[self::KEY_SCREENSHOT_ID] = $suffix;
131-
$descriptions[] = $def;
135+
$descriptions[] = $this->resolveMetaData((string)$suffix, $def);
132136
}
133137
}
134138

135139
return $descriptions;
136140
}
137141

142+
/**
143+
* @param string $suffix
144+
* @param mixed $def
145+
* @return ScreenshotMetaData
146+
*/
147+
private function resolveMetaData(string $suffix, mixed $def) : ScreenshotMetaData
148+
{
149+
$title = '';
150+
151+
if(is_array($def)) {
152+
$data = ArrayDataCollection::create($def);
153+
$title = $data->getString(KEY_SCREENSHOT_TITLE);
154+
}
155+
156+
return new ScreenshotMetaData($suffix, $title);
157+
}
158+
138159
private function resolveFileName(?string $suffix=null) : string
139160
{
140161
$fileName = $this->mod->getModID();

src/Mods/Mod/Screenshots/ModScreenshotInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
2+
/**
3+
* @package Mods
4+
* @subpackage Screenshots
5+
*/
26

37
declare(strict_types=1);
48

@@ -7,6 +11,13 @@
711
use AppUtils\FileHelper\FileInfo;
812
use AppUtils\Interfaces\StringPrimaryRecordInterface;
913

14+
/**
15+
* Interface for a mod screenshot.
16+
* This is implemented in {@see ModScreenshot}.
17+
*
18+
* @package Mods
19+
* @subpackage Screenshots
20+
*/
1021
interface ModScreenshotInterface extends StringPrimaryRecordInterface
1122
{
1223
public function isDefault() : bool;

src/Mods/Tags/TagCollection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public static function mergeTags(...$tagLists) : array
5656
$tags = array();
5757

5858
foreach($tagLists as $tagList) {
59-
if(is_array($tagList)) {
60-
array_push($tags, ...self::filterTags($tagList));
61-
}
59+
array_push($tags, ...self::filterTags($tagList));
6260
}
6361

6462
$result = array_unique($tags);

tests/CPMDBTests/TestSuites/Ateliers/AtelierTests.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CPMDB\Mods\Ateliers\Atelier\CubAtelierStore;
88
use CPMDB\Mods\Ateliers\Atelier\NcFashionAtelier;
99
use CPMDB\Mods\Ateliers\AtelierNames;
10+
use CPMDB\Mods\Clothing\ClothingModInfo;
1011
use CPMDBTEsts\TestClasses\CPMDBTestCase;
1112

1213
final class AtelierTests extends CPMDBTestCase
@@ -47,6 +48,10 @@ public function test_getAtelier() : void
4748
{
4849
$mod = $this->createCollection()->getByID('clothing.cute-zipper-top');
4950

50-
$this->assertSame(CubAtelierStore::ATELIER_ID, $mod->getAtelier()->getID());
51+
$this->assertInstanceOf(ClothingModInfo::class, $mod);
52+
53+
$atelier = $mod->getAtelier();
54+
$this->assertNotNull($atelier);
55+
$this->assertSame(CubAtelierStore::ATELIER_ID, $atelier->getID());
5156
}
5257
}

tests/CPMDBTests/TestSuites/Items/CollectionTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function test_multi() : void
3838
->selectTag(Physics::TAG_NAME)
3939
->getItemsAsCollection();
4040

41-
$this->assertNotEmpty($items);
41+
$this->assertTrue($items->countRecords() > 0);
4242
$this->assertTrue($items->itemCodeExists('earrings_08_basic_04_kwek'));
4343
}
4444
}

tools/generate-atelier-classes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function generateAtelierCollectionClass() : void
111111
* @method AtelierInterface getByID(string $id)
112112
* @method AtelierInterface[] getAll()
113113
* @method AtelierInterface getDefault()
114+
* @property array<string,AtelierInterface> $items
114115
*/
115116
class AtelierCollection extends BaseStringPrimaryCollection
116117
{

0 commit comments

Comments
 (0)