Completed
Push — master ( 6f92e6...cbba0d )
by Fèvre
9s
created

BlogController::article()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 155
Code Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 155
rs 5.2676
c 0
b 0
f 0
cc 8
eloc 94
nc 8
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\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
        if (empty($category)) {
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
        //Check if the article is found.
155
        if (is_null($article)) {
156
            $this->Flash->error(__('This article doesn\'t exist or has been deleted.'));
157
158
            return $this->redirect(['action' => 'index']);
159
        }
160
161
        $this->loadModel('BlogArticlesComments');
162
163
        //A comment has been posted.
164
        if ($this->request->is('post')) {
165
            //Check if the user is connected.
166
            if (!$this->Auth->user()) {
167
                $this->Flash->error(__('You must be connected to post a comment.'));
168
169
                return $this->redirect([
170
                    '_name' => 'blog-article',
171
                    'slug' => h($article->title),
172
                    'id' => $article->id
173
                ]);
174
            }
175
176
            $this->request->data['article_id'] = $article->id;
177
            $this->request->data['user_id'] = $this->Auth->user('id');
178
179
            $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...
180
181
            //Attach Event.
182
            $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...
183
184
            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...
185
                $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...
186
                $event = new Event('Model.BlogArticlesComments.new');
187
                $this->eventManager()->dispatch($event);
188
189
                $this->Flash->success(__('Your comment has been posted successfully !'));
190
                //Redirect the user to the last page of the article.
191
                $this->redirect([
192
                    'action' => 'go',
193
                    $insertComment->id
194
                ]);
195
            }
196
        }
197
198
        $this->loadModel('PollsUsers');
199
        $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...
200
            ->find()
201
            ->contain([
202
                'Polls' => function ($q) {
203
                    return $q->select(['id']);
204
                },
205
                'PollsAnswers'
206
            ])
207
            ->where([
208
                'PollsUsers.user_id' => $this->Auth->user('id'),
209
                'Polls.id' => $article->poll ? $article->poll->id : null
210
            ])
211
            ->first();
212
213
        //Paginate all comments related to the article.
214
        $this->paginate = [
215
            'maxLimit' => Configure::read('Blog.comment_per_page')
216
        ];
217
218
        $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...
219
            ->find()
220
            ->where([
221
                'BlogArticlesComments.article_id' => $article->id
222
            ])
223
            ->contain([
224
                'Users' => function ($q) {
225
                    return $q->find('medium');
226
                }
227
            ])
228
            ->order([
229
                'BlogArticlesComments.created' => 'asc'
230
            ]);
231
232
        $comments = $this->paginate($comments);
233
234
        //Select the like for the current auth user.
235
        $this->loadModel('BlogArticlesLikes');
236
        $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...
237
            ->find()
238
            ->where([
239
                'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null,
240
                'article_id' => $article->id
241
            ])
242
            ->first();
243
244
        //Build the newEntity for the comment form.
245
        $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...
246
247
        //Search related articles
248
        $keywords = preg_split("/([\s,\W])+/", $article->title);
249
250
        $query = $this->BlogArticles->find();
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...
251
        $query
252
            ->contain([
253
                'BlogCategories',
254
            ]);
255
256
        foreach ($keywords as $keyword) {
257
            $query->orWhere(function ($exp, $q) use ($keyword) {
0 ignored issues
show
Unused Code introduced by
The parameter $q is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
258
                return $exp->like('BlogArticles.title', '%' . $keyword . '%');
259
            });
260
        }
261
262
        $articles = $query->andWhere([
263
            'BlogArticles.is_display' => 1,
264
            'BlogArticles.id !=' => $article->id
265
        ]);
266
267
        //Current user.
268
        $this->loadModel('Users');
269
        $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...
270
            ->find()
271
            ->contain([
272
                'Groups' => function ($q) {
273
                    return $q->select(['id', 'is_staff']);
274
                }
275
            ])
276
            ->where([
277
                'Users.id' => $this->Auth->user('id')
278
            ])
279
            ->select(['id', 'group_id'])
280
            ->first();
281
282
        $this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser', 'hasVoted'));
283
    }
284
285
    /**
286
     * Quote a message.
287
     *
288
     * @param int $articleId Id of the article where is the message to quote.
289
     * @param int $commentId Id of the message to quote.
290
     *
291
     * @throws \Cake\Network\Exception\NotFoundException
292
     *
293
     * @return mixed
294
     */
295
    public function quote($articleId = null, $commentId = null)
