TagCloud::addCloudTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/*
3
*/
4
5
class TagCloud extends ViewableData
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    public static $tagCloudItems = array();
8
9
    public static $elementID = "flashcontent";
10
11
    public static function addCloudTag($itemName, $style)
12
    {
13
        $tagCloudItems[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tagCloudItems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tagCloudItems = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
14
            "itemName" => $itemName,
15
            "style" => $style
16
        );
17
    }
18
19
    public function init()
20
    {
21
        foreach (self::$tagCloudItems as $itemArray) {
22
            $cloudString = '<a style=\''.$itemArray["style"].'\'>'.$itemArray["ItemName"].'</a>';
23
        }
24
        Requirements::javascript("flash/javascript/swfobjectOlder.js");
25
        $customJS = '
26
			var so = new SWFObject("themes/massolution/images/tagcloud.swf", "tagcloud", "400", "300", "7", "");
27
			so.addParam("wmode", "transparent");
28
			so.addVariable("tcolor", "0xffffff");
29
			so.addVariable("tcolor2", "0xacacac");
30
			so.addVariable("mode", "tags");
31
			so.addVariable("distr", "true");
32
			so.addVariable("tspeed", "100");
33
			so.addVariable("tagcloud", "<tags>'.$cloudString.'</tags>");
0 ignored issues
show
Bug introduced by
The variable $cloudString 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...
34
			so.write("'.self::$elementID.'");';
35
        Requirements::customScript($customJS);
0 ignored issues
show
Documentation introduced by
$customJS is of type string, but the function expects a object<The>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        $doSet = new DataObjectSet();
37
        return $doSet;
38
    }
39
}
40