Passed
Push — master ( b56763...f5ccad )
by Mihail
05:39
created

SearchComments   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 15.25 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 9
loc 59
rs 10
wmc 5
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
B getResult() 0 34 3

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Apps\Model\Front\Search;
4
5
6
use Apps\ActiveRecord\CommentPost;
7
use Apps\Model\Front\Search\Interfaces\SearchContainer;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\Model;
10
use Ffcms\Core\Helper\Text;
11
12
/**
13
 * Class SearchComments. Search instance for comments text.
14
 * @package Apps\Model\Front\Search
15
 */
16
class SearchComments extends Model implements SearchContainer
17
{
18
    private $query;
19
    private $limit;
20
21
    /**
22
     * SearchContainer constructor. Pass string query inside
23
     * @param string $query
24
     * @param int $limit
25
     */
26 View Code Duplication
    public function __construct($query, $limit = 10)
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...
27
    {
28
        $this->query = $query;
29
        $this->limit = (int)$limit;
30
        if ($this->limit < 1) {
31
            $this->limit = 1;
32
        }
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Build search results. Should return array collection: [AbstractSearchResult]
38
     * @return array
39
     */
40
    public function getResult()
41
    {
42
        // search in comments post
43
        $query = CommentPost::where('moderate', '=', 0)
44
            ->search($this->query)
45
            ->take($this->limit)
46
            ->get();
47
48
        // check if response is empty
49
        if ($query->count() < 1) {
50
            return [];
51
        }
52
53
        // build output
54
        $result = [];
55
        foreach ($query as $item) {
56
            /** @var CommentPost $item */
57
            $snippet = App::$Security->strip_tags($item->message);
58
            $snippet = Text::snippet($snippet);
0 ignored issues
show
Bug introduced by
It seems like $snippet defined by \Ffcms\Core\Helper\Text::snippet($snippet) on line 58 can also be of type array; however, Ffcms\Core\Helper\Text::snippet() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
60
            // make unique instance object
61
            $instance = new AbstractSearchResult();
62
            $instance->setTitle(App::$Translate->get('Search', 'Comment on the page'));
63
            $instance->setSnippet($snippet);
64
            $instance->setUri($item->pathway . '#comments-list');
65
            $instance->setDate($item->created_at);
66
            $instance->setRelevance((int)$item->relevance);
0 ignored issues
show
Documentation introduced by
The property relevance does not exist on object<Apps\ActiveRecord\CommentPost>. 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...
67
68
            // add instance to result set
69
            $result[] = $instance;
70
        }
71
72
        return $result;
73
    }
74
}