BlogCell::sidebar()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 58
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 9.639
c 0
b 0
f 0
cc 2
eloc 39
nc 1
nop 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
namespace App\View\Cell;
3
4
use Cake\View\Cell;
5
6
class BlogCell extends Cell
7
{
8
9
    /**
10
     * Display the sidebar on the blog pages.
11
     *
12
     * @return void
13
     */
14
    public function sidebar()
15
    {
16
        $this->loadModel('BlogCategories');
17
        $this->loadModel('BlogArticles');
18
19
        $articleSearch = $this->BlogArticles->newEntity($this->request->data);
0 ignored issues
show
Bug introduced by
The property BlogArticles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Deprecated Code introduced by
The property Cake\Http\ServerRequest::$data has been deprecated with message: 3.4.0 This public property will be removed in 4.0.0. Use getData() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
20
21
        //Select all Categories.
22
        $categories = $this->BlogCategories
0 ignored issues
show
Bug introduced by
The property BlogCategories does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
            ->find()
24
            ->select([
25
                'id',
26
                'title',
27
                'article_count'
28
            ])
29
            ->all();
30
31
        //Select featured article.
32
        $featured = $this->BlogArticles
33
            ->find()
34
            ->select([
35
                'id',
36
                'title',
37
                'created',
38
                'comment_count'
39
            ])
40
            ->contain([
41
                'Users' => function ($q) {
42
                    return $q->find('short');
43
                }
44
            ])
45
            ->where([
46
                'BlogArticles.id !=' => ($this->request->id) ? $this->request->id : 0,
47
                'BlogArticles.is_display' => 1
48
            ])
49
            ->order([
50
                'BlogArticles.created' => 'desc'
51
            ])
52
            ->first();
53
54
        //Select all articles and group them by monthly.
55
        $archives = $this->BlogArticles
56
            ->find('all')
57
            ->select([
58
                'date' => 'DATE(created)',
59
                'count' => 'COUNT(*)'
60
            ])
61
            ->group('SUBSTR(DATE(created), 1, 7)')
62
            ->order([
63
                'date' => 'desc'
64
            ])
65
            ->where([
66
                'is_display' => 1
67
            ])
68
            ->toArray();
69
70
        $this->set(compact('categories', 'featured', 'archives', 'articleSearch'));
71
    }
72
}
73