Completed
Branchmaster (d2c4e1)
created

Title::process()   B

↳ Parent: Title

Complexity

Conditions 6
Paths 8

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 32
Code Lines 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
rs 8.439
c 1
b 0
f 0
cc 6
eloc 16
nc 8
nop 0
1
<?php
2
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
6
/**
7
 * helper class to provide a title helper
8
 *
9
 * @since 3.0.0
10
 *
11
 * @package Redaxscript
12
 * @category Template
13
 * @author Henry Ruhs
14
 */
15
16
class Title extends HelperAbstract
17
{
18
	/**
19
	 * process
20
	 *
21
	 * @since 3.0.0
22
	 *
23
	 * @return string
24
	 */
25
26
	public function process()
27
	{
28
		$lastTable = $this->_registry->get('lastTable');
29
		$lastId = $this->_registry->get('lastId');
30
		$useTitle = $this->_registry->get('useTitle');
31
		$settingDivider = Db::getSetting('divider');
32
		$settingTitle = Db::getSetting('title');
33
34
		/* find title */
35
36
		if ($useTitle)
37
		{
38
			$title = $useTitle;
39
		}
40
		else if ($lastTable && $lastId)
41
		{
42
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->findOne();
43
			$title = $content->title;
44
			if ($title)
45
			{
46
				$title .= $settingDivider . $settingTitle;
47
			}
48
		}
49
50
		/* handle title */
51
52
		if ($title)
53
		{
54
			return $title;
0 ignored issues
show
Bug introduced by redaxmedia
The variable $title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
		}
56
		return $settingTitle;
57
	}
58
}
59