Completed
Push — master ( 2b4546...faabd5 )
by Fèvre
10:23 queued 08:18
created

BlogController   F

Complexity

Total Complexity 45

Size/Duplication

Total Lines 759
Duplicated Lines 11.07 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 9
dl 84
loc 759
rs 3.2
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
B archive() 0 28 1
B search() 10 43 3
B articleLike() 0 72 5
B articleUnlike() 0 47 4
B deleteComment() 0 47 5
B category() 0 46 2
A initialize() 0 6 1
A beforeFilter() 0 6 1
B index() 26 26 1
B article() 0 120 6
A go() 48 48 3
A quote() 0 56 3
B getEditComment() 0 54 5
B editComment() 0 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 Cake\Core\Configure;
6
use Cake\Event\Event;
7
use Cake\Network\Exception\NotFoundException;
8
use Cake\Routing\Router;
9
10
class BlogController extends AppController
11
{
12
13
    /**
14
     * Initialization hook method.
15
     *
16
     * @return void
17
     */
18
    public function initialize()
19
    {
20
        parent::initialize();
21
22
        $this->loadComponent('RequestHandler');
23
    }
24
25
    /**
26
     * BeforeFilter handle.
27
     *
28
     * @param Event $event The beforeFilter event that was fired.
29
     *
30
     * @return void
31
     */
32
    public function beforeFilter(Event $event)
33
    {
34
        parent::beforeFilter($event);
35
36
        $this->Auth->allow(['index', 'category', 'article', 'go', 'archive', 'search']);
37
    }
38
39
    /**
40
     * Display all Articles.
41
     *
42
     * @return void
43
     */
44 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...
45
    {
46
        $this->loadModel('BlogArticles');
47
        $this->paginate = [
48
            'maxLimit' => Configure::read('Blog.article_per_page')
49
        ];
50
51
        $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...
52
            ->find()
53
            ->contain([
54
                'BlogCategories',
55
                'Users' => function ($q) {
56
                    return $q->find('short');
57
                }
58
            ])
59
            ->order([
60
                'BlogArticles.created' => 'desc'
61
            ])
62
            ->where([
63
                'BlogArticles.is_display' => 1
64
            ]);
65
66
        $articles = $this->paginate($articles);
67
68
        $this->set(compact('articles'));
69
    }
70
71
    /**
72
     * Display a specific category with all its articles.
73
     *
74
     * @return \Cake\Network\Response|void
75
     */
76
    public function category()
77
    {
78
        $this->loadModel('BlogCategories');
79
80
        $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...
81
            ->find()
82
            ->where([
83
                'BlogCategories.id' => $this->request->id
84
            ])
85
            ->contain([
86
                'BlogArticles'
87
            ])
88
            ->first();
89
90
        //Check if the category is found.
91
        if (empty($category)) {
92
            $this->Flash->error(__('This category doesn\'t exist or has been deleted.'));
93
94
            return $this->redirect(['action' => 'index']);
95
        }
96
97
        //Paginate all Articles.
98
        $this->loadModel('BlogArticles');
99
        $this->paginate = [
100
            'maxLimit' => Configure::read('Blog.article_per_page')
101
        ];
102
103
        $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...
104
            ->find()
105
            ->contain([
106
                'Users' => function ($q) {
107
                    return $q->find('short');
108
                }
109
            ])
110
            ->where([
111
                'BlogArticles.category_id' => $category->id,
112
                'BlogArticles.is_display' => 1
113
            ])
114
            ->order([
115
                'BlogArticles.created' => 'desc'
116
            ]);
117
118
        $articles = $this->paginate($articles);
119
120
        $this->set(compact('category', 'articles'));
121
    }
122
123
    /**
124
     * Display a specific article.
125
     *
126
     * @return \Cake\Network\Response|void
127
     */
128
    public function article()
129
    {
130
        $this->loadModel('BlogArticles');
131
132
        $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...
133
            ->find()
134
            ->where([
135
                'BlogArticles.id' => $this->request->id,
136
                'BlogArticles.is_display' => 1
137
            ])
138
            ->contain([
139
                'BlogCategories',
140
                'BlogAttachments',
141
                'Users' => function ($q) {
142
                        return $q->find('full');
143
                }
144
            ])
145
            ->first();
146
147
        //Check if the article is found.
148
        if (is_null($article)) {
149
            $this->Flash->error(__('This article doesn\'t exist or has been deleted.'));
150
151
            return $this->redirect(['action' => 'index']);
152
        }
153
154
        $this->loadModel('BlogArticlesComments');
155
156
        //A comment has been posted.
157
        if ($this->request->is('post')) {
158
            //Check if the user is connected.
159
            if (!$this->Auth->user()) {
160
                return $this->Flash->error(__('You must be connected to post a comment.'));
161
            }
162
163
            $this->request->data['article_id'] = $article->id;
164
            $this->request->data['user_id'] = $this->Auth->user('id');
165
166
            $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...
167
168
            //Attach Event.
169
            $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...
170
171
            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...
172
                $this->Flash->success(__('Your comment has been posted successfully !'));
173
                //Redirect the user to the last page of the article.
174
                $this->redirect([
175
                    'action' => 'go',
176
                    $insertComment->id
177
                ]);
178
            }
179
        }
180
181
        //Paginate all comments related to the article.
182
        $this->paginate = [
183
            'maxLimit' => Configure::read('Blog.comment_per_page')
184
        ];
185
186
        $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...
187
            ->find()
188
            ->where([
189
                'BlogArticlesComments.article_id' => $article->id
190
            ])
191
            ->contain([
192
                'Users' => function ($q) {
193
                    return $q->find('medium');
194
                }
195
            ])
196
            ->order([
197
                'BlogArticlesComments.created' => 'asc'
198
            ]);
199
200
        $comments = $this->paginate($comments);
201
202
        //Select the like for the current auth user.
203
        $this->loadModel('BlogArticlesLikes');
204
        $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...
205
            ->find()
206
            ->where([
207
                'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null,
208
                'article_id' => $article->id
209
            ])
210
            ->first();
211
212
        //Build the newEntity for the comment form.
213
        $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...
214
215
        //Search related articles
216
        $keywords = preg_split("/([\s,\W])+/", $article->title);
217
218
        $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...
219
            ->find()
220
            ->contain([
221
                'BlogCategories',
222
            ])
223
            ->where([
224
                'BlogArticles.is_display' => 1,
225
                'BlogArticles.id !=' => $article->id
226
            ])
227
            ->andWhere([
228
                'BlogArticles.title RLIKE' => rtrim(implode('|', $keywords), '|')
229
            ]);
230
231
        //Current user.
232
        $this->loadModel('Users');
233
        $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...
234
            ->find()
235
            ->contain([
236
                'Groups' => function ($q) {
237
                    return $q->select(['id', 'is_staff']);
238
                }
239
            ])
240
            ->where([
241
                'Users.id' => $this->Auth->user('id')
242
            ])
243
            ->select(['id', 'group_id'])
244
            ->first();
245
246
        $this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser'));
247
    }
