Passed
Push — developer ( e5c82c...bcac4b )
by Mariusz
32:34
created

Action   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 85
Duplicated Lines 7.06 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 1
dl 6
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getListViewActions() 0 9 1
C getButton() 6 32 12
A getDropdownButton() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Layout action file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App\Layout;
13
14
/**
15
 * Layout action class.
16
 */
17
class Action
0 ignored issues
show
Coding Style introduced by
Action does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
{
19
	/**
20
	 * Get list view actions html.
21
	 *
22
	 * @param array $links
23
	 *
24
	 * @return string
25
	 */
26
	public static function getListViewActions(array $links): string
27
	{
28
		return '<div class="actions p-1">' . self::getDropdownButton([
29
			'label' => 'actions',
30
			'icon' => 'fas fa-wrench',
31
			'class' => 'btn-sm btn-light',
32
			'items' => $links
33
		]) . '</div>';
34
	}
35
36
	/**
37
	 * Get button html.
38
	 *
39
	 * @param array $link
40
	 *
41
	 * @return string
42
	 */
43
	public static function getButton(array $link): string
44
	{
45
		$showLabel = $link['showLabel'] ?? false;
46
		$btn = '<div class="c-btn-link btn-group">';
47
		if (isset($link['href'])) {
48
			$btn .= "<a role=\"button\" href=\"{$link['href']}\" ";
49
		} else {
50
			$btn .= '<button type="button" ';
51
		}
52
		$class = 'btn ';
53
		if (isset($link['class'])) {
54
			$class .= $link['class'];
55
		}
56
		$btn .= "class=\"$class\" ";
57
		if (isset($link['data']) && \is_array($link['data'])) {
58
			foreach ($link['data'] as $key => $value) {
59
				$btn .= "data-{$key}=\"{$value}\" ";
60
			}
61
		}
62 View Code Duplication
		if (!$showLabel && isset($link['label'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
			$btn .= 'data-placement="top" data-target="focus hover" data-content="' . \App\Language::translate($link['label'], $link['moduleName']) . '" ';
64
		}
65
		$btn .= '>';
66
		if (isset($link['icon'])) {
67
			$btn .= "<span class=\"{$link['icon']}\"></span>";
68
		}
69 View Code Duplication
		if ($showLabel && isset($link['label'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
			$btn .= \App\Language::translate($link['label'], $link['moduleName']);
71
		}
72
		$btn .= isset($link['href']) ? '</a>' : '</button>';
73
		return $btn . '</div>';
74
	}
75
76
	/**
77
	 * Get dropdown button html.
78
	 *
79
	 * @param array $link
80
	 *
81
	 * @return string
82
	 */
83
	public static function getDropdownButton(array $link): string
84
	{
85
		$html = '<div class="dropdown"><button type="button"';
86
		$class = 'dropdown-toggle btn ';
87
		if (isset($link['class'])) {
88
			$class .= $link['class'];
89
		}
90
		$html .= "class=\"$class\" data-toggle=\"dropdown\" aria-haspopup=\"true\">";
91
		if (isset($link['icon'])) {
92
			$html .= "<span class=\"{$link['icon']}\"></span>";
93
		}
94
		$html .= '</button><div class="dropdown-menu">';
95
		foreach ($link['items'] as $item) {
96
			$html .= self::getButton($item);
97
		}
98
		$html .= '</div></div>';
99
		return $html;
100
	}
101
}
102