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

ActionRss::rss()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 28
c 1
b 1
f 0
nc 3
nop 0
dl 0
loc 55
rs 8.7752

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Apps\Controller\Front\Content;
5
6
use Apps\Model\Front\Content\EntityCategoryList;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Suin\RSSWriter\Channel;
13
use Suin\RSSWriter\Feed;
14
use Suin\RSSWriter\Item;
15
16
/**
17
 * Trait ActionRss
18
 * @package Apps\Controller\Front\Content
19
 * @property View $view
20
 * @property Request $request
21
 * @property Response $response
22
 * @method array getConfigs
23
 */
24
trait ActionRss
25
{
26
    /**
27
     * Display rss feeds from content category
28
     * @return string
29
     * @throws ForbiddenException
30
     */
31
    public function rss()
32
    {
33
        $path = $this->request->getPathWithoutControllerAction();
34
        $configs = $this->getConfigs();
0 ignored issues
show
Bug introduced by
It seems like getConfigs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
36
        // build model data
37
        $model = new EntityCategoryList($path, $configs, 0);
38
        // remove global layout
39
        $this->layout = null;
0 ignored issues
show
Bug introduced by
The property layout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
41
        // check if rss display allowed for this category
42
        if ((int)$model->category['configs']['showRss'] !== 1) {
43
            throw new ForbiddenException(__('Rss feed is disabled for this category'));
44
        }
45
46
        // set rss/xml header
47
        $this->response->headers->set('Content-Type', 'application/rss+xml');
48
49
        // initialize rss feed objects
50
        $feed = new Feed();
51
        $channel = new Channel();
52
53
        // set channel data
54
        $channel->title($model->category['title'])
0 ignored issues
show
Bug introduced by
It seems like $model->category['title'] can also be of type array or null; however, Suin\RSSWriter\Channel::title() 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...
55
            ->description($model->category['description'])
0 ignored issues
show
Bug introduced by
It seems like $model->category['description'] can also be of type array or null; however, Suin\RSSWriter\Channel::description() 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...
56
            ->url(App::$Alias->baseUrl . '/content/list/' . $model->category['path'])
57
            ->appendTo($feed);
58
59
        // add content data
60
        if ($model->getContentCount() > 0) {
61
            foreach ($model->items as $row) {
62
                $item = new Item();
63
                // add title, short text, url
64
                $item->title($row['title'])
65
                    ->description($row['text'])
66
                    ->url(App::$Alias->baseUrl . $row['uri']);
67
                // add poster
68
                if ($row['thumb'] !== null) {
69
                    $item->enclosure(App::$Alias->scriptUrl . $row['thumb'], $row['thumbSize'], 'image/jpeg');
70
                }
71
72
                // append response to channel
73
                $item->appendTo($channel);
74
            }
75
        }
76
        // define rss read event
77
        App::$Event->run(static::EVENT_RSS_READ, [
78
            'model' => $model,
79
            'feed' => $feed,
80
            'channel' => $channel
81
        ]);
82
83
        // render response from feed object
84
        return $feed->render();
85
    }
86
}
87