248
249
    /**
250
     * Quote a message.
251
     *
252
     * @param int $articleId Id of the article where is the message to quote.
253
     * @param int $commentId Id of the message to quote.
254
     *
255
     * @throws \Cake\Network\Exception\NotFoundException
256
     *
257
     * @return mixed
258
     */
259
    public function quote($articleId = null, $commentId = null)
260
    {
261
        if (!$this->request->is('ajax')) {
262
            throw new NotFoundException();
263
        }
264
265
        $this->loadModel('BlogArticlesComments');
266
267
        $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...
268
            ->find()
269
            ->where([
270
                'BlogArticlesComments.article_id' => $articleId,
271
                'BlogArticlesComments.id' => $commentId
272
            ])
273
            ->contain([
274
                'Users' => function ($q) {
275
                        return $q->find('short');
276
                }
277
            ])
278
            ->first();
279
280
        $json = [];
281
282
        if (!is_null($comment)) {
283
            $comment->toArray();
284
285
            $url = Router::url(['action' => 'go', $comment->id]);
286
            $text = __("has said :");
287
288
            //Build the quote.
289
            $json['comment'] = <<<EOT
290
<div>
291
     <div>
292
        <a href="{$url}">
293
            <strong>{$comment->user->full_name} {$text}</strong>
294
        </a>
295
    </div>
296
    <blockquote>
297
        $comment->content
298
    </blockquote>
299
</div><p>&nbsp;</p><p>&nbsp;</p>
300
EOT;
301
302
            $json['error'] = false;
303
304
            $this->set(compact('json'));
305
        } else {
306
            $json['comment'] = __("This comment doesn't exist.");
307
            $json['error'] = true;
308
309
            $this->set(compact('json'));
310
        }
311
312
        //Send response in JSON.
313
        $this->set('_serialize', 'json');
314
    }