296
    {
297
        if (!$this->request->is('ajax')) {
298
            throw new NotFoundException();
299
        }
300
301
        $this->loadModel('BlogArticlesComments');
302
303
        $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...
304
            ->find()
305
            ->where([
306
                'BlogArticlesComments.article_id' => $articleId,
307
                'BlogArticlesComments.id' => $commentId
308
            ])
309
            ->contain([
310
                'Users' => function ($q) {
311
                        return $q->find('short');
312
                }
313
            ])
314
            ->first();
315
316
        $json = [];
317
318
        if (!is_null($comment)) {
319
            $comment->toArray();
320
321
            $url = Router::url(['action' => 'go', $comment->id]);
322
            $text = __("has said :");
323
324
            //Build the quote.
325
            $json['comment'] = <<<EOT
326
<div>
327
     <div>
328
        <a href="{$url}">
329
            <strong>{$comment->user->full_name} {$text}</strong>
330
        </a>
331
    </div>
332
    <blockquote>
333
        $comment->content
334
    </blockquote>
335
</div><p>&nbsp;</p><p>&nbsp;</p>
336
EOT;
337
338
            $json['error'] = false;
339
340
            $this->set(compact('json'));
341
        } else {
342
            $json['comment'] = __("This comment doesn't exist.");
343
            $json['error'] = true;
344
345
            $this->set(compact('json'));
346
        }
347
348
        //Send response in JSON.
349
        $this->set('_serialize', 'json');
350
    }
351
352
    /**
353
     * Redirect an user to an article, page and comment.
354
     *
355
     * @param int $commentId Id of the comment.
356
     *
357
     * @return \Cake\Network\Response
358
     */
359 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...
360
    {
361
        $this->loadModel('BlogArticlesComments');
362
363
        $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...
364
            ->find()
365
            ->contain([
366
                'BlogArticles'
367
            ])
368
            ->where([
369
                'BlogArticlesComments.id' => $commentId
370
            ])
371
            ->first();
372
373
        if (is_null($comment)) {
374
            $this->Flash->error(__("This comment doesn't exist or has been deleted."));
375
376
            return $this->redirect(['action' => 'index']);
377
        }
378
379
        $comment->toArray();
380
381
        //Count the number of message before this message.
382
        $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...
383
            ->find()
384
            ->where([
385
                'BlogArticlesComments.article_id' => $comment->article_id,
386
                'BlogArticlesComments.created <' => $comment->created
387
            ])
388
            ->count();
389
390
        //Get the number of messages per page.
391
        $messagesPerPage = Configure::read('Blog.comment_per_page');
392
393
        //Calculate the page.
394
        $page = floor($messagesBefore / $messagesPerPage) + 1;
395
396
        $page = ($page > 1) ? $page : 1;
397
398
        //Redirect the user.
399
        return $this->redirect([
400
            '_name' => 'blog-article',
401
            'slug' => $comment->blog_article->title,
402
            'id' => $comment->blog_article->id,
403
            '?' => ['page' => $page],
404
            '#' => 'comment-' . $commentId
405
        ]);
406
    }
407
408
    /**
409
     * Get all articles by a date formatted to "m-Y".
410
     *
411
     * @param string $date The date of the archive.
412
     *
413
     * @return void
414
     */
415
    public function archive($date = null)
416
    {
417
        $this->loadModel('BlogArticles');
418
419
        $this->paginate = [
420
            'maxLimit' => Configure::read('Blog.article_per_page')
421
        ];
422
423
        $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...
424
            ->find()
425
            ->where([
426
                'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date,
427
                'BlogArticles.is_display' => 1
428
            ])
429
            ->contain([
430
                'BlogCategories',
431
                'Users' => function ($q) {
432
                        return $q->find('short');
433
                }
434
            ])
435
            ->order([
436
                'BlogArticles.created' => 'desc'
437
            ]);
438
439
        $articles = $this->paginate($archives);
440
441
        $this->set(compact('articles', 'date'));
442
    }
443
444
    /**
445
     * Search articles.
446
     *
447
     * @return void
448
     */
449
    public function search()
450
    {
451
        $this->loadModel('BlogArticles');
452
453
        //Check the keyword to search. (For pagination)
454 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...
455
            $keyword = $this->request->data['search'];
456
            $this->request->session()->write('Search.Blog.Keyword', $keyword);
457
        } else {
458
            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...
459
                $keyword = $this->request->session()->read('Search.Blog.Keyword');
460
            } else {
461
                $keyword = '';
462
            }
