Passed
Push — master ( 6221c1...b56763 )
by Mihail
05:30
created

SearchContent::getResult()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 21
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 38
rs 8.5806
1
<?php
2
3
4
namespace Apps\Model\Front\Search;
5
6
7
use Apps\Model\Front\Search\Interfaces\SearchContainer;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\Model;
10
use Apps\ActiveRecord\Content;
11
use Ffcms\Core\Helper\Text;
12
use Ffcms\Core\Helper\Type\Str;
13
14
class SearchContent extends Model implements SearchContainer
15
{
16
    private $query;
17
    private $limit;
18
19
    /**
20
     * SearchContainer constructor. Pass string query inside
21
     * @param string $query
22
     * @param int $limit
23
     */
24
    public function __construct($query, $limit = 10)
25
    {
26
        $this->query = $query;
27
        $this->limit = (int)$limit;
28
        if ($this->limit < 1) {
29
            $this->limit = 1;
30
        }
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Build search results
36
     * @return array[AbstractSearchResult]
0 ignored issues
show
Documentation introduced by
The doc-type array[AbstractSearchResult] could not be parsed: Expected "]" at position 2, but found "AbstractSearchResult". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
37
     */
38
    public function getResult()
39
    {
40
        // relevant search by string query
41
        $records = Content::search($this->query)
0 ignored issues
show
Bug introduced by
The method search() does not exist on Apps\ActiveRecord\Content. Did you maybe mean getSearchQueriesForColumn()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
            ->take($this->limit)
43
            ->get();
44
45
        // check if result is not empty
46
        if ($records->count() < 1) {
47
            return [];
48
        }
49
50
        // build result items
51
        $result = [];
52
        foreach ($records as $item) {
53
            /** @var \Apps\ActiveRecord\Content $item */
54
            $title = $item->getLocaled('title');
55
            $text = App::$Security->strip_tags($item->getLocaled('text'));
0 ignored issues
show
Bug introduced by
It seems like $item->getLocaled('text') targeting Ffcms\Core\Arch\ActiveModel::getLocaled() can also be of type null; however, Ffcms\Core\Helper\Security::strip_tags() 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...
56
            $snippet = Text::snippet($text);
0 ignored issues
show
Bug introduced by
It seems like $text defined by \Ffcms\Core\App::$Securi...em->getLocaled('text')) on line 55 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...
57
            // prevent empty items
58
            if (Str::likeEmpty($title)) {
0 ignored issues
show
Bug introduced by
It seems like $title defined by $item->getLocaled('title') on line 54 can also be of type array or string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, 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
                continue;
60
            }
61
62
            // initialize abstract response pattern
63
            $res = new AbstractSearchResult();
64
            $res->setTitle($title);
0 ignored issues
show
Bug introduced by
It seems like $title defined by $item->getLocaled('title') on line 54 can also be of type array or null; however, Apps\Model\Front\Search\...earchResult::setTitle() 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...
65
            $res->setSnippet($snippet);
66
            $res->setDate($item->created_at);
0 ignored issues
show
Bug introduced by
It seems like $item->created_at can also be of type boolean; however, Apps\Model\Front\Search\...SearchResult::setDate() 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...
67
            $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Documentation introduced by
The property relevance does not exist on object<Apps\ActiveRecord\Content>. 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...
68
            $res->setUri('/content/read/' . $item->getPath());
69
70
            // accumulate response var
71
            $result[] = $res;
72
        }
73
        
74
        return $result;
75
    }
76
}