315
316
    /**
317
     * Redirect an user to an article, page and comment.
318
     *
319
     * @param int $commentId Id of the comment.
320
     *
321
     * @return \Cake\Network\Response
322
     */
323 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...
324
    {
325
        $this->loadModel('BlogArticlesComments');
326
327
        $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...
328
            ->find()
329
            ->contain([
330
                'BlogArticles'
331
            ])
332
            ->where([
333
                'BlogArticlesComments.id' => $commentId
334
            ])
335
            ->first();
336
337
        if (is_null($comment)) {
338
            $this->Flash->error(__("This comment doesn't exist or has been deleted."));
339
340
            return $this->redirect(['action' => 'index']);
341
        }
342
343
        $comment->toArray();
344
345
        //Count the number of message before this message.
346
        $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...
347
            ->find()
348
            ->where([
349
                'BlogArticlesComments.article_id' => $comment->article_id,
350
                'BlogArticlesComments.created <' => $comment->created
351
            ])
352
            ->count();
353
354
        //Get the number of messages per page.
355
        $messagesPerPage = Configure::read('Blog.comment_per_page');
356
357
        //Calculate the page.
358
        $page = floor($messagesBefore / $messagesPerPage) + 1;
359
360
        $page = ($page > 1) ? $page : 1;
361
362
        //Redirect the user.
363
        return $this->redirect([
364
            '_name' => 'blog-article',
365
            'slug' => $comment->blog_article->title,
366
            'id' => $comment->blog_article->id,
367
            '?' => ['page' => $page],
368
            '#' => 'comment-' . $commentId
369
        ]);
370
    }
371
372
    /**
373
     * Get all articles by a date formatted to "m-Y".
374
     *
375
     * @param string $date The date of the archive.
376
     *
377
     * @return void
378
     */
379
    public function archive($date = null)
380
    {
381
        $this->loadModel('BlogArticles');
382
383
        $this->paginate = [
384
            'maxLimit' => Configure::read('Blog.article_per_page')
385
        ];
386
387
        $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...
388
            ->find()
389
            ->where([
390
                'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date,
391
                'BlogArticles.is_display' => 1
392
            ])
393
            ->contain([
394
                'BlogCategories',
395
                'Users' => function ($q) {
396
                        return $q->find('short');
397
                }
398
            ])
399
            ->order([
400
                'BlogArticles.created' => 'desc'
401
            ]);
402
403
        $articles = $this->paginate($archives);
404
405
        $this->set(compact('articles', 'date'));
406
    }
407
408
    /**
409
     * Search articles.
410
     *
411
     * @return void
412
     */
413
    public function search()
414
    {
415
        $this->loadModel('BlogArticles');
416
417
        //Check the keyword to search. (For pagination)
418 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...
419
            $keyword = $this->request->data['search'];
420
            $this->request->session()->write('Search.Blog.Keyword', $keyword);
421
        } else {
422
            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...
423
                $keyword = $this->request->session()->read('Search.Blog.Keyword');
424
            } else {
425
                $keyword = '';
426
            }
427
        }
428
429
        //Pagination
430
        $this->paginate = [
431
            'maxLimit' => Configure::read('Blog.article_per_page')
432
        ];
433
434
        $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...
435
            ->find()
