Passed
Push — master ( 004834...f22cf7 )
by Mihail
04:58
created

Newcomment   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 4
B display() 0 24 4
A makeQuery() 0 12 3
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\OopTools;
8
use Apps\ActiveRecord\CommentPost;
9
use Apps\Model\Api\Comments\EntityCommentData;
10
11
class Newcomment extends AbstractWidget
12
{
13
    use OopTools;
14
    
15
	public $snippet;
16
	public $count;
17
	public $cache;
18
19
    public function init()
20
    {
21
    	$cfg = $this->getConfigs();
22
    	if ($this->snippet === null) {
23
    	    $this->snippet = $cfg['snippet'];
24
    	}
25
    	if ($this->count === null) {
26
    	    $this->count = $cfg['count'];
27
    	}
28
    	if ($this->cache === null) {
29
    	    $this->cache = $cfg['cache'];
30
    	}
31
    }
32
    
33
    public function display()
34
    {
35
        $classHash = $this->createStringClassSnapshotHash();
36
        
37
        $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...
38
        if ((int)$this->cache > 0) {
39
            if (App::$Cache->get('widget.newcomment.' . $classHash) !== null) {
40
                $records = App::$Cache->get('widget.newcomment.' . $classHash);
41
            } else {
42
                $records = $this->makeQuery();
43
            }
44
        } else {
45
            $records = $this->makeQuery();
46
        }
47
        // check if records is found
48
        if ($records === null) {
49
            return __('Comments not yet found');
50
        }
51
52
    	return App::$View->render('widgets/newcomment/default', [
53
    	    'records' => $records,
54
    	    'snippet' => $this->snippet
55
    	]);
56
    }
57
    
58
    private function makeQuery()
59
    {
60
        $records = CommentPost::where('lang', '=', App::$Request->getLanguage());
61
        
62
        if ($records === null || $records->count() < 1) {
63
            return null;
64
        }
65
        
66
        return $records->orderBy('id', 'DESC')
67
        ->take($this->count)
68
        ->get();
69
    }
70
}