Completed
Branch master (d2c4e1)
by
unknown
09:55 queued 07:09
created

Robots::process()   C

Complexity

Conditions 8
Paths 12

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 41
Code Lines 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 41
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 20
nc 12
nop 0
1
<?php
2
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
6
/**
7
 * helper class to provide a robots helper
8
 *
9
 * @since 3.0.0
10
 *
11
 * @package Redaxscript
12
 * @category Template
13
 * @author Henry Ruhs
14
 */
15
16
class Robots extends HelperAbstract
17
{
18
	/**
19
	 * array of robots
20
	 *
21
	 * @var array
22
	 */
23
24
	protected static $_robotArray =
25
	[
26
		'none',
27
		'all',
28
		'index',
29
		'follow',
30
		'index_no',
31
		'follow_no'
32
	];
33
34
	/**
35
	 * process
36
	 *
37
	 * @since 3.0.0
38
	 *
39
	 * @return string
40
	 */
41
42
	public function process()
43
	{
44
		$lastTable = $this->_registry->get('lastTable');
45
		$lastId = $this->_registry->get('lastId');
46
		$contentError = $this->_registry->get('contentError');
47
		$useRobots = $this->_registry->get('useRobots');
48
		$settingRobots = Db::getSetting('robots');
49
50
		/* find robots */
51
52
		if ($useRobots)
53
		{
54
			$robots = $useRobots;
55
		}
56
		else if ($contentError)
57
		{
58
			$robots = 0;
59
		}
60
		else if ($lastTable && $lastId)
61
		{
62
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->whereNull('access')->findOne();
63
			$robots = $content->robots;
64
65
			/* handle parent */
66
67
			if (!$robots)
68
			{
69
				$parentId = $content->category ? $content->category : $content->parent;
70
				$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
71
				$robots = $parent->robots;
72
			}
73
		}
74
75
		/* handle robots */
76
77
		if (array_key_exists($robots, self::$_robotArray))
78
		{
79
			return self::$_robotArray[$robots];
0 ignored issues
show
Bug introduced by redaxmedia
The variable $robots 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...
80
		}
81
		return self::$_robotArray[$settingRobots];
82
	}
83
}
84