Completed
Push — add-polls-feature ( 61fe2b )
by Fèvre
02:22
created

BlogController   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 798
Duplicated Lines 24.56 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 12
dl 196
loc 798
rs 8
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A beforeFilter() 0 6 1
B index() 26 26 1
B category() 5 46 2
B article() 0 145 6
A quote() 29 56 3
A go() 48 48 3
B archive() 28 28 1
B search() 10 43 3
B articleLike() 16 77 5
B articleUnlike() 8 52 4
B deleteComment() 5 51 5
B getEditComment() 16 54 5
B editComment() 5 48 5

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BlogController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BlogController, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace App\Controller;
3
4
use App\Event\Badges;
5
use App\Event\Statistics;
6
use Cake\Core\Configure;
7
use Cake\Event\Event;
8
use Cake\Network\Exception\NotFoundException;
9
use Cake\Routing\Router;
10
11
class BlogController extends AppController
12
{
13
14
    /**
15
     * Initialization hook method.
16
     *
17
     * @return void
18
     */
19
    public function initialize()
20
    {
21
        parent::initialize();
22
23
        $this->loadComponent('RequestHandler');
24
    }
25
26
    /**
27
     * BeforeFilter handle.
28
     *
29
     * @param Event $event The beforeFilter event that was fired.
30
     *
31
     * @return void
32
     */
33
    public function beforeFilter(Event $event)
34
    {
35
        parent::beforeFilter($event);
36
37
        $this->Auth->allow(['index', 'category', 'article', 'go', 'archive', 'search']);
38
    }
39
40
    /**
41
     * Display all Articles.
42
     *
43
     * @return void
44
     */
45 View Code Duplication
    public function index()
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
        $this->loadModel('BlogArticles');
48
        $this->paginate = [
49
            'maxLimit' => Configure::read('Blog.article_per_page')
50
        ];
51
52
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
            ->find()
54
            ->contain([
55
                'BlogCategories',
56
                'Users' => function ($q) {
57
                    return $q->find('short');
58
                }
59
            ])
60
            ->order([
61
                'BlogArticles.created' => 'desc'
62
            ])
63
            ->where([
64
                'BlogArticles.is_display' => 1
65
            ]);
66
67
        $articles = $this->paginate($articles);
68
69
        $this->set(compact('articles'));
70
    }
71
72
    /**
73
     * Display a specific category with all its articles.
74
     *
75
     * @return \Cake\Network\Response|void
76
     */
77
    public function category()
78
    {
79
        $this->loadModel('BlogCategories');
80
81
        $category = $this->BlogCategories
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
            ->find()
83
            ->where([
84
                'BlogCategories.id' => $this->request->id
85
            ])
86
            ->contain([
87
                'BlogArticles'
88
            ])
89
            ->first();
90
91
        //Check if the category is found.
92 View Code Duplication
        if (empty($category)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
            $this->Flash->error(__('This category doesn\'t exist or has been deleted.'));
94
95
            return $this->redirect(['action' => 'index']);
96
        }
97
98
        //Paginate all Articles.
99
        $this->loadModel('BlogArticles');
100
        $this->paginate = [
101
            'maxLimit' => Configure::read('Blog.article_per_page')
102
        ];
103
104
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
            ->find()
106
            ->contain([
107
                'Users' => function ($q) {
108
                    return $q->find('short');
109
                }
110
            ])
111
            ->where([
112
                'BlogArticles.category_id' => $category->id,
113
                'BlogArticles.is_display' => 1
114
            ])
115
            ->order([
116
                'BlogArticles.created' => 'desc'
117
            ]);
118
119
        $articles = $this->paginate($articles);
120
121
        $this->set(compact('category', 'articles'));
122
    }
123
124
    /**
125
     * Display a specific article.
126
     *
127
     * @return \Cake\Network\Response|void
128
     */
129
    public function article()