436
            ->contain([
437
                'Users' => function ($q) {
438
                    return $q->find('short');
439
                }
440
            ])
441
            ->where([
442
                'BlogArticles.is_display' => 1
443
            ])
444
            ->andWhere(function ($q) use ($keyword) {
445
                return $q
446
                    ->like('title', "%$keyword%");
447
            })
448
            ->order([
449
                'BlogArticles.created' => 'desc'
450
            ]);
451
452
        $articles = $this->paginate($articles);
453
454
        $this->set(compact('articles', 'keyword'));
455
    }
456
457
    /**
458
     * Like an article.
459
     *
460
     * @param int $articleId Id of the article to like.
461
     *
462
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
463
     *
464
     * @return void
465
     */
466
    public function articleLike($articleId = null)
467
    {
468
        if (!$this->request->is('ajax')) {
469
            throw new NotFoundException();
470
        }
471
472
        //Check if the user hasn't already liked this article.
473
        $this->loadModel('BlogArticlesLikes');
474
        $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...
475
            ->find()
476
            ->where([
477
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
478
                'BlogArticlesLikes.article_id' => $articleId
479
            ])
480
            ->first();
481
482
        $json = [];
483
484
        if (!is_null($checkLike)) {
485
            $json['message'] = __('You already like this article !');
486
            $json['error'] = true;
487
488
            $this->set(compact('json'));
489
490
            $this->set('_serialize', 'json');
491
        }
492
493
        //Check if the article exist.
494
        $this->loadModel('BlogArticles');
495
        $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...
496
            ->find()
497
            ->where([
498
                'BlogArticles.id' => $articleId,
499
                'BlogArticles.is_display' => 1
500
            ])
501
            ->first();
502
503
        if (is_null($checkArticle)) {
504
            $json['message'] = __("This article doesn't exist !");
505
            $json['error'] = true;
506
507
            $this->set(compact('json'));
508
509
            $this->set('_serialize', 'json');
510
        }
511
512
        //Prepare data to be saved.
513
        $data = [];
514
        $data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id');
515
        $data['BlogArticlesLikes']['article_id'] = $articleId;
516
517
        $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...
518
519
        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...
520
            $json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>");
521
            $json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>");
522
            $json['url'] = Router::url(
523
                [
524
                    'action' => 'articleUnlike',
525
                    $articleId
526
                ]
527
            );
528
            $json['error'] = false;
529
        } else {
530
            $json['message'] = __('An error occurred, please try again later.');
531
            $json['error'] = true;
532
        }
533
534
        $this->set(compact('json'));
535
536
        $this->set('_serialize', 'json');
537
    }
538
539
    /**
540
     * Unlike an article.
541
     *
542
     * @param int|null $articleId Id of the article to like.
543
     *
544
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
545
     *
546
     * @return void
547
     */
548
    public function articleUnlike($articleId = null)
549
    {
550
        if (!$this->request->is('ajax')) {
551
            throw new NotFoundException();
552
        }
553
554
        //Check if the user like this article.
555
        $this->loadModel('BlogArticlesLikes');
556
        $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...
557
            ->find()
558
            ->contain([
559
                'BlogArticles'
560
            ])
561
            ->where([
562
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
563
                'BlogArticlesLikes.article_id' => $articleId,
564
                'BlogArticles.is_display' => 1
565
            ])
566
            ->first();
567
568
        $json = [];
569
570
        if (is_null($like)) {
571
            $json['message'] = __("You don't like this article !");
572
            $json['error'] = true;
573
574
            $this->set(compact('json'));
575
576
            $this->set('_serialize', 'json');
577
        }
578
579
        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...
580
            $json['url'] = Router::url([
581
                                'action' => 'articleLike',
582
                                $articleId
583
                            ]);
584
            $json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>");
585
            $json['error'] = false;
586
        } else {
587
            $json['message'] = __('An error occurred, please try again later.');
588
            $json['error'] = true;
589
        }
590
591
        $this->set(compact('json'));
592
593
        $this->set('_serialize', 'json');
594
    }
