Completed
Push — master ( ba4ad2...de4e13 )
by Will
02:43
created

ElementalSolrIndexer::publishDirtyClones()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
class ElementalSolrIndexer {
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...
4
5
    const ELEMENTAL_FIELD_NAME = 'Elemental_Element';
6
7
    /**
8
     *
9
     */
10
    public function updateFieldDefinition($xml) {
11
        $xml .= "\n\t\t<field name='". self::ELEMENTAL_FIELD_NAME. "_ID' type='int' indexed='true' stored='true' multiValued='true' />";
12
13
        return $xml;
14
    }
15
16
    /**
17
     *
18
     */
19
    public function elementPageChanged($object, $doc) {
20
        if($object->hasMethod('ElementArea')) {
21
            $dirty = array();
22
23
            foreach($object->ElementArea()->WidgetControllers() as $element) {
24
                $doc->addField(self::ELEMENTAL_FIELD_NAME . '_ID', $element->ID);
25
26
                // if this page has virtual clones on another page make sure that we also update the Solr index for
27
                // those pages automatically.
28
                foreach($element->VirtualClones() as $clone) {
29
                    $dirty[$clone->ID] = $clone->ID;
30
                }
31
            }
32
        }
33
34
        $this->publishDirtyClones($dirty);
0 ignored issues
show
Bug introduced by
The variable $dirty 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...
Unused Code introduced by
The call to the method ElementalSolrIndexer::publishDirtyClones() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
35
    }
36
37
    /**
38
     * @todo
39
     */
40
    public function publishDirtyClones($dirty) {
41
        foreach($dirty as $id) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
42
43
        }
44
    }
45
}
46