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.

AuthorDatasource   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 86
c 0
b 0
f 0
dl 0
loc 163
rs 9.1199
wmc 41

2 Methods

Rating   Name   Duplication   Size   Complexity  
F execute() 0 142 37
A __processAuthorFilter() 0 17 4

How to fix   Complexity   

Complex Class

Complex classes like AuthorDatasource often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AuthorDatasource, and based on these observations, apply Extract Interface, too.

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
            $bits = preg_split('/,\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY);
18
            $bits = array_map('trim', $bits);
0 ignored issues
show
Bug introduced by
It seems like $bits 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
            $bits = array_map('trim', /** @scrutinizer ignore-type */ $bits);
Loading history...
19
        } else {
20
            $bits = $filter;
21
        }
22
23
        $authors = Symphony::Database()->fetchCol('id', sprintf("
24
                SELECT `id` FROM `tbl_authors`
25
                WHERE `%s` IN ('%s')",
26
                $field,
27
                implode("', '", $bits)
28
        ));
29
30
        return (is_array($authors) && !empty($authors) ? $authors : null);
31
    }
32
33
    public function execute(array &$param_pool = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$param_pool" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$param_pool"; expected 0 but found 1
Loading history...
34
    {
35
        $author_ids = array();
36
37
        if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) {
38
            foreach ($this->dsParamFILTERS as $field => $value) {
39
                if (!is_array($value) && trim($value) == '') {
40
                    continue;
41
                }
42
43
                $ret = $this->__processAuthorFilter($field, $value);
44
45
                if (empty($ret)) {
46
                    $author_ids = array();
47
                    break;
48
                }
49
50
                if (empty($author_ids)) {
51
                    $author_ids = $ret;
52
                    continue;
53
                }
54
55
                $author_ids = array_intersect($author_ids, $ret);
56
            }
57
58
            $authors = AuthorManager::fetchByID(array_values($author_ids));
59
        } else {
60
            $authors = AuthorManager::fetch($this->dsParamSORT, $this->dsParamORDER);
61
        }
62
63
        if ((!is_array($authors) || 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...
64
            throw new FrontendPageNotFoundException;
65
        } elseif (!is_array($authors) || empty($authors)) {
66
            $result = $this->emptyXMLSet();
67
            return $result;
68
        } else {
69
            if ($this->_negate_result === true) {
70
                return $this->negateXMLSet();
71
            }
72
73
            if (!$this->_param_output_only) {
74
                $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...
75
            }
76
77
            $singleParam = false;
78
            $key = 'ds-' . $this->dsParamROOTELEMENT;
79
80
            if (isset($this->dsParamPARAMOUTPUT)) {
81
                if (!is_array($this->dsParamPARAMOUTPUT)) {
82
                    $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...
83
                }
84
85
                $singleParam = count($this->dsParamPARAMOUTPUT) === 1;
86
            }
87
88
            foreach ($authors as $author) {
89
                if (isset($this->dsParamPARAMOUTPUT)) {
90
                    foreach ($this->dsParamPARAMOUTPUT as $param) {
91
                        // The new style of paramater is `ds-datasource-handle.field-handle`
92
                        $param_key = $key . '.' . str_replace(':', '-', $param);
93
94
                        if (!is_array($param_pool[$param_key])) {
95
                            $param_pool[$param_key] = array();
96
                        }
97
98
                        $param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
99
100
                        if ($singleParam) {
101
                            if (!is_array($param_pool[$key])) {
102
                                $param_pool[$key] = array();
103
                            }
104
105
                            $param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
106
                        }
107
                    }
108
                }
109
110
                if ($this->_param_output_only) {
111
                    continue;
112
                }
113
114
                $xAuthor = new XMLElement('author');
115
                $xAuthor->setAttributeArray(array(
116
                    'id' => $author->get('id'),
117
                    'user-type' => $author->get('user_type'),
118
                    'primary-account' => $author->get('primary')
119
                ));
120
121
                // No included elements, so just create the Author XML
122
                if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) {
123
                    $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...
124
                } else {
125
                    // Name
126
                    if (in_array('name', $this->dsParamINCLUDEDELEMENTS)) {
127
                        $xAuthor->appendChild(
128
                            new XMLElement('name', $author->getFullName())
129
                        );
130
                    }
131
132
                    // Username
133
                    if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) {
134
                        $xAuthor->appendChild(
135
                            new XMLElement('username', $author->get('username'))
136
                        );
137
                    }
138
139
                    // Email
140
                    if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) {
141
                        $xAuthor->appendChild(
142
                            new XMLElement('email', $author->get('email'))
143
                        );
144
                    }
145
146
                    // Author Token
147
                    if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) {
148
                        $xAuthor->appendChild(
149
                            new XMLElement('author-token', $author->createAuthToken())
150
                        );
151
                    }
152
153
                    // Default Area
154
                    if (in_array('default-area', $this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area'))) {
155
                        // Section
156
                        if ($section = SectionManager::fetch($author->get('default_area'))) {
157
                            $default_area = new XMLElement('default-area', $section->get('name'));
0 ignored issues
show
Bug introduced by
It seems like $section->get('name') can also be of type array; however, parameter $value of XMLElement::__construct() does only seem to accept XMLElement|string, 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

157
                            $default_area = new XMLElement('default-area', /** @scrutinizer ignore-type */ $section->get('name'));
Loading history...
158
                            $default_area->setAttributeArray(array('id' => $section->get('id'), 'handle' => $section->get('handle'), 'type' => 'section'));
159
                            $xAuthor->appendChild($default_area);
160
161
                            // Pages
162
                        } else {
163
                            $default_area = new XMLElement('default-area', $author->get('default_area'));
164
                            $default_area->setAttribute('type', 'page');
165
                            $xAuthor->appendChild($default_area);
166
                        }
167
                    }
168
169
                    $result->appendChild($xAuthor);
170
                }
171
            }
172
        }
173
174
        return $result;
175
    }
176
}
177