Completed
Branchmaster (d2c4e1)
created

Keywords::process()   C

↳ Parent: Keywords

Complexity

Conditions 7
Paths 10

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 36
Code Lines 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 17
nc 10
nop 0
1
<?php
2
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
6
/**
7
 * helper class to provide a keywords helper
8
 *
9
 * @since 3.0.0
10
 *
11
 * @package Redaxscript
12
 * @category Template
13
 * @author Henry Ruhs
14
 */
15
16
class Keywords 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
		$useKeywords = $this->_registry->get('useKeywords');
31
		$settingKeywords = Db::getSetting('keywords');
32
33
		/* find keywords */
34
35
		if ($useKeywords)
36
		{
37
			$keywords = $useKeywords;
38
		}
39
		else if ($lastTable && $lastId)
40
		{
41
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->findOne();
42
			$keywords = $content->keywords;
43
44
			/* handle parent */
45
46
			if (!$keywords)
47
			{
48
				$parentId = $content->category ? $content->category : $content->parent;
49
				$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
50
				$keywords = $parent->keywords;
51
			}
52
		}
53
54
		/* handle keywords */
55
56
		if ($keywords)
57
		{
58
			return $keywords;
0 ignored issues
show
Bug introduced by redaxmedia
The variable $keywords 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...
59
		}
60
		return $settingKeywords;
61
	}
62
}
63