Passed
Push — master ( f69251...ac6453 )
by Andreas
14:50
created

net_nehmer_static_handler_view::_handler_view()   C

Complexity

Conditions 13
Paths 34

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13.33

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 34
nop 3
dl 0
loc 50
ccs 28
cts 32
cp 0.875
crap 13.33
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

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
 * @package net.nehmer.static
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
use midcom\workflow\dialog;
11
12
/**
13
 * n.n.static index page handler
14
 *
15
 * @package net.nehmer.static
16
 */
17
class net_nehmer_static_handler_view extends midcom_baseclasses_components_handler
18
{
19
    /**
20
     * The article to display
21
     *
22
     * @var midcom_db_article
23
     */
24
    private $_article;
25
26
    /**
27
     * The Datamanager of the article to display.
28
     *
29
     * @var datamanager
30
     */
31
    private $_datamanager;
32
33
    /**
34
     * Simple helper which references all important members to the request data listing
35
     * for usage within the style listing.
36
     */
37 3
    private function _prepare_request_data()
38
    {
39 3
        $this->_request_data['article'] = $this->_article;
40 3
        $this->_request_data['datamanager'] = $this->_datamanager;
41
42 3
        $buttons = [];
43 3
        $workflow = $this->get_workflow('datamanager');
44 3
        if ($this->_article->can_do('midgard:update')) {
45 2
            $buttons[] = $workflow->get_button("edit/{$this->_article->guid}/", [
46 2
                MIDCOM_TOOLBAR_ACCESSKEY => 'e',
47
            ]);
48
        }
49
50 3
        if (   $this->_article->topic === $this->_topic->id
51 3
            && $this->_article->can_do('midgard:delete')) {
52 2
            $delete = $this->get_workflow('delete', ['object' => $this->_article]);
53 2
            $buttons[] = $delete->get_button("delete/{$this->_article->guid}/");
54
        }
55
56 3
        $this->_view_toolbar->add_items($buttons);
57 3
    }
58
59
    /**
60
     * Looks up an article to display. If the handler_id is 'index', the index article is tried to be
61
     * looked up, otherwise the article name is taken from args[0]. Triggered error messages are
62
     * generated accordingly. A missing index will trigger a forbidden error, a missing regular
63
     * article a 404.
64
     *
65
     * If create privileges apply, we relocate to the index creation article
66
     */
67 3
    public function _handler_view(string $handler_id, array $args, array &$data)
68
    {
69 3
        if ($handler_id == 'index') {
70 1
            if ($response = $this->_load_index_article()) {
71 1
                return $response;
72
            }
73
        } else {
74 2
            $qb = net_nehmer_static_viewer::get_topic_qb($this->_config, $this->_topic->id);
75 2
            $qb->add_constraint('name', '=', $args[0]);
76 2
            $qb->add_constraint('up', '=', 0);
77 2
            $qb->set_limit(1);
78
79 2
            $this->_article = $qb->get_result(0);
0 ignored issues
show
Documentation Bug introduced by
It seems like $qb->get_result(0) can also be of type false. However, the property $_article is declared as type midcom_db_article. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
80 2
            if (!$this->_article) {
81
                throw new midcom_error_notfound('Could not find ' . $args[0]);
82
            }
83
        }
84
85 3
        if ($handler_id == 'view_raw') {
86
            midcom::get()->skip_page_style = true;
87
        }
88
89 3
        $this->_datamanager = new datamanager($this->_request_data['schemadb']);
90 3
        $this->_datamanager->set_storage($this->_article);
91
92 3
        $arg = $this->_article->name ?: $this->_article->guid;
93 3
        if (   $arg != 'index'
94 3
            && $this->_config->get('hide_navigation')) {
95
            $this->add_breadcrumb("{$arg}/", $this->_article->title);
96
        }
97
98 3
        $this->_prepare_request_data();
99
100 3
        midcom::get()->metadata->set_request_metadata($this->_article->metadata->revised, $this->_article->guid);
101 3
        $this->bind_view_to_object($this->_article, $this->_datamanager->get_schema()->get_name());
102
103 3
        if (   $this->_config->get('indexinnav')
104 3
            || $this->_config->get('autoindex')
105 3
            || $this->_article->name != 'index') {
106 1
            $this->set_active_leaf($this->_article->id);
107
        }
108
109 3
        if (   $this->_config->get('folder_in_title')
110 3
            && $this->_topic->extra != $this->_article->title) {
111
            midcom::get()->head->set_pagetitle("{$this->_topic->extra}: {$this->_article->title}");
112
        } else {
113 3
            midcom::get()->head->set_pagetitle($this->_article->title);
114
        }
115 3
        $data['view_article'] = $data['datamanager']->get_content_html();
116 3
        return $this->show('show-article');
117
    }
118
119 1
    private function _load_index_article()
120
    {
121 1
        $qb = net_nehmer_static_viewer::get_topic_qb($this->_config, $this->_topic->id);
122 1
        $qb->add_constraint('name', '=', 'index');
123 1
        $qb->set_limit(1);
124 1
        $this->_article = $qb->get_result(0);
0 ignored issues
show
Documentation Bug introduced by
It seems like $qb->get_result(0) can also be of type false. However, the property $_article is declared as type midcom_db_article. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
125
126 1
        if (empty($this->_article)) {
127
            if ($this->_topic->can_do('midgard:create')) {
128
                // Check via non-ACLd QB that the topic really doesn't have index article before relocating
129
                $index_qb = midcom_db_article::new_query_builder();
130
                $index_qb->add_constraint('topic', '=', $this->_topic->id);
131
                $index_qb->add_constraint('name', '=', 'index');
132
                if ($index_qb->count_unchecked() == 0) {
133
                    $schema = $this->_request_data['schemadb']->get_first();
134
                    $this->_request_data['schema'] = $schema->get_name();
135
                    dialog::add_head_elements();
136
                    return $this->show('index-missing');
137
                }
138
            }
139
140
            throw new midcom_error_forbidden('Directory index forbidden');
141
        }
142 1
    }
143
}
144