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 ( a3ab80...7a98f7 )
by Brendan
06:22
created

NavigationDatasource::execute()   C

Complexity

Conditions 9
Paths 12

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
c 2
b 0
f 0
nc 12
nop 1
dl 0
loc 46
rs 5.0942
1
<?php
2
3
    /**
4
     * @package data-sources
5
     */
6
7
    /**
8
     * The `NavigationDatasource` outputs the Symphony page structure as XML.
9
     * This datasource supports filtering to narrow down the results to only
10
     * show pages that match a particular page type, have a specific parent, etc.
11
     *
12
     * @since Symphony 2.3
13
     */
14
    class NavigationDatasource extends Datasource
15
    {
16
        public function execute(array &$param_pool = null)
17
        {
18
            $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...
19
            $type_sql = $parent_sql = null;
20
21
            if (trim($this->dsParamFILTERS['type']) !== '') {
22
                $type_sql = $this->__processNavigationTypeFilter($this->dsParamFILTERS['type'],
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...
23
                    Datasource::determineFilterType($this->dsParamFILTERS['type']));
24
            }
25
26
            if (trim($this->dsParamFILTERS['parent']) !== '') {
27
                $parent_sql = $this->__processNavigationParentFilter($this->dsParamFILTERS['parent']);
28
            }
29
30
            // Build the Query appending the Parent and/or Type WHERE clauses
31
            $pages = Symphony::Database()->fetch(sprintf(
32
                "SELECT DISTINCT p.id, p.title, p.handle, (SELECT COUNT(id) FROM `tbl_pages` WHERE parent = p.id) AS children
33
            FROM `tbl_pages` AS p
34
            LEFT JOIN `tbl_pages_types` AS pt ON (p.id = pt.page_id)
35
            WHERE 1 = 1
36
            %s
37
            %s
38
            ORDER BY p.`sortorder` ASC",
39
                // Add Parent SQL
40
                !is_null($parent_sql) ? $parent_sql : " AND p.parent IS null ",
41
                // Add Types SQL
42
                !is_null($type_sql) ? $type_sql : ""
43
            ));
44
45
            if ((!is_array($pages) || empty($pages))) {
46
                if ($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...
47
                    throw new FrontendPageNotFoundException;
48
                }
49
                $result->appendChild($this->__noRecordsFound());
50
            } else {
51
                // Build an array of all the types so that the page's don't have to do
52
                // individual lookups.
53
                $page_types = PageManager::fetchAllPagesPageTypes();
54
55
                foreach ($pages as $page) {
56
                    $result->appendChild($this->__buildPageXML($page, $page_types));
57
                }
58
            }
59
60
            return $result;
61
        }
62
63
        public function __processNavigationTypeFilter($filter, $filter_type = Datasource::FILTER_OR)
64
        {
65
            $types = preg_split('/' . ($filter_type === Datasource::FILTER_AND ? '\+' : '(?<!\\\\),') . '\s*/', $filter,
66
                -1, PREG_SPLIT_NO_EMPTY);
67
            $types = array_map('trim', $types);
68
69
            $types = array_map(array('Datasource', 'removeEscapedCommas'), $types);
70
71
            if ($filter_type === Datasource::FILTER_OR) {
72
                $type_sql = " AND pt.type IN ('" . implode("', '", $types) . "')";
73
            } else {
74
                foreach ($types as $type) {
75
                    $type_sql = " AND pt.type = '" . $type . "'";
76
                }
77
            }
78
79
            return $type_sql;
0 ignored issues
show
Bug introduced by
The variable $type_sql 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...
80
        }
81
82
        public function __processNavigationParentFilter($parent)
83
        {
84
            $parent_paths = preg_split('/,\s*/', $parent, -1, PREG_SPLIT_NO_EMPTY);
85
            $parent_paths = array_map(function ($a) {
86
                return trim($a, ' /');
87
            }, $parent_paths);
88
89
            return (is_array($parent_paths) && !empty($parent_paths) ? " AND p.`path` IN ('" . implode("', '",
90
                    $parent_paths) . "')" : null);
91
        }
92
93
        public function __buildPageXML($page, $page_types)
94
        {
95
            $oPage = new XMLElement('page');
96
            $oPage->setAttribute('handle', $page['handle']);
97
            $oPage->setAttribute('id', $page['id']);
98
            $oPage->appendChild(new XMLElement('name', General::sanitize($page['title'])));
99
100
            if (in_array($page['id'], array_keys($page_types))) {
101
                $xTypes = new XMLElement('types');
102
103
                foreach ($page_types[$page['id']] as $type) {
104
                    $xTypes->appendChild(new XMLElement('type', $type));
105
                }
106
107
                $oPage->appendChild($xTypes);
108
            }
109
110
            if ($page['children'] !== '0') {
111
                if ($children = PageManager::fetch(false, array('id, handle, title'),
112
                    array(sprintf('`parent` = %d', $page['id'])))
113
                ) {
114
                    foreach ($children as $c) {
115
                        $oPage->appendChild($this->__buildPageXML($c, $page_types));
116
                    }
117
                }
118
            }
119
120
            return $oPage;
121
        }
122
    }
123