Title::process()   F
last analyzed

Complexity

Conditions 20
Paths 42

Size

Total Lines 101

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 18
cts 18
cp 1
rs 3.3333
c 0
b 0
f 0
cc 20
nc 42
nop 0
crap 20

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
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
7
/**
8
 * helper class to provide a title helper
9
 *
10
 * @since 3.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Template
14
 * @author Henry Ruhs
15
 */
16
17
class Title extends HelperAbstract
18
{
19
	/**
20
	 * process
21
	 *
22
	 * @since 3.0.0
23
	 *
24
	 * @return string|null
25
	 */
26
27 8
	public function process() : ?string
28
	{
29 8
		$settingModel = new Model\Setting();
30 8
		$firstParameter = $this->_registry->get('firstParameter');
31 8
		$secondParameter = $this->_registry->get('secondParameter');
32 8
		$adminParameter = $this->_registry->get('adminParameter');
33 8
		$tableParameter = $this->_registry->get('tableParameter');
34 8
		$lastTable = $this->_registry->get('lastTable');
35 8
		$lastId = $this->_registry->get('lastId');
36
		$useTitle = $this->_registry->get('useTitle');
37
		$settingDivider = $settingModel->get('divider');
38
		$settingTitle = $settingModel->get('title');
39 8
		$title = null;
40
41 1
		/* find title */
42
43 7
		if ($useTitle)
44
		{
45 5
			$title = $useTitle;
46 5
		}
47 5
48
		/* handle admin */
49 5
50
		else if ($firstParameter === 'admin')
51
		{
52
			$title = $this->_language->get('administration');
53
			if ($adminParameter && $this->_language->get($adminParameter))
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 32 can also be of type array; however, Redaxscript\Registry::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
			{
55 8
				$title .= $settingDivider . $this->_language->get($adminParameter);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 32 can also be of type array; however, Redaxscript\Registry::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
				if ($tableParameter && $this->_language->get($tableParameter))
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 33 can also be of type array; however, Redaxscript\Registry::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57 6
				{
58
					$title .= $settingDivider . $this->_language->get($tableParameter);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 33 can also be of type array; however, Redaxscript\Registry::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59 2
				}
60
			}
61
		}
62
63
		/* handle login */
64
65
		else if ($firstParameter === 'login')
66
		{
67
			if ($secondParameter === 'recover')
68
			{
69
				$title = $this->_language->get('recovery');
70
			}
71
			else if ($secondParameter === 'reset')
72
			{
73
				$title = $this->_language->get('reset');
74
			}
75
			else
76
			{
77
				$title = $this->_language->get('login');
78
			}
79
		}
80
81
		/* handle logout */
82
83
		else if ($firstParameter === 'logout')
84
		{
85
			$title = $this->_language->get('logout');
86
		}
87
88
		/* handle register */
89
90
		else if ($firstParameter === 'register')
91
		{
92
			$title = $this->_language->get('registration');
93
		}
94
95
		/* handle module */
96
97
		else if ($firstParameter === 'module')
98
		{
99
			$title = $this->_language->get('module');
100
		}
101
102
		/* handle search */
103
104
		else if ($firstParameter === 'search')
105
		{
106
			$title = $this->_language->get('search');
107
		}
108
109
		/* handle error */
110
111
		else if (!$lastId)
112
		{
113
			$title = $this->_language->get('error');
114
		}
115
		else if ($lastTable && $lastId)
116
		{
117
			$title = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->findOne()->title;
118
		}
119
120
		/* handle title */
121
122
		if ($title && $settingTitle)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $settingTitle 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 ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
123
		{
124
			return $title . $settingDivider . $settingTitle;
125
		}
126
		return $title ? : $settingTitle;
127
	}
128
}
129