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 |
||
| 20 | class Value_Set { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Value type which saves a single value |
||
| 24 | */ |
||
| 25 | const TYPE_SINGLE_VALUE = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Value type which saves multiple values with a single property |
||
| 29 | */ |
||
| 30 | const TYPE_MULTIPLE_VALUES = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Value type which saves a single value with multiple proerties |
||
| 34 | */ |
||
| 35 | const TYPE_MULTIPLE_PROPERTIES = 3; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Value type which saves multiple values with multiple propertys |
||
| 39 | */ |
||
| 40 | const TYPE_VALUE_SET = 4; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default value property required for every value set |
||
| 44 | */ |
||
| 45 | const VALUE_PROPERTY = 'value'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Value set type |
||
| 49 | * |
||
| 50 | * @var integer static::TYPE_* constant |
||
| 51 | */ |
||
| 52 | protected $type = self::TYPE_SINGLE_VALUE; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Array of valid value set types |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $valid_types = array( self::TYPE_SINGLE_VALUE, self::TYPE_MULTIPLE_VALUES, self::TYPE_MULTIPLE_PROPERTIES, self::TYPE_VALUE_SET ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Registered value set properties (properties) with their default value (when the property is missing in the passed raw_value_set) |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $properties = array( self::VALUE_PROPERTY => '' ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Data the value set represents |
||
| 70 | * |
||
| 71 | * @var array Nullable array |
||
| 72 | */ |
||
| 73 | protected $value_set = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Value set constructor |
||
| 77 | * |
||
| 78 | * @param integer $type static::TYPE_* constant |
||
| 79 | * @param array $additional_properties |
||
| 80 | */ |
||
| 81 | 1 | public function __construct( $type = self::TYPE_SINGLE_VALUE, $additional_properties = array() ) { |
|
| 82 | 1 | if ( ! in_array( $type, $this->valid_types ) ) { |
|
| 83 | Incorrect_Syntax_Exception::raise( "Invalid type specified for Value_Set: $type" ); |
||
| 84 | } |
||
| 85 | |||
| 86 | 1 | $this->type = $type; |
|
| 87 | 1 | $this->properties = array_merge( $this->properties, $additional_properties ); |
|
| 88 | 1 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Format a raw value set into one which guarantees that only (and all) registered properties are present |
||
| 92 | * |
||
| 93 | * @param array $raw_value_set |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | protected function get_formatted_value_set( $raw_value_set ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return value set type |
||
| 110 | * |
||
| 111 | * @return int static::TYPE_* constant |
||
| 112 | */ |
||
| 113 | 1 | public function get_type() { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return whether this value type requires a keepalive key |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | public function keepalive() { |
||
| 123 | return ( $this->type !== static::TYPE_SINGLE_VALUE ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return whether the data is empty |
||
| 128 | * |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | public function is_empty() { |
||
| 132 | return empty( $this->value_set ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return data formatted according to the value set $type |
||
| 137 | * |
||
| 138 | * @return string|array<mixed>|array<array<mixed>> |
||
| 139 | */ |
||
| 140 | public function get() { |
||
| 141 | if ( $this->value_set === null ) { |
||
| 142 | return null; |
||
| 143 | } |
||
| 144 | $value = ''; |
||
| 145 | $value_property = static::VALUE_PROPERTY; |
||
| 146 | |||
| 147 | switch ( $this->type ) { |
||
| 148 | case static::TYPE_MULTIPLE_VALUES: |
||
| 149 | $value = array_map( function( $set ) use ( $value_property ) { |
||
| 150 | return $set[ $value_property ]; |
||
| 151 | }, $this->value_set ); |
||
| 152 | break; |
||
| 153 | View Code Duplication | case static::TYPE_MULTIPLE_PROPERTIES: |
|
| 154 | $value = array(); |
||
| 155 | if ( ! empty( $this->value_set ) ) { |
||
| 156 | $value = $this->value_set[0]; |
||
| 157 | } |
||
| 158 | break; |
||
| 159 | case static::TYPE_VALUE_SET: |
||
| 160 | $value = $this->value_set; |
||
| 161 | break; |
||
| 162 | |||
| 163 | case static::TYPE_SINGLE_VALUE: |
||
| 164 | View Code Duplication | default: |
|
| 165 | if ( ! empty( $this->value_set ) ) { |
||
| 166 | $value = $this->value_set[0][ static::VALUE_PROPERTY ]; |
||
| 167 | } |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | |||
| 171 | return $value; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return the full value set data regardless of type |
||
| 176 | * |
||
| 177 | * @return array<array<mixed>> |
||
| 178 | */ |
||
| 179 | public function get_set() { |
||
| 180 | return $this->value_set; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check if an array is flat |
||
| 185 | * |
||
| 186 | * @param array $array |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | protected function is_flat_array( $array ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Convert a flat array to a raw value set |
||
| 202 | * |
||
| 203 | * @param array $value_array |
||
| 204 | * @return array<array<mixed>> |
||
| 205 | */ |
||
| 206 | protected function flat_array_to_raw_value_set( $value_array ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the value set data |
||
| 237 | * Accepts: single value, array of values, array of key-values, array of arrays of key-values |
||
| 238 | * |
||
| 239 | * @param string|array<mixed>|array<array<mixed>> $raw_value_set |
||
| 240 | */ |
||
| 241 | public function set( $raw_value_set ) { |
||
| 257 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..