Completed
Push — master ( 149ad0...66da0f )
by
unknown
02:11
created

getManipulatedData()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
/**
3
 * This class is is responsible for adding objects to another object's has_many
4
 * and many_many relation, as defined by the {@link RelationList} passed to the
5
 * {@link GridField} constructor.
6
 *
7
 * Objects can be searched through an input field (partially matching one or
8
 * more fields).
9
 *
10
 * Selecting from the results will add the object to the relation.
11
 *
12
 * Often used alongside {@link GridFieldDeleteAction} for detaching existing
13
 * records from a relationship.
14
 *
15
 * For easier setup, have a look at a sample configuration in
16
 * {@link GridFieldConfig_RelationEditor}.
17
 *
18
 * @package forms
19
 * @subpackage fields-gridfield
20
 */
21
class ElementalGridFieldAddExistingAutocompleter extends GridFieldAddExistingAutocompleter {
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...
22
23
    /**
24
     * If an object ID is set, add the object to the list
25
     *
26
     * @param GridField $gridField
27
     * @param SS_List $dataList
28
     * @return SS_List
29
     */
30
    public function getManipulatedData(GridField $gridField, SS_List $dataList)
31
    {
32
33
        if(!$gridField->State->GridFieldAddRelation) {
34
            return $dataList;
35
        }
36
        $objectID = Convert::raw2sql($gridField->State->GridFieldAddRelation);
37
        if($objectID) {
38
            $object = DataObject::get_by_id($dataList->dataclass(), $objectID);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SS_List as the method dataclass() does only exist in the following implementations of said interface: ArrayList, DataList, FieldList, HasManyList, ManyManyList, Member_GroupSet, PolymorphicHasManyList, RelationList, UnsavedRelationList.

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...
Documentation introduced by
$objectID is of type array|string, but the function expects a integer.

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...
39
40
            if($object) {
41
                $virtual = new ElementVirtualLinked();
42
                $virtual->LinkedElementID = $object->ID;
0 ignored issues
show
Documentation introduced by
The property LinkedElementID does not exist on object<ElementVirtualLinked>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
                $virtual->write();
44
45
                $dataList->add($virtual);
46
            }
47
        }
48
49
        $gridField->State->GridFieldAddRelation = null;
50
51
        return $dataList;
52
    }
53
}
54