Passed
Push — master ( 10220a...9ad793 )
by Mihail
05:11
created

Newcomment::init()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
cc 5
eloc 11
c 4
b 3
f 0
nc 16
nop 0
dl 0
loc 18
rs 8.8571
1
<?php
2
3
namespace Widgets\Front\Newcomment;
4
5
use Ffcms\Core\App;
6
use Extend\Core\Arch\FrontWidget as AbstractWidget;
7
use Ffcms\Core\Traits\ClassTools;
8
use Apps\ActiveRecord\CommentPost;
9
10
/**
11
 * Class Newcomment. New comments widget. Show new comments in system.
12
 * @package Widgets\Front\Newcomment
13
 */
14
class Newcomment extends AbstractWidget
15
{
16
    use ClassTools;
17
18
    public $snippet;
19
    public $count;
20
    public $cache;
21
    public $lang;
22
23
    private $_cacheName;
24
25
	/**
26
	 * Set default configs if not passed
27
	 * {@inheritDoc}
28
	 * @see \Ffcms\Core\Arch\Widget::init()
29
	 */
30
    public function init()
31
    {
32
    	$cfg = $this->getConfigs();
33
    	if ($this->snippet === null) {
34
    	    $this->snippet = $cfg['snippet'];
35
    	}
36
    	if ($this->count === null) {
37
    	    $this->count = $cfg['count'];
38
    	}
39
    	if ($this->cache === null) {
40
    	    $this->cache = $cfg['cache'];
41
    	}
42
        if ($this->lang === null) {
43
            $this->lang = App::$Request->getLanguage();
44
        }
45
46
        $this->_cacheName = 'widget.newcomment.' . $this->createStringClassSnapshotHash();
47
    }
48
49
    /**
50
     * Show latest comments
51
     * {@inheritDoc}
52
     * @see \Ffcms\Core\Arch\Widget::display()
53
     * @throws \Ffcms\Core\Exception\NativeException
54
     * @throws \Ffcms\Core\Exception\SyntaxException
55
     */
56
    public function display()
57
    {
58
        // work with cache and make query
59
        $records = null;
0 ignored issues
show
Unused Code introduced by
$records is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        if ((int)$this->cache > 0) {
61
            if (App::$Cache->get($this->_cacheName) !== null) {
62
                $records = App::$Cache->get($this->_cacheName);
63
            } else {
64
                $records = $this->makeQuery();
65
                App::$Cache->set($this->_cacheName, $records, $this->cache);
0 ignored issues
show
Documentation introduced by
$records is of type object, but the function expects a string.

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...
66
            }
67
        } else {
68
            $records = $this->makeQuery();
69
        }
70
71
        // check if records is found
72
        if ($records === null) {
73
            return __('Comments not yet found');
74
        }
75
76
        // render view
77
    	return App::$View->render('widgets/newcomment/default', [
78
    	    'records' => $records,
79
    	    'snippet' => $this->snippet
80
    	]);
81
    }
82
83
    /**
84
     * Make database query and return results
85
     * @return object
86
     */
87
    private function makeQuery()
88
    {
89
        $records = CommentPost::where('lang', $this->lang)
90
            ->where('moderate', 0);
91
92
        if ($records === null || $records->count() < 1) {
93
            return null;
94
        }
95
96
        return $records->orderBy('id', 'DESC')
97
        ->take($this->count)
98
        ->get();
99
    }
100
}