463
        }
464
465
        //Pagination
466
        $this->paginate = [
467
            'maxLimit' => Configure::read('Blog.article_per_page')
468
        ];
469
470
        $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...
471
            ->find()
472
            ->contain([
473
                'Users' => function ($q) {
474
                    return $q->find('short');
475
                }
476
            ])
477
            ->where([
478
                'BlogArticles.is_display' => 1
479
            ])
480
            ->andWhere(function ($q) use ($keyword) {
481
                return $q
482
                    ->like('title', "%$keyword%");
483
            })
484
            ->order([
485
                'BlogArticles.created' => 'desc'
486
            ]);
487
488
        $articles = $this->paginate($articles);
489
490
        $this->set(compact('articles', 'keyword'));
491
    }
492
493
    /**
494
     * Like an article.
495
     *
496
     * @param int $articleId Id of the article to like.
497
     *
498
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
499
     *
500
     * @return void
501
     */
502
    public function articleLike($articleId = null)
503
    {
504
        if (!$this->request->is('ajax')) {
505
            throw new NotFoundException();
506
        }
507
508
        //Check if the user hasn't already liked this article.
509
        $this->loadModel('BlogArticlesLikes');
510
        $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...
511
            ->find()
512
            ->where([
513
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
514
                'BlogArticlesLikes.article_id' => $articleId
515
            ])
516
            ->first();
517
518
        $json = [];
519
520 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...
521
            $json['message'] = __('You already like this article !');
522
            $json['error'] = true;
523
524
            $this->set(compact('json'));
525
526
            $this->set('_serialize', 'json');
527
528
            return;
529
        }
530
531
        //Check if the article exist.
532
        $this->loadModel('BlogArticles');
533
        $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...
534
            ->find()
535
            ->where([
536
                'BlogArticles.id' => $articleId,
537
                'BlogArticles.is_display' => 1
538
            ])
539
            ->first();
540
541 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...
542
            $json['message'] = __("This article doesn't exist !");
543
            $json['error'] = true;
544
545
            $this->set(compact('json'));
546
            $this->set('_serialize', 'json');
547
548
            return;
549
        }
550
551
        //Prepare data to be saved.
552
        $data = [];
553
        $data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id');
554
        $data['BlogArticlesLikes']['article_id'] = $articleId;
555
556
        $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...
557
558
        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...
559
            //Update the Statistics
560
            $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...
561
            $event = new Event('Model.BlogArticlesLikes.new');
562
            $this->eventManager()->dispatch($event);
563
564
            $json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>");
565
            $json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>");
566
            $json['url'] = Router::url(
567
                [
568
                    'action' => 'articleUnlike',
569
                    $articleId
570
                ]
571
            );
572
            $json['error'] = false;
573
        } else {
574
            $json['message'] = __('An error occurred, please try again later.');
575
            $json['error'] = true;
576
        }
577
578
        $this->set(compact('json'));
579
580
        $this->set('_serialize', 'json');
581
    }
582
583
    /**
584
     * Unlike an article.
585
     *
586
     * @param int|null $articleId Id of the article to like.
587
     *
588
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
589
     *
590
     * @return void
591
     */
592
    public function articleUnlike($articleId = null)
593
    {
594
        if (!$this->request->is('ajax')) {
595
            throw new NotFoundException();
596
        }
597
598
        //Check if the user like this article.
599
        $this->loadModel('BlogArticlesLikes');
600
        $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...
601
            ->find()
602
            ->contain([
603
                'BlogArticles'
604
            ])
605
            ->where([
606
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
607
                'BlogArticlesLikes.article_id' => $articleId,
608
                'BlogArticles.is_display' => 1
609
            ])
610
            ->first();
611
612
        $json = [];
613
614 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...
615
            $json['message'] = __("You don't like this article !");
616
            $json['error'] = true;
617
618
            $this->set(compact('json'));
619
620
            $this->set('_serialize', 'json');
621
622
            return;
623
        }
624
625
        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...
626
            //Update the Statistics
627
            $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...
628
            $event = new Event('Model.BlogArticlesLikes.new');
629
            $this->eventManager()->dispatch($event);
630
631
            $json['url'] = Router::url([
632
                                'action' => 'articleLike',
633
                                $articleId
634
                            ]);
635
            $json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>");
636
            $json['error'] = false;
637
        } else {
638
            $json['message'] = __('An error occurred, please try again later.');
639
            $json['error'] = true;
640
        }
