Completed
Pull Request — master (#5891)
by Ingo
11:10
created

LiteralField::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This field lets you put an arbitrary piece of HTML into your forms.
5
 *
6
 * <code>
7
 * new LiteralField (
8
 *    $name = "literalfield",
9
 *    $content = '<b>some bold text</b> and <a href="http://silverstripe.com">a link</a>'
10
 * )
11
 * </code>
12
 *
13
 * @package forms
14
 * @subpackage fields-dataless
15
 */
16
class LiteralField extends DatalessField {
17
18
	private static $casting = [
19
		'Value' => 'HTMLFragment',
20
	];
21
22
	/**
23
	 * @var string|FormField
24
	 */
25
	protected $content;
26
27
	protected $schemaDataType = self::SCHEMA_DATA_TYPE_STRUCTURAL;
28
29
	protected $schemaComponent = 'LiteralField';
30
31
	/**
32
	 * @param string $name
33
	 * @param string|FormField $content
34
	 */
35
	public function __construct($name, $content) {
36
		$this->setContent($content);
37
38
		parent::__construct($name);
39
	}
40
41
	/**
42
	 * @param array $properties
43
	 *
44
	 * @return string
45
	 */
46
	public function FieldHolder($properties = array()) {
47
		if($this->content instanceof ViewableData) {
48
			$context = $this->content;
49
50
			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...
51
				$context = $context->customise($properties);
52
			}
53
54
			return $context->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...
55
		}
56
57
		return $this->content;
58
	}
59
60
	/**
61
	 * @param array $properties
62
	 *
63
	 * @return string
64
	 */
65
	public function Field($properties = array()) {
66
		return $this->FieldHolder($properties);
67
	}
68
69
	/**
70
	 * Sets the content of this field to a new value.
71
	 *
72
	 * @param string|FormField $content
73
	 *
74
	 * @return $this
75
	 */
76
	public function setContent($content) {
77
		$this->content = $content;
78
79
		return $this;
80
	}
81
82
	/**
83
	 * @return string
84
	 */
85
	public function getContent() {
86
		return $this->content;
87
	}
88
89
	/**
90
	 * Synonym of {@link setContent()} so that LiteralField is more compatible with other field types.
91
	 *
92
	 * @param string|FormField $content
93
	 *
94
	 * @return $this
95
	 */
96
	public function setValue($content) {
97
		$this->setContent($content);
98
99
		return $this;
100
	}
101
102
	/**
103
	 * @return static
104
	 */
105
	public function performReadonlyTransformation() {
106
		$clone = clone $this;
107
108
		$clone->setReadonly(true);
109
110
		return $clone;
111
	}
112
113
	/**
114
	 * Header fields support dynamic titles via schema state
115
	 *
116
	 * @return array
117
	 */
118
	public function getSchemaStateDefaults() {
119
		$state = parent::getSchemaStateDefaults();
120
121
		$state['data']['content'] = $this->FieldHolder();
122
123
		return $state;
124
	}
125
}
126