GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#2835)
by
unknown
06:01
created

AuthorDatasource::execute()   F

Complexity

Conditions 36
Paths 3045

Size

Total Lines 151
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 84
c 0
b 0
f 0
nc 3045
nop 1
dl 0
loc 151
rs 2

How to fix   Long Method    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
/**
4
 * @package data-sources
5
 */
6
/**
7
 * The `AuthorDatasource` extends the base `Datasource` class and allows
8
 * the retrieval of Author information from the current Symphony installation.
9
 *
10
 * @since Symphony 2.3
11
 */
12
class AuthorDatasource extends Datasource
13
{
14
    public function __processAuthorFilter($field, $filter)
15
    {
16
        if (!is_array($filter)) {
17
            $filter = preg_split('/,\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY);
18
            $filter = array_map('trim', $filter);
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
            $filter = array_map('trim', /** @scrutinizer ignore-type */ $filter);
Loading history...
19
        }
20
21
        $authors = Symphony::Database()
22
            ->select(['id'])
23
            ->from('tbl_authors')
24
            ->where([$field => ['in' => $filter]])
25
            ->execute()
26
            ->column('id');
27
28
        return is_array($authors) && !empty($authors) ? $authors : null;
29
    }
30
31
    public function execute(array &$param_pool = null)
32
    {
33
        $authorsQuery = (new AuthorManager)
34
            ->select()
35
            ->sort((string)$this->dsParamSORT, $this->dsParamORDER);
36
37
        if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) {
38
            $author_ids = [];
39
            foreach ($this->dsParamFILTERS as $field => $value) {
40
                if (!is_array($value) && trim($value) == '') {
41
                    continue;
42
                }
43
44
                $ret = $this->__processAuthorFilter($field, $value);
45
46
                if (empty($ret)) {
47
                    $author_ids = array();
48
                    break;
49
                }
50
51
                if (empty($author_ids)) {
52
                    $author_ids = $ret;
53
                    continue;
54
                }
55
56
                $author_ids = array_intersect($author_ids, $ret);
57
            }
58
            if (!empty($author_ids)) {
59
                $authorsQuery->authors(array_values($author_ids));
0 ignored issues
show
Bug introduced by
array_values($author_ids) of type array is incompatible with the type integer expected by parameter $author_ids of AuthorQuery::authors(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                $authorsQuery->authors(/** @scrutinizer ignore-type */ array_values($author_ids));
Loading history...
60
            }
61
        }
62
63
        $authors = $authorsQuery->execute()->rows();
64
65
        if (empty($authors) && $this->dsParamREDIRECTONEMPTY === 'yes') {
0 ignored issues
show
Bug Best Practice introduced by
The property dsParamREDIRECTONEMPTY does not exist on AuthorDatasource. Did you maybe forget to declare it?
Loading history...
66
            throw new FrontendPageNotFoundException;
67
        } elseif (empty($authors)) {
68
            $result = $this->emptyXMLSet();
69
            return $result;
70
        } else {
71
            if ($this->_negate_result === true) {
72
                return $this->negateXMLSet();
73
            }
74
75
            if (!$this->_param_output_only) {
76
                $result = new XMLElement($this->dsParamROOTELEMENT);
0 ignored issues
show
Bug Best Practice introduced by
The property dsParamROOTELEMENT does not exist on AuthorDatasource. Did you maybe forget to declare it?
Loading history...
77
            }
78
79
            $singleParam = false;
80
            $key = 'ds-' . $this->dsParamROOTELEMENT;
81
82
            if (isset($this->dsParamPARAMOUTPUT)) {
83
                if (!is_array($this->dsParamPARAMOUTPUT)) {
84
                    $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT);
0 ignored issues
show
Bug Best Practice introduced by
The property dsParamPARAMOUTPUT does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
                }
86
87
                $singleParam = count($this->dsParamPARAMOUTPUT) === 1;
88
            }
89
90
            foreach ($authors as $author) {
91
                if (isset($this->dsParamPARAMOUTPUT)) {
92
                    foreach ($this->dsParamPARAMOUTPUT as $param) {
93
                        // The new style of paramater is `ds-datasource-handle.field-handle`
94
                        $param_key = $key . '.' . str_replace(':', '-', $param);
95
96
                        if (!is_array($param_pool[$param_key])) {
97
                            $param_pool[$param_key] = array();
98
                        }
99
100
                        $param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
101
102
                        if ($singleParam) {
103
                            if (!is_array($param_pool[$key])) {
104
                                $param_pool[$key] = array();
105
                            }
106
107
                            $param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
108
                        }
109
                    }
110
                }
111
112
                if ($this->_param_output_only) {
113
                    continue;
114
                }
115
116
                $xAuthor = new XMLElement('author');
117
                $xAuthor->setAttributeArray(array(
118
                    'id' => $author->get('id'),
119
                    'user-type' => $author->get('user_type'),
120
                    'primary-account' => $author->get('primary')
121
                ));
122
123
                // No included elements, so just create the Author XML
124
                if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) {
125
                    $result->appendChild($xAuthor);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
126
                } else {
127
                    // Name
128
                    if (in_array('name', $this->dsParamINCLUDEDELEMENTS)) {
129
                        $xAuthor->appendChild(
130
                            new XMLElement('name', $author->getFullName())
131
                        );
132
                    }
133
134
                    // Username
135
                    if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) {
136
                        $xAuthor->appendChild(
137
                            new XMLElement('username', $author->get('username'))
138
                        );
139
                    }
140
141
                    // Email
142
                    if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) {
143
                        $xAuthor->appendChild(
144
                            new XMLElement('email', $author->get('email'))
145
                        );
146
                    }
147
148
                    // Author Token
149
                    if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) {
150
                        $xAuthor->appendChild(
151
                            new XMLElement('author-token', $author->createAuthToken())
152
                        );
153
                    }
154
155
                    // Default Area
156
                    if (in_array('default-area', $this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area'))) {
157
                        // Section
158
                        $section = (new SectionManager)
159
                            ->select()
160
                            ->section($author->get('default_area'))
161
                            ->execute()
162
                            ->rows();
163
                        if ($section) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $section of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
164
                            $default_area = new XMLElement('default-area', $section->get('name'));
165
                            $default_area->setAttributeArray(array('id' => $section->get('id'), 'handle' => $section->get('handle'), 'type' => 'section'));
166
                            $xAuthor->appendChild($default_area);
167
168
                            // Pages
169
                        } else {
170
                            $default_area = new XMLElement('default-area', $author->get('default_area'));
171
                            $default_area->setAttribute('type', 'page');
172
                            $xAuthor->appendChild($default_area);
173
                        }
174
                    }
175
176
                    $result->appendChild($xAuthor);
177
                }
178
            }
179
        }
180
181
        return $result;
182
    }
183
}
184