Skip to content

Commit ce6cbd9

Browse files
committed
Add phpstan type to NewsHandler
1 parent f4cbda7 commit ce6cbd9

1 file changed

Lines changed: 77 additions & 6 deletions

File tree

src/News/NewsHandler.php

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,41 @@
55
namespace phpweb\News;
66

77
use DateTimeImmutable;
8-
98
use function array_filter;
109
use function array_values;
1110
use function is_array;
1211

12+
/**
13+
* @phpstan-type NewsEntryStruct array{
14+
* title: string,
15+
* id: string,
16+
* published: string,
17+
* updated: string,
18+
* link?: list<array{
19+
* href: string,
20+
* rel: string,
21+
* type?: string,
22+
* }>,
23+
* category: list<array{
24+
* term: string,
25+
* label: string,
26+
* }>,
27+
* newsImage?: array{
28+
* link: string,
29+
* content: string,
30+
* },
31+
* content: string,
32+
* intro?: string,
33+
* finalTeaserDate?: string,
34+
* }
35+
*/
1336
final class NewsHandler
1437
{
1538
private const MAX_FRONT_PAGE_NEWS = 25;
1639

40+
/**
41+
* @return NewsEntryStruct|null
42+
*/
1743
public function getLastestNews(): array|null
1844
{
1945
$news = $this->getPregeneratedNews();
@@ -24,8 +50,14 @@ public function getLastestNews(): array|null
2450
return $news[0];
2551
}
2652

27-
/** @return list<array> */
28-
public function getFrontPageNews(): array
53+
/**
54+
* Unused - Looks up generated news with frontpage tags.
55+
*
56+
* Currently unused as the front page no longer displays news.
57+
*
58+
* @return list<NewsEntryStruct>
59+
*/
60+
public function getFrontPageNews(int $limit = self::MAX_FRONT_PAGE_NEWS): array
2961
{
3062
$frontPage = [];
3163
foreach ($this->getPregeneratedNews() as $entry) {
@@ -35,7 +67,7 @@ public function getFrontPageNews(): array
3567
}
3668

3769
$frontPage[] = $entry;
38-
if (count($frontPage) >= self::MAX_FRONT_PAGE_NEWS) {
70+
if (count($frontPage) >= $limit) {
3971
break 2;
4072
}
4173
}
@@ -44,7 +76,9 @@ public function getFrontPageNews(): array
4476
return $frontPage;
4577
}
4678

47-
/** @return list<array> */
79+
/**
80+
* @return list<NewsEntryStruct>
81+
*/
4882
public function getConferences(): array
4983
{
5084
$conferences = [];
@@ -62,7 +96,9 @@ public function getConferences(): array
6296
return $conferences;
6397
}
6498

65-
/** @return list<array> */
99+
/**
100+
* @return list<NewsEntryStruct>
101+
*/
66102
public function getNewsByYear(int $year): array
67103
{
68104
return array_values(array_filter(
@@ -71,11 +107,46 @@ public function getNewsByYear(int $year): array
71107
));
72108
}
73109

110+
/**
111+
* @return list<NewsEntryStruct>
112+
*/
74113
public function getPregeneratedNews(): array
75114
{
76115
$NEWS_ENTRIES = null;
77116
include __DIR__ . '/../../include/pregen-news.inc';
78117

118+
/** @phpstan-ignore-next-line - pregen-news sets global variable */
79119
return is_array($NEWS_ENTRIES) ? $NEWS_ENTRIES : [];
80120
}
121+
122+
123+
/**
124+
* @param NewsEntryStruct $data
125+
*/
126+
public static function parseTeaserCutoff(array $data): ?DateTimeImmutable
127+
{
128+
$finalTeaserDate = $data['finalTeaserDate'] ?? null;
129+
if (!$finalTeaserDate) {
130+
return null;
131+
}
132+
133+
return new DateTimeImmutable($finalTeaserDate);
134+
}
135+
136+
/**
137+
* @param NewsEntryStruct $data
138+
* @param list<string>|string $tags
139+
*/
140+
public static function isTagged(array $data, array|string $tags): bool
141+
{
142+
$tags = is_array($tags) ? $tags : [$tags];
143+
144+
foreach ($data['category'] as $category) {
145+
if (in_array($category['term'], $tags, true)) {
146+
return true;
147+
}
148+
}
149+
150+
return false;
151+
}
81152
}

0 commit comments

Comments
 (0)