Completed
Push — master ( 402a90...134b49 )
by Henry
16:11
created

includes/Template/Helper/Description.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
7
/**
8
 * helper class to provide a description helper
9
 *
10
 * @since 3.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Template
14
 * @author Henry Ruhs
15
 */
16
17
class Description 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
		$lastTable = $this->_registry->get('lastTable');
31 8
		$lastId = $this->_registry->get('lastId');
32 8
		$useDescription = $this->_registry->get('useDescription');
33 8
		$settingDescription = $settingModel->get('description');
34
35
		/* find description */
36
37 8
		if ($useDescription)
38
		{
39 1
			$description = $useDescription;
40
		}
41 7
		else if ($lastTable && $lastId)
42
		{
43 5
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->whereNull('access')->findOne();
44 5
			$description = $content->description;
45
46
			/* handle parent */
47
48 5
			if (!$description)
49
			{
50 3
				$parentId = $content->category ? : $content->parent;
51 3
				if ($parentId)
52
				{
53 2
					$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
54 2
					$description = $parent->description;
55
				}
56
			}
57
		}
58
59
		/* handle description */
60
61 8
		if ($description)
62
		{
63 5
			return $description;
0 ignored issues
show
The variable $description 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...
64
		}
65 3
		return $settingDescription;
66
	}
67
}
68