anrw /
classy
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Class for handling menu functionality. |
||
| 4 | * |
||
| 5 | * @package Classy |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Classy; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class Menu. |
||
| 12 | */ |
||
| 13 | class Menu { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Menu ID. |
||
| 17 | * |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | public $ID; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Menu title. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $title; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Items. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $items; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Main constructor. Tries to find menu id based on provided arg (or not) and inits menu. |
||
| 38 | * |
||
| 39 | * @param string $arg It can be menu id, slug or full name. |
||
| 40 | */ |
||
| 41 | public function __construct( $arg = null ) { |
||
| 42 | $locations = get_nav_menu_locations(); |
||
| 43 | |||
| 44 | if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) { |
||
| 45 | $menu_id = $this->check_menu_id( $arg ); |
||
| 46 | } elseif ( is_string( $arg ) ) { |
||
| 47 | $menu_id = $this->get_menu_id_by_name( $arg ); |
||
| 48 | } elseif ( is_array( $locations ) && count( $locations ) ) { |
||
| 49 | $menu_id = $this->get_locations_menu_id( $locations, $arg ); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ( ! isset( $menu_id ) ) { |
||
| 53 | $menu_id = $this->get_first_menu_id(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if ( $menu_id ) { |
||
| 57 | $this->ID = $menu_id; |
||
|
0 ignored issues
–
show
|
|||
| 58 | $this->title = $this->get_menu_name_by_id(); |
||
|
0 ignored issues
–
show
It seems like
$this->get_menu_name_by_id() of type integer or boolean is incompatible with the declared type string of property $title.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 59 | $this->init(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Init menu. |
||
| 65 | */ |
||
| 66 | protected function init() { |
||
| 67 | $_return = array(); |
||
| 68 | $items = wp_get_nav_menu_items( $this->ID ); |
||
| 69 | |||
| 70 | foreach ( $items as $item ) { |
||
| 71 | $_return[ $item->ID ] = new Menu_Item( $item ); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Apply nesting. |
||
| 75 | foreach ( $_return as $item_id => $item ) { |
||
| 76 | if ( |
||
| 77 | isset( $item->menu_item_parent ) && |
||
| 78 | $item->menu_item_parent && |
||
| 79 | isset( $_return[ $item->menu_item_parent ] ) |
||
| 80 | ) { |
||
| 81 | $_return[ $item->menu_item_parent ]->add_child( $item ); |
||
| 82 | unset( $_return[ $item_id ] ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->items = $_return; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns first menu id or false if there are no menus. |
||
| 91 | * |
||
| 92 | * @return int|bool |
||
| 93 | */ |
||
| 94 | protected function get_first_menu_id() { |
||
| 95 | $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
||
| 96 | |||
| 97 | if ( is_array( $menus ) && count( $menus ) ) { |
||
| 98 | if ( isset( $menus[0]->term_id ) ) { |
||
| 99 | return $menus[0]->term_id; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Performs menu id lookup inside navigation Menus. |
||
| 108 | * |
||
| 109 | * @param array $locations locations that are returned by get_nav_menu_locations(). |
||
| 110 | * @param int|string $arg navigation id. |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | protected function get_locations_menu_id( $locations, $arg ) { |
||
| 114 | if ( is_numeric( $arg ) ) { |
||
| 115 | $arg = array_search( $arg, $locations ); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( isset( $locations[ $arg ] ) ) { |
||
| 119 | $menu_id = $locations[ $arg ]; |
||
| 120 | |||
| 121 | return $menu_id; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Checks if the provided menu id exists. |
||
| 127 | * |
||
| 128 | * @param int $menu_id Menu ID. |
||
| 129 | * |
||
| 130 | * @return int|boolean |
||
| 131 | */ |
||
| 132 | protected function check_menu_id( $menu_id ) { |
||
| 133 | $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
||
| 134 | |||
| 135 | if ( is_array( $menus ) && count( $menus ) ) { |
||
| 136 | foreach ( $menus as $menu ) { |
||
| 137 | if ( absint( $menu->term_id ) === absint( $menu_id ) ) { |
||
| 138 | return $menu_id; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns menu id by menu name/slug. |
||
| 148 | * |
||
| 149 | * @param string $slug Menu's name. |
||
| 150 | * |
||
| 151 | * @return int|bool |
||
| 152 | */ |
||
| 153 | protected function get_menu_id_by_name( $slug = null ) { |
||
| 154 | if ( $slug && is_string( $slug ) ) { |
||
|
0 ignored issues
–
show
The expression
$slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
| 155 | if ( $menu_id = get_term_by( 'slug', $slug, 'nav_menu' ) ) { |
||
| 156 | return $menu_id; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ( $menu_id = get_term_by( 'name', $slug, 'nav_menu' ) ) { |
||
| 160 | return $menu_id; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns menu name by menu id. |
||
| 169 | * |
||
| 170 | * @return int|bool |
||
| 171 | */ |
||
| 172 | protected function get_menu_name_by_id() { |
||
| 173 | $menu_obj = wp_get_nav_menu_object( $this->ID ); |
||
| 174 | |||
| 175 | if ( $menu_obj ) { |
||
| 176 | return $menu_obj->name; |
||
| 177 | } |
||
| 178 | |||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns menu items. |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function get_items() { |
||
| 189 | return $this->items; |
||
| 190 | } |
||
| 191 | } |
||
| 192 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.