Completed
Push — 3.1 ( 97d1e4...fde6df )
by Gordon
14:39
created

PointsOfInterestLayerExtension::updateHasGeo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
class PointsOfInterestLayerExtension extends DataExtension
4
{
5
    public static $many_many = array(
6
        'PointsOfInterestLayers' => 'PointsOfInterestLayer',
7
    );
8
9
    public static $belongs_many_many_extraFields = array(
10
        'PointsOfInterestLayers' => array(
11
            'SortOrder' => 'Int',
12
        ),
13
    );
14
15
    /**
16
     * Only set has geo to true if layers exist
17
     * @param  boolean &$hasGeo will be set to true if any layers
18
     */
19
    public function updateHasGeo(&$hasGeo) {
0 ignored issues
show
Unused Code introduced by
The parameter $hasGeo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20 2
        if ($this->owner->PointsOfInterestLayers()->count() > 0) {
21
            $result = true;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22 2
        }
23 2
    }
24 2
25 2
    /**
26 2
     * Update cms fields - add list of POIs.
27 2
     *
28 2
     * @param FieldList $fields list of existing fields on the object
29 2
     */
30
    public function updateCMSFields(FieldList $fields)
31 2
    {
32 2
        $gridConfig2 = GridFieldConfig_RelationEditor::create();
33 2
        $gridConfig2->getComponentByType(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setSearchFields() does only exist in the following implementations of said interface: GridFieldAddExistingAutocompleter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
34
            'GridFieldAddExistingAutocompleter')->
35
            setSearchFields(array('Name')
36
        );
0 ignored issues
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 12 spaces, but found 8).
Loading history...
37
        $gridConfig2->getComponentByType('GridFieldPaginator')->setItemsPerPage(100);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setItemsPerPage() does only exist in the following implementations of said interface: GridFieldPaginator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
38
        $gridField2 = new GridField('POI Layers', 'POI Layers:',
39
            $this->owner->PointsOfInterestLayers(),
40
            $gridConfig2
41
        );
42
        $fields->addFieldToTab('Root.MapLayers', $gridField2);
43
    }
44
45
    /**
46
     * Use extension point to add the markers
47
     * @param  MapAPI &$map reference to object representing the map
48
     * @param  $autozoom will be altered to true for autozoom, false not to
49
     */
50
    public function updateBasicMap(&$map, &$autozoom) {
51
        if (Object::has_extension($this->owner->ClassName, 'PointsOfInterestLayerExtension')) {
52
            foreach($this->owner->PointsOfInterestLayers() as $layer) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
53
                $layericon = $layer->DefaultIcon();
54
                if ($layericon->ID === 0) {
55
                    $layericon = null;
56
                }
57
                foreach ($layer->PointsOfInterest() as $poi) {
58
                    if ($poi->MapPinEdited) {
59
                        if ($poi->MapPinIconID == 0 && $layericon) {
60
                            $poi->CachedMapPinURL = $layericon->getAbsoluteURL();
61
                        }
62
                        $map->addMarkerAsObject($poi);
63
64
                        // we have a point of interest, so turn on auto zoom
65
                        $autozoom = true;
66
                    }
67
                }
68
            }
69
            $map->setClusterer(true);
70
        }
71
    }
72
}
73