641
642
        $this->set(compact('json'));
643
644
        $this->set('_serialize', 'json');
645
    }
646
647
    /**
648
     * Delete a comment.
649
     *
650
     * @param int $id Id of the comment to delete.
651
     *
652
     * @return \Cake\Network\Response
653
     */
654
    public function deleteComment($id = null)
655
    {
656
        $this->loadModel('BlogArticlesComments');
657
658
        $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...
659
            ->find()
660
            ->contain([
661
                'BlogArticles'
662
            ])
663
            ->where([
664
                'BlogArticlesComments.id' => $id
665
            ])
666
            ->first();
667
668
        if (is_null($comment)) {
669
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
670
671
            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...
672
        }
673
674
        //Current user.
675
        $this->loadModel('Users');
676
        $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...
677
            ->find()
678
            ->contain([
679
                'Groups' => function ($q) {
680
                    return $q->select(['id', 'is_staff']);
681
                }
682
            ])
683
            ->where([
684
                'Users.id' => $this->Auth->user('id')
685
            ])
686
            ->select(['id', 'group_id'])
687
            ->first();
688
689
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
690
            $this->Flash->error(__("You don't have the authorization to delete this comment !"));
691
692
            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...
693
        }
694
695
        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...
696
            $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...
697
            $event = new Event('Model.BlogArticlesComments.new');
698
            $this->eventManager()->dispatch($event);
699
700
            $this->Flash->success(__("This comment has been deleted successfully !"));
701
        }
702
703
        return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]);
704
    }
705
706
    /**
707
     * Get the form to edit a comment.
708
     *
709
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
710
     *
711
     * @return void
712
     */
713
    public function getEditComment()
714
    {
715
        if (!$this->request->is('ajax')) {
716
            throw new NotFoundException();
717
        }
718
719
        $this->loadModel('BlogArticlesComments');
720
        $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...
721
            ->find()
722
            ->where([
723
                'BlogArticlesComments.id' => $this->request->data['id']
724
            ])
725
            ->first();
726
727
        $json = [
728
            'error' => false,
729
            'errorMessage' => ''
730
        ];
731
732 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...
733
            $json['error'] = true;
734
            $json['errorMessage'] = __("This comment doesn't exist or has been deleted !");
735
736
            $this->set(compact('json'));
737
738
            return;
739
        }
740
741
        //Current user.
742
        $this->loadModel('Users');
743
        $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...
744
            ->find()
745
            ->contain([
746
                'Groups' => function ($q) {
747
                    return $q->select(['id', 'is_staff']);
748
                }
749
            ])
750
            ->where([
751
                'Users.id' => $this->Auth->user('id')
752
            ])
753
            ->select(['id', 'group_id'])
754
            ->first();
755
756
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
757
            $json['error'] = true;
758
            $json['errorMessage'] = __("You don't have the authorization to edit this comment !");
759
760
            $this->set(compact('json'));
761
762
            return;
763
        }
764
765
        $this->set(compact('json', 'comment'));
766
    }
767
768
    /**
769
     * Edit a comment.
770
     *
771
     * @param int $id Id of the comment.
772
     *
773
     * @throws \Cake\Network\Exception\NotFoundException When it's not a POST request.
774
     *
775
     * @return \Cake\Network\Response
776
     */
777
    public function editComment($id = null)
778
    {
779
        if (!$this->request->is('post')) {
780
            throw new NotFoundException();
781
        }
782
783
        $this->loadModel('BlogArticlesComments');
784
785
        $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...
786
            ->find()
787
            ->contain([
788
                'BlogArticles'
789
            ])
790
            ->where([
791
                'BlogArticlesComments.id' => $id
792
            ])
793
            ->first();
794
795
        if (is_null($comment)) {
796
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
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
        //Current user.
802
        $this->loadModel('Users');
803
        $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...
804
            ->find()
805
            ->contain([
806
                'Groups' => function ($q) {
807
                    return $q->select(['id', 'is_staff']);
808
                }
809
            ])
810
            ->where([
811
                'Users.id' => $this->Auth->user('id')
812
            ])
813
            ->select(['id', 'group_id'])
814
            ->first();
815
816
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
817
            $this->Flash->error(__("You don't have the authorization to edit this comment !"));
818
819
            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...
820
        }
821
822
        $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...
823
        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...
824
            $this->Flash->success(__("This comment has been edited successfully !"));
825
        }
826
827
        return $this->redirect(['action' => 'go', $comment->id]);
828
    }
829
}
830