| Total Complexity | 40 |
| Total Lines | 359 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractParameter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractParameter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | abstract class AbstractParameter extends AbstractNamedDBElement |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses. |
||
| 56 | */ |
||
| 57 | public const ALLOWED_ELEMENT_CLASS = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short |
||
| 61 | * @Assert\Length(max=20) |
||
| 62 | * @ORM\Column(type="string", nullable=false) |
||
| 63 | */ |
||
| 64 | protected $symbol = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var float|null The guaranteed minimum value of this property. |
||
| 68 | * @Assert\Type({"float","null"}) |
||
| 69 | * @Assert\LessThanOrEqual(propertyPath="value_typical", message="parameters.validator.min_lesser_typical") |
||
| 70 | * @Assert\LessThan(propertyPath="value_max", message="parameters.validator.min_lesser_max") |
||
| 71 | * @ORM\Column(type="float", nullable=true) |
||
| 72 | */ |
||
| 73 | protected $value_min; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var float|null The typical value of this property. |
||
| 77 | * @Assert\Type({"null", "float"}) |
||
| 78 | * @ORM\Column(type="float", nullable=true) |
||
| 79 | */ |
||
| 80 | protected $value_typical; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var float|null The maximum value of this property. |
||
| 84 | * @Assert\Type({"float", "null"}) |
||
| 85 | * @Assert\GreaterThanOrEqual(propertyPath="value_typical", message="parameters.validator.max_greater_typical") |
||
| 86 | * @ORM\Column(type="float", nullable=true) |
||
| 87 | */ |
||
| 88 | protected $value_max; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string The unit in which the value values are given (e.g. V) |
||
| 92 | * @Assert\Length(max=5) |
||
| 93 | * @ORM\Column(type="string", nullable=false) |
||
| 94 | */ |
||
| 95 | protected $unit = ''; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string A text value for the given property. |
||
| 99 | * @ORM\Column(type="string", nullable=false) |
||
| 100 | */ |
||
| 101 | protected $value_text = ''; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string The group this parameter belongs to. |
||
| 105 | * @ORM\Column(type="string", nullable=false, name="param_group") |
||
| 106 | */ |
||
| 107 | protected $group = ''; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Mapping is done in sub classes. |
||
| 111 | * |
||
| 112 | * @var AbstractDBElement|null The element to which this parameter belongs to. |
||
| 113 | */ |
||
| 114 | protected $element; |
||
| 115 | |||
| 116 | public function __construct() |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public function updateTimestamps(): void |
||
| 124 | { |
||
| 125 | parent::updateTimestamps(); |
||
| 126 | if ($this->element instanceof AbstractNamedDBElement) { |
||
| 127 | $this->element->updateTimestamps(); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the element this parameter belongs to. |
||
| 133 | * |
||
| 134 | * @return AbstractDBElement|null |
||
| 135 | */ |
||
| 136 | public function getElement(): ?AbstractDBElement |
||
| 137 | { |
||
| 138 | return $this->element; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return a formatted string version of the values of the string. |
||
| 143 | * Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text]. |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getFormattedValue(): string |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Sets the element to which this parameter belongs to. |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function setElement(AbstractDBElement $element): self |
||
| 190 | { |
||
| 191 | if (! is_a($element, static::ALLOWED_ELEMENT_CLASS)) { |
||
| 192 | throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS)); |
||
| 193 | } |
||
| 194 | |||
| 195 | $this->element = $element; |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets the name of the specification. This value is required. |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function setName(string $name): AbstractNamedDBElement |
||
| 206 | { |
||
| 207 | $this->name = $name; |
||
| 208 | |||
| 209 | return $this; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the name of the group this parameter is associated to (e.g. Technical Parameters). |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getGroup(): string |
||
| 218 | { |
||
| 219 | return $this->group; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets the name of the group this parameter is associated to. |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function setGroup(string $group): self |
||
| 228 | { |
||
| 229 | $this->group = $group; |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the mathematical symbol for this specification (e.g. "V_CB"). |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getSymbol(): string |
||
| 240 | { |
||
| 241 | return $this->symbol; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Sets the mathematical symbol for this specification (e.g. "V_CB"). |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function setSymbol(string $symbol): self |
||
| 250 | { |
||
| 251 | $this->symbol = $symbol; |
||
| 252 | |||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns The guaranteed minimum value of this property. |
||
| 258 | * |
||
| 259 | * @return float|null |
||
| 260 | */ |
||
| 261 | public function getValueMin(): ?float |
||
| 262 | { |
||
| 263 | return $this->value_min; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Sets the minimum value of this property. |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function setValueMin(?float $value_min): self |
||
| 272 | { |
||
| 273 | $this->value_min = $value_min; |
||
| 274 | |||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns the typical value of this property. |
||
| 280 | * |
||
| 281 | * @return float|null |
||
| 282 | */ |
||
| 283 | public function getValueTypical(): ?float |
||
| 284 | { |
||
| 285 | return $this->value_typical; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Return a formatted version with the minimum value with the unit of this parameter. |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getValueTypicalWithUnit(): string |
||
| 294 | { |
||
| 295 | return $this->formatWithUnit($this->value_typical); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return a formatted version with the maximum value with the unit of this parameter. |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getValueMaxWithUnit(): string |
||
| 304 | { |
||
| 305 | return $this->formatWithUnit($this->value_max); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Return a formatted version with the typical value with the unit of this parameter. |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function getValueMinWithUnit(): string |
||
| 314 | { |
||
| 315 | return $this->formatWithUnit($this->value_min); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the typical value of this property. |
||
| 320 | * |
||
| 321 | * @param float $value_typical |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function setValueTypical(?float $value_typical): self |
||
| 326 | { |
||
| 327 | $this->value_typical = $value_typical; |
||
| 328 | |||
| 329 | return $this; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the guaranteed maximum value. |
||
| 334 | * |
||
| 335 | * @return float|null |
||
| 336 | */ |
||
| 337 | public function getValueMax(): ?float |
||
| 338 | { |
||
| 339 | return $this->value_max; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Sets the guaranteed maximum value. |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function setValueMax(?float $value_max): self |
||
| 348 | { |
||
| 349 | $this->value_max = $value_max; |
||
| 350 | |||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the unit used by the value (e.g. "V"). |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getUnit(): string |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Sets the unit used by the value. |
||
| 366 | * |
||
| 367 | * @return $this |
||
| 368 | */ |
||
| 369 | public function setUnit(string $unit): self |
||
| 370 | { |
||
| 371 | $this->unit = $unit; |
||
| 372 | |||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the text value. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function getValueText(): string |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Sets the text value. |
||
| 388 | * |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | public function setValueText(string $value_text): self |
||
| 392 | { |
||
| 393 | $this->value_text = $value_text; |
||
| 394 | |||
| 395 | return $this; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Return a string representation and (if possible) with its unit. |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function formatWithUnit(float $value, string $format = '%g'): string |
||
| 411 | } |
||
| 412 | } |
||
| 413 |