Passed
Push — master ( b534e3...17b30a )
by Mihail
19:51
created

ActionTag   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 40
rs 10
wmc 4
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B tag() 0 30 4
1
<?php
2
3
namespace Apps\Controller\Front\Content;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\View;
7
use Ffcms\Core\Exception\NotFoundException;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Str;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Apps\ActiveRecord\Content as ContentRecord;
13
14
/**
15
 * Trait ActionTag
16
 * @package Apps\Controller\Front\Content
17
 * @property View $view
18
 * @property Request $request
19
 * @property Response $response
20
 * @method array getConfigs
21
 */
22
trait ActionTag
23
{
24
    /**
25
     * List latest by created_at content items contains tag name
26
     * @param string $name
27
     * @return null|string
28
     * @throws NotFoundException
29
     * @throws \Ffcms\Core\Exception\SyntaxException
30
     */
31
    public function tag($name)
32
    {
33
        // remove spaces and other sh@ts
34
        $name = App::$Security->strip_tags(trim($name));
35
36
        // check if tag is not empty
37
        if (!Any::isStr($name) || Str::length($name) < 2) {
0 ignored issues
show
Bug introduced by
It seems like $name defined by \Ffcms\Core\App::$Securi...strip_tags(trim($name)) on line 34 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...
38
            throw new NotFoundException(__('Tag is empty or is too short!'));
39
        }
40
41
        // get equal rows order by creation date
42
        $records = ContentRecord::where('meta_keywords', 'like', '%' . $name . '%')
43
            ->orderBy('created_at', 'DESC')
44
            ->take(self::TAG_PER_PAGE);
45
        // check if result is not empty
46
        if ($records->count() < 1) {
47
            throw new NotFoundException(__('Nothing founded'));
48
        }
49
50
        // define tag list event
51
        App::$Event->run(static::EVENT_TAG_LIST, [
52
            'records' => $records
53
        ]);
54
55
        // render response
56
        return $this->view->render('tag', [
57
            'records' => $records->get(),
58
            'tag' => $name
59
        ]);
60
    }
61
}
62