MessageBoxField::addCSSClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * This field lets you put an arbitrary message box into your backend.
4
 *
5
 * <code>
6
 * MessageBoxField::create(
7
 *    $name = "yourmessageboxfield",
8
 *    $content = 'your message'
9
 * )->addCSSClass('notice');
10
 * </code>
11
 */
12
class MessageBoxField extends LiteralField
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...
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $classes = 'message';
18
19
    /**
20
     * @var string
21
     */
22
    public function addCSSClass($CSSClass)
23
    {
24
        $this->classes .= ' '.(string) $CSSClass;
25
26
        return $this;
27
    }
28
29
    /**
30
     * adjusts the return to include the required classes.
31
     *
32
     * @param array $properties
33
     *
34
     * @return string
35
     */
36
    public function FieldHolder($properties = array())
37
    {
38
        $content = $this->content;
39
40
        if ($content instanceof ViewableData) {
41
            if ($properties) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $properties of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
                $content = $content->customise($properties);
43
            }
44
45
            $content = $content->forTemplate();
0 ignored issues
show
Bug introduced by
The method forTemplate does only exist in FormField, but not in ViewableData_Customised.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
        }
47
48
        return '<p class="'.$this->classes.'"" name="'.$this->getName().'">'.$content.'</p>';
49
    }
50
}
51