130
    {
131
        $this->loadModel('BlogArticles');
132
133
        $article = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
            ->find()
135
            ->where([
136
                'BlogArticles.id' => $this->request->id,
137
                'BlogArticles.is_display' => 1
138
            ])
139
            ->contain([
140
                'BlogCategories',
141
                'BlogAttachments',
142
                'Users' => function ($q) {
143
                    return $q->find('full');
144
                },
145
                'Polls',
146
                'Polls.PollsAnswers',
147
                'Polls.PollsAnswers.Polls' => function ($q) {
148
                    return $q->select(['id', 'user_count']);
149
                },
150
                'Polls.PollsUsers'
151
            ])
152
            ->first();
153
154
        $this->loadModel('PollsUsers');
155
        $hasVoted = $this->PollsUsers
0 ignored issues
show
Documentation introduced by
The property PollsUsers does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156
            ->find()
157
            ->contain([
158
                'Polls' => function ($q) {
159
                    return $q->select(['id']);
160
                },
161
                'PollsAnswers'
162
            ])
163
            ->where([
164
                'PollsUsers.user_id' => 1, //$this->Auth->user('id'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
                'Polls.id' => $article->poll->id
166
            ])
167
            ->first();
168
169
        //Check if the article is found.
170
        if (is_null($article)) {
171
            $this->Flash->error(__('This article doesn\'t exist or has been deleted.'));
172
173
            return $this->redirect(['action' => 'index']);
174
        }
175
176
        $this->loadModel('BlogArticlesComments');
177
178
        //A comment has been posted.
179
        if ($this->request->is('post')) {
180
            //Check if the user is connected.
181
            if (!$this->Auth->user()) {
182
                return $this->Flash->error(__('You must be connected to post a comment.'));
183
            }
184
185
            $this->request->data['article_id'] = $article->id;
186
            $this->request->data['user_id'] = $this->Auth->user('id');
187
188
            $newComment = $this->BlogArticlesComments->newEntity($this->request->data, ['validate' => 'create']);
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
189
190
            //Attach Event.
191
            $this->BlogArticlesComments->eventManager()->attach(new Badges($this));
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
193
            if ($insertComment = $this->BlogArticlesComments->save($newComment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
194
                $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
195
                $event = new Event('Model.BlogArticlesComments.new');
196
                $this->eventManager()->dispatch($event);
197
198
                $this->Flash->success(__('Your comment has been posted successfully !'));
199
                //Redirect the user to the last page of the article.
200
                $this->redirect([
201
                    'action' => 'go',
202
                    $insertComment->id
203
                ]);
204
            }
205
        }
206
207
        //Paginate all comments related to the article.
208
        $this->paginate = [
209
            'maxLimit' => Configure::read('Blog.comment_per_page')
210
        ];
211
212
        $comments = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
213
            ->find()
214
            ->where([
215
                'BlogArticlesComments.article_id' => $article->id
216
            ])
217
            ->contain([
218
                'Users' => function ($q) {
219
                    return $q->find('medium');
220
                }
221
            ])
222
            ->order([
223
                'BlogArticlesComments.created' => 'asc'
224
            ]);
225
226
        $comments = $this->paginate($comments);
227
228
        //Select the like for the current auth user.
229
        $this->loadModel('BlogArticlesLikes');
230
        $like = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
231
            ->find()
232
            ->where([
233
                'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null,
234
                'article_id' => $article->id
235
            ])
236
            ->first();
237
238
        //Build the newEntity for the comment form.
239
        $formComments = $this->BlogArticlesComments->newEntity();
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
240
241
        //Search related articles
242
        $keywords = preg_split("/([\s,\W])+/", $article->title);
243
244
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
245
            ->find()
246
            ->contain([
247
                'BlogCategories',
248
            ])
249
            ->where([
250
                'BlogArticles.is_display' => 1,
251
                'BlogArticles.id !=' => $article->id
252
            ])
253
            ->andWhere([
254
                'BlogArticles.title RLIKE' => rtrim(implode('|', $keywords), '|')
255
            ]);
256
257
        //Current user.
258
        $this->loadModel('Users');
259
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
260
            ->find()
261
            ->contain([
262
                'Groups' => function ($q) {
263
                    return $q->select(['id', 'is_staff']);
264
                }
265
            ])
266
            ->where([
267
                'Users.id' => $this->Auth->user('id')
268
            ])
269
            ->select(['id', 'group_id'])
270
            ->first();
271
272
        $this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser', 'hasVoted'));
273
    }
274
275
    /**
276
     * Quote a message.
277
     *
278
     * @param int $articleId Id of the article where is the message to quote.
279
     * @param int $commentId Id of the message to quote.
280
     *
281
     * @throws \Cake\Network\Exception\NotFoundException
282
     *
283
     * @return mixed
284
     */
285
    public function quote($articleId = null, $commentId = null)
286
    {
287
        if (!$this->request->is('ajax')) {
288
            throw new NotFoundException();
289
        }
290
291
        $this->loadModel('BlogArticlesComments');
292
293
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
294
            ->find()
295
            ->where([
296
                'BlogArticlesComments.article_id' => $articleId,
297
                'BlogArticlesComments.id' => $commentId
298
            ])
299
            ->contain([
300
                'Users' => function ($q) {
301
                        return $q->find('short');
302
                }
303
            ])
304
            ->first();
305
306
        $json = [];
307
308 View Code Duplication
        if (!is_null($comment)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
309
            $comment->toArray();
310
311
            $url = Router::url(['action' => 'go', $comment->id]);
312
            $text = __("has said :");
313
314
            //Build the quote.
315
            $json['comment'] = <<<EOT
316
<div>
317
     <div>
318
        <a href="{$url}">
319
            <strong>{$comment->user->full_name} {$text}</strong>
320
        </a>
321
    </div>
322
    <blockquote>
323
        $comment->content
324
    </blockquote>
325
</div><p>&nbsp;</p><p>&nbsp;</p>
326
EOT;
327
328
            $json['error'] = false;
329
330
            $this->set(compact('json'));
331
        } else {
332
            $json['comment'] = __("This comment doesn't exist.");
333
            $json['error'] = true;
334
335
            $this->set(compact('json'));
336
        }
337
338
        //Send response in JSON.
339
        $this->set('_serialize', 'json');
340
    }
341
342
    /**
343
     * Redirect an user to an article, page and comment.
344
     *
345
     * @param int $commentId Id of the comment.
346
     *
347
     * @return \Cake\Network\Response
348
     */
349 View Code Duplication
    public function go($commentId = null)
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...
350
    {
351
        $this->loadModel('BlogArticlesComments');
352
353
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
354
            ->find()
355
            ->contain([
356
                'BlogArticles'
357
            ])
358
            ->where([
359
                'BlogArticlesComments.id' => $commentId
360
            ])
361
            ->first();
362
363
        if (is_null($comment)) {
364
            $this->Flash->error(__("This comment doesn't exist or has been deleted."));
365
366
            return $this->redirect(['action' => 'index']);
367
        }
368
369
        $comment->toArray();
370
371
        //Count the number of message before this message.
372
        $messagesBefore = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
373
            ->find()
374
            ->where([
375
                'BlogArticlesComments.article_id' => $comment->article_id,
376
                'BlogArticlesComments.created <' => $comment->created
377
            ])
378
            ->count();
379
380
        //Get the number of messages per page.
381
        $messagesPerPage = Configure::read('Blog.comment_per_page');
382
383
        //Calculate the page.
384
        $page = floor($messagesBefore / $messagesPerPage) + 1;
385
386
        $page = ($page > 1) ? $page : 1;
387
388
        //Redirect the user.
389
        return $this->redirect([
390
            '_name' => 'blog-article',
391
            'slug' => $comment->blog_article->title,
392
            'id' => $comment->blog_article->id,
393
            '?' => ['page' => $page],
394
            '#' => 'comment-' . $commentId
395
        ]);
396
    }
397
398
    /**
399
     * Get all articles by a date formatted to "m-Y".
400
     *
401
     * @param string $date The date of the archive.
402
     *
403
     * @return void
404
     */
405 View Code Duplication
    public function archive($date = null)
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...
406
    {
407
        $this->loadModel('BlogArticles');
408
409
        $this->paginate = [
410
            'maxLimit' => Configure::read('Blog.article_per_page')
411
        ];
412
413
        $archives = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
414
            ->find()
415
            ->where([
416
                'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date,
417
                'BlogArticles.is_display' => 1
418
            ])
419
            ->contain([
420
                'BlogCategories',
421
                'Users' => function ($q) {
422
                        return $q->find('short');
423
                }
424
            ])
425
            ->order([
426
                'BlogArticles.created' => 'desc'
427
            ]);
428
429
        $articles = $this->paginate($archives);
430
431
        $this->set(compact('articles', 'date'));
432
    }
433
434
    /**
435
     * Search articles.
436
     *
437
     * @return void
438
     */
439
    public function search()
440
    {
441
        $this->loadModel('BlogArticles');
442
443
        //Check the keyword to search. (For pagination)
444 View Code Duplication
        if (!empty($this->request->data['search'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
445
            $keyword = $this->request->data['search'];
446
            $this->request->session()->write('Search.Blog.Keyword', $keyword);
447
        } else {
448
            if ($this->request->session()->read('Search.Blog.Keyword')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->request->session(...('Search.Blog.Keyword') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
449
                $keyword = $this->request->session()->read('Search.Blog.Keyword');
450
            } else {
451
                $keyword = '';
452
            }
453
        }
454
455
        //Pagination
456
        $this->paginate = [
457
            'maxLimit' => Configure::read('Blog.article_per_page')
458
        ];
459
460
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
461
            ->find()
462
            ->contain([
463
                'Users' => function ($q) {
464
                    return $q->find('short');
465
                }
466
            ])
467
            ->where([
468
                'BlogArticles.is_display' => 1
469
            ])
470
            ->andWhere(function ($q) use ($keyword) {
471
                return $q
472
                    ->like('title', "%$keyword%");
473
            })
474
            ->order([
475
                'BlogArticles.created' => 'desc'
476
            ]);
477
478
        $articles = $this->paginate($articles);
479
480
        $this->set(compact('articles', 'keyword'));
481
    }
482
483
    /**
484
     * Like an article.
485
     *
486
     * @param int $articleId Id of the article to like.
487
     *
488
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
489
     *
490
     * @return void
491
     */
492
    public function articleLike($articleId = null)
493
    {
494
        if (!$this->request->is('ajax')) {
495
            throw new NotFoundException();
496
        }
497
498
        //Check if the user hasn't already liked this article.
499
        $this->loadModel('BlogArticlesLikes');
500
        $checkLike = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
501
            ->find()
502
            ->where([
503
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
504
                'BlogArticlesLikes.article_id' => $articleId
505
            ])
506
            ->first();
507
508
        $json = [];
509
510 View Code Duplication
        if (!is_null($checkLike)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
511
            $json['message'] = __('You already like this article !');
512
            $json['error'] = true;
513
514
            $this->set(compact('json'));
515
516
            $this->set('_serialize', 'json');
517
        }
518
519
        //Check if the article exist.
520
        $this->loadModel('BlogArticles');
521
        $checkArticle = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
522
            ->find()
523
            ->where([
524
                'BlogArticles.id' => $articleId,
525
                'BlogArticles.is_display' => 1
526
            ])
527
            ->first();
528
529 View Code Duplication
        if (is_null($checkArticle)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
530
            $json['message'] = __("This article doesn't exist !");
531
            $json['error'] = true;
532
533
            $this->set(compact('json'));
534
535
            $this->set('_serialize', 'json');
536
        }
537
538
        //Prepare data to be saved.
539
        $data = [];
540
        $data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id');
541
        $data['BlogArticlesLikes']['article_id'] = $articleId;
542
543
        $like = $this->BlogArticlesLikes->newEntity($data);
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
544
545
        if ($this->BlogArticlesLikes->save($like)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
546
            //Update the Statistics
547
            $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
548
            $event = new Event('Model.BlogArticlesLikes.new');
549
            $this->eventManager()->dispatch($event);
550
551
            $json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>");
552
            $json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>");
553
            $json['url'] = Router::url(
554
                [
555
                    'action' => 'articleUnlike',
556
                    $articleId
557
                ]
558
            );
559
            $json['error'] = false;
560
        } else {
561
            $json['message'] = __('An error occurred, please try again later.');
562
            $json['error'] = true;
563
        }
564
565
        $this->set(compact('json'));
566
567
        $this->set('_serialize', 'json');
568
    }
569
570
    /**
571
     * Unlike an article.
572
     *
573
     * @param int|null $articleId Id of the article to like.
574
     *
575
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
576
     *
577
     * @return void
578
     */
579
    public function articleUnlike($articleId = null)
580
    {
581
        if (!$this->request->is('ajax')) {
582
            throw new NotFoundException();
583
        }
584
585
        //Check if the user like this article.
586
        $this->loadModel('BlogArticlesLikes');
587
        $like = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
588
            ->find()
589
            ->contain([
590
                'BlogArticles'
591
            ])
592
            ->where([
593
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
594
                'BlogArticlesLikes.article_id' => $articleId,
595
                'BlogArticles.is_display' => 1
596
            ])
597
            ->first();
598
599
        $json = [];
600
601 View Code Duplication
        if (is_null($like)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
602
            $json['message'] = __("You don't like this article !");
603
            $json['error'] = true;
604
605
            $this->set(compact('json'));
606
607
            $this->set('_serialize', 'json');
608
        }
609
610
        if ($this->BlogArticlesLikes->delete($like)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
611
            //Update the Statistics
612
            $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
613
            $event = new Event('Model.BlogArticlesLikes.new');
614
            $this->eventManager()->dispatch($event);
615
616
            $json['url'] = Router::url([
617
                                'action' => 'articleLike',
618
                                $articleId
619
                            ]);
620
            $json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>");
621
            $json['error'] = false;
622
        } else {
623
            $json['message'] = __('An error occurred, please try again later.');
624
            $json['error'] = true;
625
        }
626
627
        $this->set(compact('json'));
628
629
        $this->set('_serialize', 'json');
630
    }
631
632
    /**
633
     * Delete a comment.
634
     *
635
     * @param int $id Id of the comment to delete.
636
     *
637
     * @return \Cake\Network\Response
638
     */
639
    public function deleteComment($id = null)
640
    {
641
        $this->loadModel('BlogArticlesComments');
642
643
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
644
            ->find()
645
            ->contain([
646
                'BlogArticles'
647
            ])
648
            ->where([
649
                'BlogArticlesComments.id' => $id
650
            ])
651
            ->first();
652
653
        if (is_null($comment)) {
654
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
655
656
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
657
        }
658
659
        //Current user.
660
        $this->loadModel('Users');
661
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
662
            ->find()
663
            ->contain([
664
                'Groups' => function ($q) {
665
                    return $q->select(['id', 'is_staff']);
666
                }
667
            ])
668
            ->where([
669
                'Users.id' => $this->Auth->user('id')
670
            ])
671
            ->select(['id', 'group_id'])
672
            ->first();
673
674 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
675
            $this->Flash->error(__("You don't have the authorization to delete this comment !"));
676
677
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
678
        }
679
680
        if ($this->BlogArticlesComments->delete($comment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
681
            $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
682
            $event = new Event('Model.BlogArticlesComments.new');
683
            $this->eventManager()->dispatch($event);
684
685
            $this->Flash->success(__("This comment has been deleted successfully !"));
686
        }
687
688
        return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]);
689
    }
690
691
    /**
692
     * Get the form to edit a comment.
693
     *
694
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
695
     *
696
     * @return void
697
     */
698
    public function getEditComment()
699
    {
700
        if (!$this->request->is('ajax')) {
701
            throw new NotFoundException();
702
        }
703
704
        $this->loadModel('BlogArticlesComments');
705
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
706
            ->find()
707
            ->where([
708
                'BlogArticlesComments.id' => $this->request->data['id']
709
            ])
710
            ->first();
711
712
        $json = [
713
            'error' => false,
714
            'errorMessage' => ''
715
        ];
716
717 View Code Duplication
        if (is_null($comment)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
718
            $json['error'] = true;
719
            $json['errorMessage'] = __("This comment doesn't exist or has been deleted !");
720
721
            $this->set(compact('json'));
722
723
            return;
724
        }
725
726
        //Current user.
727
        $this->loadModel('Users');
728
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
729
            ->find()
730
            ->contain([
731
                'Groups' => function ($q) {
732
                    return $q->select(['id', 'is_staff']);
733
                }
734
            ])
735
            ->where([
736
                'Users.id' => $this->Auth->user('id')
737
            ])
738
            ->select(['id', 'group_id'])
739
            ->first();
740
741 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
742
            $json['error'] = true;
743
            $json['errorMessage'] = __("You don't have the authorization to edit this comment !");
744
745
            $this->set(compact('json'));
746
747
            return;
748
        }
749
750
        $this->set(compact('json', 'comment'));
751
    }
752
753
    /**
754
     * Edit a comment.
755
     *
756
     * @param int $id Id of the comment.
757
     *
758
     * @return \Cake\Network\Response
759
     */
760
    public function editComment($id = null)
761
    {
762
        $this->loadModel('BlogArticlesComments');
763
764
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
765
            ->find()
766
            ->contain([
767
                'BlogArticles'
768
            ])
769
            ->where([
770
                'BlogArticlesComments.id' => $id
771
            ])
772
            ->first();
773
774
        if (is_null($comment)) {
775
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
776
777
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
778
        }
779
780
        //Current user.
781
        $this->loadModel('Users');
782
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
783
            ->find()
784
            ->contain([
785
                'Groups' => function ($q) {
786
                    return $q->select(['id', 'is_staff']);
787
                }
788
            ])
789
            ->where([
790
                'Users.id' => $this->Auth->user('id')
791
            ])
792
            ->select(['id', 'group_id'])
793
            ->first();
794
795 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
796
            $this->Flash->error(__("You don't have the authorization to edit this comment !"));
797
798
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
799
        }
800
801
        $this->BlogArticlesComments->patchEntity($comment, $this->request->data());
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
802
        if ($this->BlogArticlesComments->save($comment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
803
            $this->Flash->success(__("This comment has been edited successfully !"));
804
        }
805
806
        return $this->redirect(['action' => 'go', $comment->id]);
807
    }
808
}
809