Passed
Push — 5.x ( 39b1e8...2ac3d7 )
by Jeroen
15:45 queued 13s
created

Breadcrumbs::addHomeItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0017

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 26
ccs 16
cts 17
cp 0.9412
crap 3.0017
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Menus;
4
5
use Elgg\Menu\PreparedMenu;
6
7
/**
8
 * Prepares breadcrumbs
9
 *
10
 * @since 5.0
11
 */
12
class Breadcrumbs {
13
	
14
	/**
15
	 * Prepare breadcrumbs before display. This turns titles into 100-character excerpts, and also
16
	 * removes the last crumb if it's not a link.
17
	 *
18
	 * @param \Elgg\Event $event 'prepare', 'menu:breadcrumbs'
19
	 *
20
	 * @return void
21
	 */
22 45
	public static function cleanupBreadcrumbs(\Elgg\Event $event): void {
23
		/* @var $breadcrumbs PreparedMenu */
24 45
		$breadcrumbs = $event->getValue();
25
		
26 45
		$items = $breadcrumbs->getItems('default');
27 45
		if (empty($items)) {
28 9
			return;
29
		}
30
		
31 36
		$last = null;
32 36
		foreach ($items as $crumb) {
33 36
			$last = $crumb;
34 36
			$crumb->setText(elgg_get_excerpt((string) $crumb->getText(), 100));
35
		}
36
		
37
		// remove last crumb if it has no link
38 36
		if (empty($last->getHref())) {
1 ignored issue
show
Bug introduced by
The method getHref() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
		if (empty($last->/** @scrutinizer ignore-call */ getHref())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39 1
			elgg_log("Having a breadcrumb at the end of the list without a link makes no sense. Please update your code for the '{$last->getText()}[{$last->getID()}]' breadcrumb.", 'NOTICE');
40 1
			$breadcrumbs->getSection('default')->remove($last->getID());
41 35
		} elseif (!$last->getChildren() && elgg_http_url_is_identical(elgg_get_current_url(), $last->getHref())) {
42
			elgg_log("Having a breadcrumb at the end of the list which links to the current page makes no sense. Please update your code for the '{$last->getText()}[{$last->getID()}]' breadcrumb.", 'NOTICE');
43
			$breadcrumbs->getSection('default')->remove($last->getID());
44
		}
45
	}
46
	
47
	/**
48
	 * Adds a home item
49
	 *
50
	 * @param \Elgg\Event $event 'prepare', 'menu:breadcrumbs'
51
	 *
52
	 * @return null|PreparedMenu
53
	 */
54 43
	public static function addHomeItem(\Elgg\Event $event): ?PreparedMenu {
55
		/* @var $return PreparedMenu */
56 43
		$return = $event->getValue();
57
		
58
		/* @var $items \ElggMenuItem[] */
59 43
		$items = $return->getItems('default');
60 43
		if (empty($items)) {
61 9
			return null;
62
		}
63
		
64 34
		$href = elgg_get_site_url();
65 34
		if (elgg_in_context('admin')) {
66
			$href = elgg_generate_url('admin');
67
		}
68
		
69 34
		array_unshift($items, \ElggMenuItem::factory([
70 34
			'name' => 'home',
71 34
			'icon' => 'home',
72 34
			'text' => false,
73 34
			'title' => elgg_get_site_entity()->getDisplayName(),
74 34
			'href' => $href,
75 34
		]));
76
		
77 34
		$return->getSection('default')->fill($items);
78
		
79 34
		return $return;
80
	}
81
}
82