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

Search::actionIndex()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 35
Code Lines 15

Duplication

Lines 3
Ratio 8.57 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 15
c 1
b 1
f 0
nc 3
nop 0
dl 3
loc 35
rs 8.5806
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
6
use Apps\Model\Front\Search\EntitySearchMain;
7
use Extend\Core\Arch\FrontAppController as Controller;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Helper\Type\Str;
11
12
class Search extends Controller
13
{
14
    const EVENT_SEARCH_RUNNED = 'search.run';
15
16
    const QUERY_MAX_LENGTH = 100;
17
18
    /**
19
     * Main search method
20
     * @return string
21
     * @throws \Ffcms\Core\Exception\NativeException
22
     * @throws NotFoundException
23
     * @throws \Ffcms\Core\Exception\SyntaxException
24
     */
25
    public function actionIndex()
26
    {
27
        // get search query value from GET headers
28
        $query = (string)App::$Request->query->get('query', null);
29
        // strip html tags
30
        $query = App::$Security->strip_tags($query);
31
        // get configs
32
        $configs = $this->getConfigs();
33
34
        // check search query length
35 View Code Duplication
        if (Str::likeEmpty($query) || Str::length($query) < (int)$configs['minLength']) {
0 ignored issues
show
Bug introduced by
It seems like $query defined by \Ffcms\Core\App::$Security->strip_tags($query) on line 30 can also be of type array; however, Ffcms\Core\Helper\Type\Str::length() 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...
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...
36
            throw new NotFoundException(__('Search query is too short!'));
37
        }
38
39
        // prevent sh@t query's with big length
40
        if (Str::length($query) > static::QUERY_MAX_LENGTH) {
0 ignored issues
show
Bug introduced by
It seems like $query defined by \Ffcms\Core\App::$Security->strip_tags($query) on line 30 can also be of type array; however, Ffcms\Core\Helper\Type\Str::length() 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...
41
            throw new NotFoundException(__('Search query is too long!'));
42
        }
43
44
        // initialize search controller model
45
        $model = new EntitySearchMain($query, $configs);
0 ignored issues
show
Bug introduced by
It seems like $query defined by \Ffcms\Core\App::$Security->strip_tags($query) on line 30 can also be of type array; however, Apps\Model\Front\Search\...archMain::__construct() 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...
46
        // try to search by default apps
47
        $model->make();
48
49
        // register search event to allow extend it model results
50
        App::$Event->run(static::EVENT_SEARCH_RUNNED, [
51
            'model' => $model
52
        ]);
53
54
        // render output view with search result
55
        return App::$View->render('index', [
56
            'model' => $model,
57
            'query' => $query
58
        ]);
59
    }
60
61
}