TagPosts   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 18
loc 92
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A componentDetails() 0 7 1
A prepareVars() 0 6 1
A prepareContextItem() 0 7 1
A getPostsQuery() 0 18 2
A defineProperties() 18 18 1

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
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Rainlab\Blog\Models\Post;
6
use GinoPane\BlogTaxonomy\Plugin;
7
use GinoPane\BlogTaxonomy\Models\Tag;
8
use GinoPane\BlogTaxonomy\Classes\PostListAbstract;
9
10
/**
11
 * Class TagPosts
12
 *
13
 * @package GinoPane\BlogTaxonomy\Components
14
 */
15
class TagPosts extends PostListAbstract
16
{
17
    const NAME = 'postsWithTag';
18
19
    /**
20
     * @var Tag
21
     */
22
    public $tag;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $includeSeriesPosts = false;
28
29
    /**
30
     * Component Registration
31
     * @return array
32
     */
33
    public function componentDetails(): array
34
    {
35
        return [
36
            'name'        => Plugin::LOCALIZATION_KEY . 'components.tag_posts.name',
37
            'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.description'
38
        ];
39
    }
40
41
    /**
42
     * Component Properties
43
     * @return array
44
     */
45 View Code Duplication
    public function defineProperties(): array
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...
46
    {
47
        return [
48
            'tag' => [
49
                'title'         => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_title',
50
                'description'   => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_description',
51
                'default'       => '{{ :tag }}',
52
                'type'          => 'string'
53
            ],
54
            'includeSeriesPosts' => [
55
                'title'         => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_title',
56
                'description'   => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_description',
57
                'default'       => false,
58
                'type'          => 'checkbox',
59
                'showExternalParam' => false
60
            ]
61
        ] + parent::defineProperties();
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    protected function prepareVars()
68
    {
69
        parent::prepareVars();
70
71
        $this->includeSeriesPosts = $this->property('includeSeriesPosts', false);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    protected function prepareContextItem()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        // load tag
80
        $this->tag = $this->page['tag'] = Tag::whereTranslatable('slug', $this->property('tag'))->first();
81
82
        return $this->tag;
83
    }
84
85
    /**
86
     * @return mixed
87
     */
88
    protected function getPostsQuery()
89
    {
90
        $query = Post::whereHas('tags', function ($query) {
91
            $query->whereTranslatable('slug', $this->tag->slug);
92
        });
93
94
        if ($this->includeSeriesPosts) {
95
            $query->orWhereHas('series', function ($query) {
96
                $query->whereHas('tags', function ($query) {
97
                    $query->whereTranslatable('slug', $this->tag->slug);
98
                });
99
            });
100
        }
101
102
        $query->isPublished();
103
104
        return $query;
105
    }
106
}
107