595
596
    /**
597
     * Delete a comment.
598
     *
599
     * @param int $id Id of the comment to delete.
600
     *
601
     * @return \Cake\Network\Response
602
     */
603
    public function deleteComment($id = null)
604
    {
605
        $this->loadModel('BlogArticlesComments');
606
607
        $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...
608
            ->find()
609
            ->contain([
610
                'BlogArticles'
611
            ])
612
            ->where([
613
                'BlogArticlesComments.id' => $id
614
            ])
615
            ->first();
616
617
        if (is_null($comment)) {
618
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
619
620
            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...
621
        }
622
623
        //Current user.
624
        $this->loadModel('Users');
625
        $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...
626
            ->find()
627
            ->contain([
628
                'Groups' => function ($q) {
629
                    return $q->select(['id', 'is_staff']);
630
                }
631
            ])
632
            ->where([
633
                'Users.id' => $this->Auth->user('id')
634
            ])
635
            ->select(['id', 'group_id'])
636
            ->first();
637
638
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
639
            $this->Flash->error(__("You don't have the authorization to delete this comment !"));
640
641
            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...
642
        }
643
644
        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...
645
            $this->Flash->success(__("This comment has been deleted successfully !"));
646
        }
647
648
        return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]);
649
    }
650
651
    /**
652
     * Get the form to edit a comment.
653
     *
654
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
655
     *
656
     * @return void
657
     */
658
    public function getEditComment()
659
    {
660
        if (!$this->request->is('ajax')) {
661
            throw new NotFoundException();
662
        }
663
664
        $this->loadModel('BlogArticlesComments');
665
        $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...
666
            ->find()
667
            ->where([
668
                'BlogArticlesComments.id' => $this->request->data['id']
669
            ])
670
            ->first();
671
672
        $json = [
673
            'error' => false,
674
            'errorMessage' => ''
675
        ];
676
677
        if (is_null($comment)) {
678
            $json['error'] = true;
679
            $json['errorMessage'] = __("This comment doesn't exist or has been deleted !");
680
681
            $this->set(compact('json'));
682
683
            return;
684
        }
685
686
        //Current user.
687
        $this->loadModel('Users');
688
        $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...
689
            ->find()
690
            ->contain([
691
                'Groups' => function ($q) {
692
                    return $q->select(['id', 'is_staff']);
693
                }
694
            ])
695
            ->where([
696
                'Users.id' => $this->Auth->user('id')
697
            ])
698
            ->select(['id', 'group_id'])
699
            ->first();
700
701
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
702
            $json['error'] = true;
703
            $json['errorMessage'] = __("You don't have the authorization to edit this comment !");
704
705
            $this->set(compact('json'));
706
707
            return;
708
        }
709
710
        $this->set(compact('json', 'comment'));
711
    }
712
713
    /**
714
     * Edit a comment.
715
     *
716
     * @param int $id Id of the comment.
717
     *
718
     * @return \Cake\Network\Response
719
     */
720
    public function editComment($id = null)
721
    {
722
        $this->loadModel('BlogArticlesComments');
723
724
        $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...
725
            ->find()
726
            ->contain([
727
                'BlogArticles'
728
            ])
729
            ->where([
730
                'BlogArticlesComments.id' => $id
731
            ])
732
            ->first();
733
734
        if (is_null($comment)) {
735
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
736
737
            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...
738
        }
739
740
        //Current user.
741
        $this->loadModel('Users');
742
        $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...
743
            ->find()
744
            ->contain([
745
                'Groups' => function ($q) {
746
                    return $q->select(['id', 'is_staff']);
747
                }
748
            ])
749
            ->where([
750
                'Users.id' => $this->Auth->user('id')
751
            ])
752
            ->select(['id', 'group_id'])
753
            ->first();
754
755
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
756
            $this->Flash->error(__("You don't have the authorization to edit this comment !"));
757
758
            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...
759
        }
760
761
        $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...
762
        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...
763
            $this->Flash->success(__("This comment has been edited successfully !"));
764
        }
765
766
        return $this->redirect(['action' => 'go', $comment->id]);
767
    }
768
}
769