PostAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 41.51 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 22
loc 53
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 22 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Web\Action;
6
7
use Article\Entity\ArticleType;
8
use Article\Service\PostService;
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Zend\Diactoros\Response\HtmlResponse;
12
use Zend\Expressive\Template\TemplateRendererInterface as Template;
13
14
/**
15
 * Class PostAction.
16
 */
17
class PostAction
18
{
19
    /** @var Template */
20
    private $template;
21
22
    /** @var PostService */
23
    private $postService;
24
25
    /**
26
     * PostAction constructor.
27
     *
28
     * @param Template $template
29
     */
30
    public function __construct(Template $template, PostService $postService)
31
    {
32
        $this->template = $template;
33
        $this->postService = $postService;
34
    }
35
36
    /**
37
     * Executed when action is invoked.
38
     *
39
     * @param Request       $request
40
     * @param Response      $response
41
     * @param callable|null $next
42
     *
43
     * @throws \Exception
44
     *
45
     * @return HtmlResponse
46
     */
47 View Code Duplication
    public function __invoke(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
        Request $request,
49
        Response $response,
50
        callable $next = null
51
    ) {
52
        $categorySlug = $request->getAttribute('segment_1');
0 ignored issues
show
Unused Code introduced by
$categorySlug is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
        $postSlug = $request->getAttribute('segment_2');
54
        $post = $this->postService->fetchSingleArticleBySlug($postSlug);
55
56
        if (!$post || $post->type != ArticleType::POST) {
57
            return $next($request, $response);
58
        }
59
60
        list($previousPost, $nextPost) = $this->postService->fetchNearestArticle($post->published_at);
61
62
        return new HtmlResponse($this->template->render('web::post', [
63
            'layout'   => 'layout/web',
64
            'post'     => $post,
65
            'previous' => $previousPost,
66
            'next'     => $nextPost,
67
        ]));
68
    }
69
}
70