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.
Completed
Push — integration ( 5cdb31...81c93f )
by Brendan
04:03
created

AuthorDatasource::execute()   F

Complexity

Conditions 37
Paths 1827

Size

Total Lines 143
Code Lines 77

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 143
rs 2
cc 37
eloc 77
nc 1827
nop 1

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
            $bits = preg_split('/,\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY);
18
            $bits = array_map('trim', $bits);
19
        } else {
20
            $bits = $filter;
21
        }
22
23
        $q = Database::addPlaceholders($bits);
24
        $authors = Symphony::Database()->fetchCol('id', sprintf("
25
                SELECT `id` FROM `tbl_authors`
26
                WHERE `%s` IN (%s)",
27
                $field,
28
                $q
29
        ));
30
31
        return (is_array($authors) && !empty($authors) ? $authors : null);
32
    }
33
34
    public function execute(array &$param_pool = null)
35
    {
36
        $author_ids = array();
37
38
        if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) {
39
            foreach ($this->dsParamFILTERS as $field => $value) {
0 ignored issues
show
Bug introduced by
The property dsParamFILTERS 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
                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
59
            $authors = AuthorManager::fetchByID(array_values($author_ids));
60
        } else {
61
            $authors = AuthorManager::fetch($this->dsParamSORT, $this->dsParamORDER);
0 ignored issues
show
Bug introduced by
The property dsParamSORT 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...
Bug introduced by
The property dsParamORDER 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...
62
        }
63
64
        if ((!is_array($authors) || empty($authors)) && $this->dsParamREDIRECTONEMPTY === 'yes') {
0 ignored issues
show
Bug introduced by
The property dsParamREDIRECTONEMPTY 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...
65
            throw new FrontendPageNotFoundException;
66
        } elseif (!is_array($authors) || empty($authors)) {
67
            $result = $this->emptyXMLSet();
68
            return $result;
69
        } else {
70
            if ($this->_negate_result === true) {
71
                return $this->negateXMLSet();
72
            }
73
74
            if (!$this->_param_output_only) {
75
                $result = new XMLElement($this->dsParamROOTELEMENT);
0 ignored issues
show
Bug introduced by
The property dsParamROOTELEMENT 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...
76
            }
77
78
            $singleParam = false;
79
            $key = 'ds-' . $this->dsParamROOTELEMENT;
80
81
            if (isset($this->dsParamPARAMOUTPUT)) {
82
                if (!is_array($this->dsParamPARAMOUTPUT)) {
83
                    $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT);
0 ignored issues
show
Bug introduced by
The property dsParamPARAMOUTPUT 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...
84
                }
85
86
                $singleParam = count($this->dsParamPARAMOUTPUT) === 1;
87
            }
88
89
            foreach ($authors as $author) {
90
                if (isset($this->dsParamPARAMOUTPUT)) {
91
                    foreach ($this->dsParamPARAMOUTPUT as $param) {
92
                        // The new style of paramater is `ds-datasource-handle.field-handle`
93
                        $param_key = $key . '.' . str_replace(':', '-', $param);
94
95
                        if (!is_array($param_pool[$param_key])) {
96
                            $param_pool[$param_key] = array();
97
                        }
98
99
                        $param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
100
101
                        if ($singleParam) {
102
                            if (!is_array($param_pool[$key])) {
103
                                $param_pool[$key] = array();
104
                            }
105
106
                            $param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
107
                        }
108
                    }
109
                }
110
111
                if ($this->_param_output_only) {
112
                    continue;
113
                }
114
115
                $xAuthor = new XMLElement('author');
116
                $xAuthor->setAttributeArray(array(
117
                    'id' => $author->get('id'),
118
                    'user-type' => $author->get('user_type'),
119
                    'primary-account' => $author->get('primary')
120
                ));
121
122
                // No included elements, so just create the Author XML
123
                if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) {
124
                    $result->appendChild($xAuthor);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
125
                } else {
126
                    // Name
127
                    if (in_array('name', $this->dsParamINCLUDEDELEMENTS)) {
0 ignored issues
show
Bug introduced by
The property dsParamINCLUDEDELEMENTS 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...
128
                        $xAuthor->appendChild(
129
                            new XMLElement('name', $author->getFullName())
130
                        );
131
                    }
132
133
                    // Username
134
                    if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) {
135
                        $xAuthor->appendChild(
136
                            new XMLElement('username', $author->get('username'))
137
                        );
138
                    }
139
140
                    // Email
141
                    if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) {
142
                        $xAuthor->appendChild(
143
                            new XMLElement('email', $author->get('email'))
144
                        );
145
                    }
146
147
                    // Author Token
148
                    if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) {
149
                        $xAuthor->appendChild(
150
                            new XMLElement('author-token', $author->createAuthToken())
151
                        );
152
                    }
153
154
                    // Default Area
155
                    if (in_array('default-area', $this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area'))) {
156
                        // Section
157
                        if ($section = SectionManager::fetch($author->get('default_area'))) {
158
                            $default_area = new XMLElement('default-area', $section->get('name'));
159
                            $default_area->setAttributeArray(array('id' => $section->get('id'), 'handle' => $section->get('handle'), 'type' => 'section'));
160
                            $xAuthor->appendChild($default_area);
161
162
                            // Pages
163
                        } else {
164
                            $default_area = new XMLElement('default-area', $author->get('default_area'));
165
                            $default_area->setAttribute('type', 'page');
166
                            $xAuthor->appendChild($default_area);
167
                        }
168
                    }
169
170
                    $result->appendChild($xAuthor);
171
                }
172
            }
173
        }
174
175
        return $result;
176
    }
177
}
178