TagRepository::getTagMentions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 1
dl 0
loc 96
rs 9.376
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Tag;
6
7
use App\Blog\Entity\Post;
8
use App\Blog\Entity\PostTag;
9
use App\Blog\Entity\Tag;
10
use App\Blog\Post\PostRepository;
11
use Cycle\ORM\ORMInterface;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\ORMInterface 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 Cycle\ORM\Select;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Select 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 Cycle\ORM\Select\Repository;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Select\Repository 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\Data\Cycle\Reader\EntityReader;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Cycle\Reader\EntityReader 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
use Yiisoft\Data\Reader\DataReaderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Reader\DataReaderInterface 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...
16
use Yiisoft\Data\Reader\Sort;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Reader\Sort 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...
17
18
final class TagRepository extends Repository
19
{
20
    public function __construct(private ORMInterface $orm, Select $select)
21
    {
22
        parent::__construct($select);
23
    }
24
25
    public function getOrCreate(string $label): Tag
26
    {
27
        $tag = $this->findByLabel($label);
28
29
        return $tag ?? new Tag($label);
30
    }
31
32
    public function findByLabel(string $label): ?Tag
33
    {
34
        return $this
35
            ->select()
36
            ->where(['label' => $label])
37
            ->fetchOne();
38
    }
39
40
    /**
41
     * @param int $limit
42
     *
43
     * @return DataReaderInterface Collection of Array('label' => 'Tag Label', 'count' => '8')
44
     */
45
    public function getTagMentions(int $limit = 0): DataReaderInterface
46
    {
47
        /** @var Repository $postTagRepo */
48
        $postTagRepo = $this->orm->getRepository(PostTag::class);
49
        /** @var PostRepository $postRepo */
50
        $postRepo = $this->orm->getRepository(Post::class);
51
52
        // For example, below are several ways to make queries
53
        // As a result, we should get a list of most used tags
54
        // All SQL-queries received on mysql database. SQL-queries may vary by driver
55
56
        /**
57
         * Case 1 would look like:.
58
         *
59
         * SELECT `t`.`label`, count(*) `count`
60
         * FROM `post_tag` AS `postTag`
61
         * INNER JOIN `post` AS `p`
62
         * ON `p`.`id` = `postTag`.`post_id` AND `p`.`public` = TRUE
63
         * INNER JOIN `tag` AS `t`
64
         * ON `t`.`id` = `postTag`.`tag_id`
65
         * GROUP BY `t`.`label`, `tag_id`
66
         * ORDER BY `count` DESC
67
         */
68
        $case1 = $postTagRepo
0 ignored issues
show
Unused Code introduced by
The assignment to $case1 is dead and can be removed.
Loading history...
69
            ->select()
70
            ->buildQuery()
71
            ->columns(['t.label', 'count(*) count'])
72
            ->innerJoin('post', 'p')
73
            ->on('p.id', 'postTag.post_id')
74
            ->onWhere(['p.public' => true])
75
            ->innerJoin('tag', 't')
76
            ->on('t.id', 'postTag.tag_id')
77
            ->groupBy('t.label, tag_id');
78
79
        /**
80
         * Case 2 would look like:.
81
         *
82
         * SELECT `label`, count(*) `count`
83
         * FROM `tag` AS `tag`
84
         * INNER JOIN `post_tag` AS `tag_posts_pivot`
85
         * ON `tag_posts_pivot`.`tag_id` = `tag`.`id`
86
         * INNER JOIN `post` AS `tag_posts`
87
         * ON `tag_posts`.`id` = `tag_posts_pivot`.`post_id` AND `tag_posts`.`public` = TRUE
88
         * GROUP BY `tag`.`label`, `tag_id`
89
         * ORDER BY `count` DESC
90
         */
91
        $case2 = $this
0 ignored issues
show
Unused Code introduced by
The assignment to $case2 is dead and can be removed.
Loading history...
92
            ->select()
93
            ->with('posts')
94
            ->buildQuery()
95
            ->columns(['label', 'count(*) count'])
96
            ->groupBy('tag.label, tag_id');
97
98
        /**
99
         * Case 3 would look like:.
100
         *
101
         * SELECT `label`, count(*) `count`
102
         * FROM `tag` AS `tag`
103
         * INNER JOIN `post_tag` AS `tag_posts_pivot`
104
         * ON `tag_posts_pivot`.`tag_id` = `tag`.`id`
105
         * INNER JOIN `post` AS `tag_posts`
106
         * ON `tag_posts`.`id` = `tag_posts_pivot`.`post_id` AND `tag_posts`.`public` = TRUE
107
         * GROUP BY `tag_posts_pivot`.`tag_id`, `tag`.`label`
108
         * ORDER BY `count` DESC
109
         */
110
        $case3 = $this
111
            ->select()
112
            ->groupBy('[email protected]_id') // relation posts -> pivot (@) -> column
113
            ->groupBy('label')
114
            ->buildQuery()
115
            ->columns(['label', 'count(*) count']);
116
117
        /**
118
         * Case 4 would look like:.
119
         *
120
         * SELECT `label`, count(*) `count`
121
         * FROM `post` AS `post`
122
         * INNER JOIN `post_tag` AS `post_tags_pivot`
123
         * ON `post_tags_pivot`.`post_id` = `post`.`id`
124
         * INNER JOIN `tag` AS `post_tags`
125
         * ON `post_tags`.`id` = `post_tags_pivot`.`tag_id`
126
         * WHERE `post`.`public` = TRUE
127
         * GROUP BY `post_tags_pivot`.`tag_id`, `tag`.`label`
128
         */
129
        $case4 = $postRepo
0 ignored issues
show
Unused Code introduced by
The assignment to $case4 is dead and can be removed.
Loading history...
130
            ->select()
131
            ->groupBy('[email protected]_id') // relation tags -> pivot (@) -> column
132
            ->groupBy('tags.label')
133
            ->buildQuery()
134
            ->columns(['label', 'count(*) count']);
135
136
        $sort = Sort::only(['count', 'label'])->withOrder(['count' => 'desc']);
137
138
        return (new EntityReader($case3))
139
            ->withSort($sort)
140
            ->withLimit($limit);
141
    }
142
}
143