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
Pull Request — integration (#2604)
by Brendan
04:52
created

AuthorDatasource   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 173
rs 8.2769
wmc 41
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
F execute() 0 150 37
A __processAuthorFilter() 0 19 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
    /**
8
     * The `AuthorDatasource` extends the base `Datasource` class and allows
9
     * the retrieval of Author information from the current Symphony installation.
10
     *
11
     * @since Symphony 2.3
12
     */
13
    class AuthorDatasource extends Datasource
14
    {
15
        public function execute(array &$param_pool = null)
16
        {
17
            $author_ids = array();
18
19
            if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) {
20
                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...
21
                    if (!is_array($value) && trim($value) === '') {
22
                        continue;
23
                    }
24
25
                    $ret = $this->__processAuthorFilter($field, $value);
26
27
                    if (empty($ret)) {
28
                        $author_ids = array();
29
                        break;
30
                    }
31
32
                    if (empty($author_ids)) {
33
                        $author_ids = $ret;
34
                        continue;
35
                    }
36
37
                    $author_ids = array_intersect($author_ids, $ret);
38
                }
39
40
                $authors = AuthorManager::fetchByID(array_values($author_ids));
41
            } else {
42
                $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...
43
            }
44
45
            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...
46
                throw new FrontendPageNotFoundException;
47
            } elseif (!is_array($authors) || empty($authors)) {
48
                $result = $this->emptyXMLSet();
49
50
                return $result;
51
            } else {
52
                if ($this->_negate_result === true) {
53
                    return $this->negateXMLSet();
54
                }
55
56
                if (!$this->_param_output_only) {
57
                    $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...
58
                }
59
60
                $singleParam = false;
61
                $key = 'ds-' . $this->dsParamROOTELEMENT;
62
63
                if (isset($this->dsParamPARAMOUTPUT)) {
64
                    if (!is_array($this->dsParamPARAMOUTPUT)) {
65
                        $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...
66
                    }
67
68
                    $singleParam = count($this->dsParamPARAMOUTPUT) === 1;
69
                }
70
71
                foreach ($authors as $author) {
72
                    if (isset($this->dsParamPARAMOUTPUT)) {
73
                        foreach ($this->dsParamPARAMOUTPUT as $param) {
74
                            // The new style of paramater is `ds-datasource-handle.field-handle`
75
                            $param_key = $key . '.' . str_replace(':', '-', $param);
76
77
                            if (!is_array($param_pool[$param_key])) {
78
                                $param_pool[$param_key] = array();
79
                            }
80
81
                            $param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
82
83
                            if ($singleParam) {
84
                                if (!is_array($param_pool[$key])) {
85
                                    $param_pool[$key] = array();
86
                                }
87
88
                                $param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param));
89
                            }
90
                        }
91
                    }
92
93
                    if ($this->_param_output_only) {
94
                        continue;
95
                    }
96
97
                    $xAuthor = new XMLElement('author');
98
                    $xAuthor->setAttributeArray(array(
99
                        'id' => $author->get('id'),
100
                        'user-type' => $author->get('user_type'),
101
                        'primary-account' => $author->get('primary')
102
                    ));
103
104
                    // No included elements, so just create the Author XML
105
                    if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) {
106
                        $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...
107
                    } else {
108
                        // Name
109
                        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...
110
                            $xAuthor->appendChild(
111
                                new XMLElement('name', $author->getFullName())
112
                            );
113
                        }
114
115
                        // Username
116
                        if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) {
117
                            $xAuthor->appendChild(
118
                                new XMLElement('username', $author->get('username'))
119
                            );
120
                        }
121
122
                        // Email
123
                        if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) {
124
                            $xAuthor->appendChild(
125
                                new XMLElement('email', $author->get('email'))
126
                            );
127
                        }
128
129
                        // Author Token
130
                        if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) {
131
                            $xAuthor->appendChild(
132
                                new XMLElement('author-token', $author->createAuthToken())
133
                            );
134
                        }
135
136
                        // Default Area
137
                        if (in_array('default-area',
138
                                $this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area'))
139
                        ) {
140
                            // Section
141
                            if ($section = SectionManager::fetch($author->get('default_area'))) {
142
                                $default_area = new XMLElement('default-area', $section->get('name'));
143
                                $default_area->setAttributeArray(array(
144
                                    'id' => $section->get('id'),
145
                                    'handle' => $section->get('handle'),
146
                                    'type' => 'section'
147
                                ));
148
                                $xAuthor->appendChild($default_area);
149
150
                                // Pages
151
                            } else {
152
                                $default_area = new XMLElement('default-area', $author->get('default_area'));
153
                                $default_area->setAttribute('type', 'page');
154
                                $xAuthor->appendChild($default_area);
155
                            }
156
                        }
157
158
                        $result->appendChild($xAuthor);
159
                    }
160
                }
161
            }
162
163
            return $result;
164
        }
165
166
        public function __processAuthorFilter($field, $filter)
167
        {
168
            if (!is_array($filter)) {
169
                $bits = preg_split('/,\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY);
170
                $bits = array_map('trim', $bits);
171
            } else {
172
                $bits = $filter;
173
            }
174
175
            $q = Database::addPlaceholders($bits);
176
            $authors = Symphony::Database()->fetchCol('id', sprintf("
177
                SELECT `id` FROM `tbl_authors`
178
                WHERE `%s` IN (%s)",
179
                $field,
180
                $q
181
            ));
182
183
            return (is_array($authors) && !empty($authors) ? $authors : null);
184
        }
185
    }
186