Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
24 | class Content extends APIContent |
||
25 | { |
||
26 | /** |
||
27 | * @var mixed[][] An array of array of field values like[$fieldDefIdentifier][$languageCode] |
||
28 | */ |
||
29 | protected $fields; |
||
30 | |||
31 | /** |
||
32 | * @var \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
33 | */ |
||
34 | protected $versionInfo; |
||
35 | |||
36 | /** |
||
37 | * @var \eZ\Publish\API\Repository\Values\Content\Field[] An array of {@link Field} |
||
38 | */ |
||
39 | private $internalFields = array(); |
||
40 | |||
41 | /** |
||
42 | * The first matched field language among user provided prioritized languages. |
||
43 | * |
||
44 | * The first matched language among user provided prioritized languages on object retrieval, or null if none |
||
45 | * provided (all languages) or on main fallback. |
||
46 | * |
||
47 | * @internal |
||
48 | * @var string|null |
||
49 | */ |
||
50 | protected $prioritizedFieldLanguageCode; |
||
51 | |||
52 | View Code Duplication | public function __construct(array $data = array()) |
|
53 | { |
||
54 | foreach ($data as $propertyName => $propertyValue) { |
||
55 | $this->$propertyName = $propertyValue; |
||
56 | } |
||
57 | foreach ($this->internalFields as $field) { |
||
58 | $this->fields[$field->fieldDefIdentifier][$field->languageCode] = $field->value; |
||
|
|||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function getVersionInfo() |
||
66 | { |
||
67 | return $this->versionInfo; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | View Code Duplication | public function getFieldValue($fieldDefIdentifier, $languageCode = null) |
|
74 | { |
||
75 | if (null === $languageCode) { |
||
76 | $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode; |
||
77 | } |
||
78 | |||
79 | if (isset($this->fields[$fieldDefIdentifier][$languageCode])) { |
||
80 | return $this->fields[$fieldDefIdentifier][$languageCode]; |
||
81 | } |
||
82 | |||
83 | return null; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function getFields() |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | View Code Duplication | public function getFieldsByLanguage($languageCode = null) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getField($fieldDefIdentifier, $languageCode = null) |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | protected function getProperties($dynamicProperties = array('id', 'contentInfo')) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function __get($property) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function __isset($property) |
||
173 | } |
||
174 |
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.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.