Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Psalm Don't add new properties to Row #216

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions site/app/Articles/Articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getAll(?int $limit = null): array
NULL AS slug,
a.href,
a.date AS published,
a.excerpt AS excerptTexy,
a.excerpt AS leadTexy,
null AS textTexy,
s.name AS sourceName,
s.href AS sourceHref,
Expand Down Expand Up @@ -121,7 +121,7 @@ public function getAllByTags(array $tags, ?int $limit = null): array
bp.title AS titleTexy,
bp.slug,
bp.published,
bp.lead AS excerptTexy,
bp.lead AS leadTexy,
bp.text AS textTexy,
null AS sourceName,
null AS sourceHref,
Expand Down Expand Up @@ -205,12 +205,11 @@ public function getLabelByTag(string $tag): ?string
LIMIT 1';
$result = $this->database->fetch($query, $this->tags->serialize([$tag]), new DateTime(), $this->translator->getDefaultLocale());
if ($result) {
$result->tags = ($result->tags !== null ? $this->tags->unserialize($result->tags) : []);
$result->slugTags = ($result->slugTags !== null ? $this->tags->unserialize($result->slugTags) : []);

foreach ($result->slugTags as $key => $slug) {
$tags = $result->tags !== null ? $this->tags->unserialize($result->tags) : [];
$slugTags = $result->slugTags !== null ? $this->tags->unserialize($result->slugTags) : [];
foreach ($slugTags as $key => $slug) {
if ($slug === $tag) {
return $result->tags[$key] ?? null;
return $tags[$key] ?? null;
}
}
}
Expand Down Expand Up @@ -278,14 +277,7 @@ private function enrichArticles(array $articles): array
{
$result = [];
foreach ($articles as $article) {
if ($article->sourceHref === null) {
$article->postId = $article->id;
$article->leadTexy = $article->excerptTexy;
$result[] = $this->blogPosts->buildPost($article);
} else {
$article->articleId = $article->id;
$result[] = $this->buildArticle($article);
}
$result[] = $article->sourceHref === null ? $this->blogPosts->buildPost($article) : $this->buildArticle($article);
}
return $result;
}
Expand All @@ -294,11 +286,11 @@ private function enrichArticles(array $articles): array
public function buildArticle(Row $row): ArticlePublishedElsewhere
{
$article = new ArticlePublishedElsewhere();
$article->articleId = $row->articleId;
$article->articleId = $row->id;
$article->titleTexy = $row->titleTexy;
$article->href = $row->href;
$article->published = $row->published;
$article->excerptTexy = $row->excerptTexy;
$article->excerptTexy = $row->leadTexy;
$article->sourceName = $row->sourceName;
$article->sourceHref = $row->sourceHref;

Expand Down
2 changes: 1 addition & 1 deletion site/app/Articles/Blog/BlogPostLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function fetch(string $post, ?string $previewKey = null): ?Row
if ($this->post === null) {
$result = $this->database->fetch(
'SELECT
bp.id_blog_post AS postId,
bp.id_blog_post AS id,
l.id_locale AS localeId,
bp.key_translation_group AS translationGroupId,
l.locale,
Expand Down
6 changes: 3 additions & 3 deletions site/app/Articles/Blog/BlogPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getById(int $id): BlogPost
{
$result = $this->database->fetch(
'SELECT
bp.id_blog_post AS postId,
bp.id_blog_post AS id,
l.id_locale AS localeId,
bp.key_translation_group AS translationGroupId,
l.locale,
Expand Down Expand Up @@ -141,7 +141,7 @@ public function getAll(): array
{
$posts = [];
$sql = 'SELECT
bp.id_blog_post AS postId,
bp.id_blog_post AS id,
l.id_locale AS localeId,
bp.key_translation_group AS translationGroupId,
l.locale,
Expand Down Expand Up @@ -347,7 +347,7 @@ private function getEdits(int $postId): array
public function buildPost(Row $row): BlogPost
{
$post = new BlogPost();
$post->postId = $row->postId;
$post->postId = $row->id;
$post->translationGroupId = $row->translationGroupId;
$post->locale = $row->locale;
$post->localeId = $row->localeId;
Expand Down
20 changes: 16 additions & 4 deletions site/tests/Articles/ArticlesTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ArticlesTest extends TestCase
'slug' => null,
'href' => 'https://example.com/article-1',
'published' => new DateTime('3 years ago'),
'excerptTexy' => 'Excerpt 1',
'leadTexy' => 'Excerpt 1',
'textTexy' => null,
'sourceName' => 'Source 1',
'sourceHref' => 'https://source1.example/',
Expand All @@ -84,7 +84,7 @@ class ArticlesTest extends TestCase
'slug' => 'blog-1',
'href' => null,
'published' => new DateTime('2 years ago'),
'excerptTexy' => 'Lead 1',
'leadTexy' => 'Lead 1',
'textTexy' => 'Text 1',
'sourceName' => null,
'sourceHref' => null,
Expand All @@ -108,7 +108,7 @@ class ArticlesTest extends TestCase
'slug' => null,
'href' => 'https://example.com/article-2',
'published' => new DateTime('1 year ago'),
'excerptTexy' => 'Excerpt 2',
'leadTexy' => 'Excerpt 2',
'textTexy' => null,
'sourceName' => 'Source 2',
'sourceHref' => 'https://source2.example/',
Expand All @@ -132,7 +132,7 @@ class ArticlesTest extends TestCase
'slug' => 'blog-2',
'href' => null,
'published' => new DateTime('1 month ago'),
'excerptTexy' => 'Lead 2',
'leadTexy' => 'Lead 2',
'textTexy' => 'Text 2',
'sourceName' => null,
'sourceHref' => null,
Expand Down Expand Up @@ -194,6 +194,18 @@ class ArticlesTest extends TestCase
}, ShouldNotHappenException::class, 'Nearest published date is a string not a DateTime object');
}


public function testGetLabelByTag(): void
{
Assert::null($this->articles->getLabelByTag('foo'));
$this->database->setFetchResult([
'tags' => '["HTTP Secure", "HTTP/2"]',
'slugTags' => '["https", "http-2"]',
]);
Assert::same('HTTP Secure', $this->articles->getLabelByTag('https'));
Assert::same('HTTP/2', $this->articles->getLabelByTag('http-2'));
}

}

TestCaseRunner::run(ArticlesTest::class);
Loading