Passed
Pull Request — master (#566)
by Rustam
01:47
created

PostCard::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Widget;
6
7
use App\Blog\Entity\Post;
8
use Yiisoft\Html\Html;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Html\Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Yiisoft\Router\UrlGeneratorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\UrlGeneratorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Yiisoft\Yii\Bootstrap5\Widget;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Bootstrap5\Widget was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
final class PostCard extends Widget
13
{
14
    private ?Post $post = null;
15
16
    private array $options = [];
17
18
    public function __construct(private UrlGeneratorInterface $urlGenerator)
19
    {
20
    }
21
22
    public function render(): string
23
    {
24
        if (!isset($this->options['id'])) {
25
            $this->options['id'] = "{$this->getId()}-post-card";
26
        }
27
28
        $this->initOptions();
29
30
        return implode("\n", [
31
            Html::openTag('div', $this->options),
32
            Html::openTag('div', ['class' => 'card-body d-flex flex-column align-items-start']),
33
            $this->renderHead(),
34
            $this->renderBody(),
35
            $this->renderTags(),
36
            Html::closeTag('div'),
37
            Html::closeTag('div'),
38
        ]);
39
    }
40
41
    protected function renderHead(): string
42
    {
43
        return Html::a(
44
            $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

44
            $this->post->/** @scrutinizer ignore-call */ 
45
                         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...
45
            $this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]),
46
            ['class' => 'mb-0 h4 text-decoration-none'] // stretched-link
47
        )
48
            ->render();
49
    }
50
51
    protected function renderBody(): string
52
    {
53
        $return = Html::openTag('div', ['class' => 'card-text mb-auto']);
54
        $return .= $this->post->getPublishedAt() === null
55
            ? 'not published'
56
            : $this->post
57
                ->getPublishedAt()
58
                ->format('M, d');
59
        $return .= ' by ';
60
        $return .= Html::a(
61
            $this->post
62
                ->getUser()
63
                ->getLogin(),
64
            $this->urlGenerator->generate('user/profile', ['login' => $this->post
65
                ->getUser()
66
                ->getLogin(), ])
67
        )->class('mb-1 text-muted');
68
69
        $return .= Html::p(
70
            mb_substr($this->post->getContent(), 0, 400)
71
            . (mb_strlen($this->post->getContent()) > 400 ? '…' : '')
72
        );
73
74
        return $return . Html::closeTag('div');
75
    }
76
77
    protected function renderTags(): string
78
    {
79
        $return = Html::openTag('div', ['class' => 'mt-3']);
80
        foreach ($this->post->getTags() as $tag) {
81
            $return .= Html::a(
82
                $tag->getLabel(),
83
                $this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]),
84
                ['class' => 'btn btn-outline-secondary btn-sm me-2 mt-1']
85
            );
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