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

elgg_push_collection_breadcrumbs()   C

Complexity

Conditions 16
Paths 98

Size

Total Lines 61
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 17.2733

Importance

Changes 0
Metric Value
cc 16
eloc 41
nc 98
nop 4
dl 0
loc 61
ccs 34
cts 41
cp 0.8293
crap 17.2733
rs 5.5666
c 0
b 0
f 0

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
 * Breadcrumbs
4
 */
5
6
/**
7
 * Adds a breadcrumb to the breadcrumbs stack.
8
 *
9
 * @param string       $text The title to display. During rendering this is HTML encoded.
10
 * @param false|string $href Optional. The href for the title. During rendering links are
11
 *                           normalized via elgg_normalize_url().
12
 *
13
 * @return void
14
 * @since 1.8.0
15
 */
16
function elgg_push_breadcrumb(string $text, string|false $href = false): void {
17 4
	static $i = 0;
18 4
	$i++;
19 4
	elgg_register_menu_item('breadcrumbs', [
20 4
		'name' => "breadcrumb-{$i}",
21 4
		'text' => $text,
22 4
		'href' => $href,
23 4
	]);
24
}
25
26
/**
27
 * Resolves and pushes entity breadcrumbs based on named routes
28
 *
29
 * @param \ElggEntity $entity    Entity
30
 * @param bool        $link_self (deprecated) Add a link to the entity
31
 *
32
 * @return void
33
 */
34
function elgg_push_entity_breadcrumbs(\ElggEntity $entity, bool $link_self = null): void {
35
36 8
	elgg_push_collection_breadcrumbs($entity->type, $entity->subtype, $entity->getContainerEntity());
37
38 8
	if (isset($link_self)) {
39
		elgg_deprecated_notice('Using link_self argument is deprecated. A link to self will always be added if not on the "view" route of the entity.', '5.1');
40
	} else {
41 8
		$link_self = elgg_get_current_route_name() !== "view:{$entity->type}:{$entity->subtype}";
42
	}
43
	
44 8
	if ($link_self) {
45 4
		elgg_register_menu_item('breadcrumbs', [
46 4
			'name' => 'entity',
47 4
			'text' => $entity->getDisplayName(),
48 4
			'href' => $entity->getURL(),
49 4
		]);
50
	}
51
}
52
53
/**
54
 * Resolves and pushes collection breadcrumbs for a container
55
 *
56
 * @param string          $entity_type    Entity type in the collection
57
 * @param string          $entity_subtype Entity subtype in the collection
58
 * @param ElggEntity|null $container      Container/page owner entity
59
 * @param bool            $friends        Collection belongs to container's friends?
60
 *
61
 * @return void
62
 */
63
function elgg_push_collection_breadcrumbs(string $entity_type, string $entity_subtype, \ElggEntity $container = null, bool $friends = false): void {
64
	
65 39
	if ($container) {
66 29
		if (!$container instanceof \ElggSite && $entity_type !== 'group') {
67 29
			elgg_register_menu_item('breadcrumbs', [
68 29
				'name' => 'container',
69 29
				'text' => $container->getDisplayName(),
70 29
				'href' => $container->getURL(),
71 29
			]);
72
		}
73
74 29
		if ($friends) {
75 4
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:friends";
76 25
		} elseif ($entity_type === 'group') {
77
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:all";
78 25
		} elseif ($container instanceof \ElggUser) {
79 20
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:owner";
80 5
		} elseif ($container instanceof \ElggGroup) {
81 5
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:group";
82
		} elseif ($container instanceof \ElggSite) {
83
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:all";
84
		} else {
85
			$collection_route = "collection:{$entity_type}:{$entity_subtype}:container";
86
		}
87
		
88 29
		if ($collection_route === elgg_get_current_route_name()) {
89 14
			return;
90
		}
91
92 15
		$parameters = _elgg_services()->routes->resolveRouteParameters($collection_route, $container);
93 15
		if ($parameters !== false) {
94 15
			$label = elgg_echo("collection:{$entity_type}:{$entity_subtype}");
95 15
			if ($friends) {
96
				if (elgg_language_key_exists("collection:{$entity_type}:{$entity_subtype}:friends")) {
97
					$label = elgg_echo("collection:{$entity_type}:{$entity_subtype}:friends");
98
				} else {
99
					$label = elgg_echo('collection:friends', [$label]);
100
				}
101
			}
102
			
103 15
			if (elgg_route_exists($collection_route)) {
104 15
				elgg_register_menu_item('breadcrumbs', [
105 15
					'name' => 'collection',
106 15
					'text' => $label,
107 15
					'href' => elgg_generate_url($collection_route, $parameters),
108 15
				]);
109
			}
110
		}
111
		
112 15
		return;
113
	}
114
115 10
	$all_route_name = "collection:{$entity_type}:{$entity_subtype}:all";
116 10
	if (!elgg_route_exists($all_route_name) || ($all_route_name === elgg_get_current_route_name())) {
117 5
		return;
118
	}
119
	
120 5
	elgg_register_menu_item('breadcrumbs', [
121 5
		'name' => 'collection',
122 5
		'text' => elgg_echo("collection:{$entity_type}:{$entity_subtype}"),
123 5
		'href' => elgg_generate_url($all_route_name),
124 5
	]);
125
}
126