Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

PostCard::renderBody()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 2
nop 0
dl 0
loc 23
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Modules\Blog\Widget;
6
7
use App\Modules\Blog\Entity\Post;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Router\UrlGeneratorInterface;
10
use Yiisoft\Yii\Bootstrap5\Widget;
11
use function mb_strlen;
12
use function mb_substr;
13
14
final class PostCard extends Widget
15
{
16
    private ?Post $post = null;
17
18
    private array $options = [];
19
20
    public function __construct(private UrlGeneratorInterface $urlGenerator)
21
    {
22
    }
23
24
    protected function run(): string
25
    {
26
        if (!isset($this->options['id'])) {
27
            $this->options['id'] = "{$this->getId()}-post-card";
28
        }
29
30
        $this->initOptions();
31
32
        return implode("\n", [
33
            Html::openTag('div', $this->options),
34
            Html::openTag('div', ['class' => 'card-body d-flex flex-column align-items-start']),
35
            $this->renderHead(),
36
            $this->renderBody(),
37
            $this->renderTags(),
38
            Html::closeTag('div'),
39
            Html::closeTag('div'),
40
        ]);
41
    }
42
43
    protected function renderHead(): string
44
    {
45
        return Html::a(
46
            $this->post->getTitle(),
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            $this->post->/** @scrutinizer ignore-call */ 
47
                         getTitle(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
            $this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]),
48
            ['class' => 'mb-0 h4 text-decoration-none'] // stretched-link
49
        )
50
            ->render();
51
    }
52
53
    protected function renderBody(): string
54
    {
55
        $return = Html::openTag('div', ['class' => 'card-text mb-auto']);
56
        $return .= $this->post->getPublishedAt() === null
57
            ? 'not published'
58
            : $this->post
59
                ->getPublishedAt()
60
                ->format('M, d');
61
        $return .= ' by ';
62
        $return .= Html::a(
63
            $this->post
64
                ->getUser()
65
                ?->getLogin(),
66
            $this->urlGenerator->generate('user/profile', ['login' => $this->post
67
                ->getUser()
68
                ?->getLogin(), ])
69
        )->addClass('mb-1 text-muted');
70
71
        $return .= Html::p(
72
            mb_substr($this->post->getContent(), 0, 400)
73
            . (mb_strlen($this->post->getContent()) > 400 ? '…' : '')
74
        );
75
        return $return . Html::closeTag('div');
76
    }
77
78
    protected function renderTags(): string
79
    {
80
        $return = Html::openTag('div', ['class' => 'mt-3']);
81
        foreach ($this->post->getTags() as $tag) {
82
            $return .= Html::a(
83
                $tag->getLabel(),
84
                $this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]),
85
                ['class' => 'btn btn-outline-secondary btn-sm me-2 mt-1']
86
            );
87
        }
88
        return $return . Html::closeTag('div');
89
    }
90
91
    protected function initOptions(): void
92
    {
93
        Html::addCssClass($this->options, ['widget' => 'card mb-4']);
94
    }
95
96
    public function post(?Post $post): self
97
    {
98
        $this->post = $post;
99
100
        if ($post !== null) {
101
            $this->options['data']['post-slug'] = $post->getSlug();
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * The HTML attributes for the widget container tag. The following special options are recognized.
109
     *
110
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
111
     */
112
    public function options(array $value): self
113
    {
114
        $this->options = $value;
115
116
        return $this;
117
    }
118
}
119