BlogController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 22
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use App\Blog\Archive\ArchiveRepository;
8
use App\Blog\Post\PostRepository;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Blog\PostRepository. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use App\Blog\Tag\TagRepository;
10
use Psr\Http\Message\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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
use Yiisoft\Data\Paginator\OffsetPaginator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Paginator\OffsetPaginator 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...
12
use Yiisoft\Router\HydratorAttribute\RouteArgument;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\HydratorAttribute\RouteArgument 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...
13
use Yiisoft\User\CurrentUser;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\CurrentUser 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...
14
use Yiisoft\Yii\View\Renderer\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\Renderer\ViewRenderer 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...
15
16
final class BlogController
17
{
18
    private const POSTS_PER_PAGE = 3;
19
    private const POPULAR_TAGS_COUNT = 10;
20
    private const ARCHIVE_MONTHS_COUNT = 12;
21
22
    private ViewRenderer $viewRenderer;
23
24
    public function __construct(ViewRenderer $viewRenderer)
25
    {
26
        $this->viewRenderer = $viewRenderer->withControllerName('blog');
27
    }
28
29
    public function index(
30
        PostRepository $postRepository,
31
        TagRepository $tagRepository,
32
        ArchiveRepository $archiveRepo,
33
        CurrentUser $currentUser,
34
        #[RouteArgument('page')] $pageNum = 1,
35
    ): Response {
36
        $dataReader = $postRepository->findAllPreloaded();
37
        $paginator = (new OffsetPaginator($dataReader))
38
            ->withPageSize(self::POSTS_PER_PAGE)
39
            ->withCurrentPage((int)$pageNum);
40
41
        $data = [
42
            'paginator' => $paginator,
43
            'archive' => $archiveRepo
44
                ->getFullArchive()
45
                ->withLimit(self::ARCHIVE_MONTHS_COUNT),
46
            'tags' => $tagRepository->getTagMentions(self::POPULAR_TAGS_COUNT),
47
            'isGuest' => $currentUser->isGuest(),
48
        ];
49
50
        return $this->viewRenderer->render('index', $data);
51
    }
52
}
53