| Total Complexity | 40 |
| Total Lines | 332 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 76 | abstract class AbstractParameter extends AbstractNamedDBElement |
||
| 77 | { |
||
| 78 | /** |
||
| 79 | * @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses. |
||
| 80 | */ |
||
| 81 | public const ALLOWED_ELEMENT_CLASS = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short |
||
| 85 | * @Assert\Length(max=20) |
||
| 86 | * @ORM\Column(type="string", nullable=false) |
||
| 87 | */ |
||
| 88 | protected string $symbol = ''; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var float|null the guaranteed minimum value of this property |
||
| 92 | * @Assert\Type({"float","null"}) |
||
| 93 | * @Assert\LessThanOrEqual(propertyPath="value_typical", message="parameters.validator.min_lesser_typical") |
||
| 94 | * @Assert\LessThan(propertyPath="value_max", message="parameters.validator.min_lesser_max") |
||
| 95 | * @ORM\Column(type="float", nullable=true) |
||
| 96 | */ |
||
| 97 | protected ?float $value_min = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var float|null the typical value of this property |
||
| 101 | * @Assert\Type({"null", "float"}) |
||
| 102 | * @ORM\Column(type="float", nullable=true) |
||
| 103 | */ |
||
| 104 | protected ?float $value_typical = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var float|null the maximum value of this property |
||
| 108 | * @Assert\Type({"float", "null"}) |
||
| 109 | * @Assert\GreaterThanOrEqual(propertyPath="value_typical", message="parameters.validator.max_greater_typical") |
||
| 110 | * @ORM\Column(type="float", nullable=true) |
||
| 111 | */ |
||
| 112 | protected ?float $value_max = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string The unit in which the value values are given (e.g. V) |
||
| 116 | * @ORM\Column(type="string", nullable=false) |
||
| 117 | */ |
||
| 118 | protected string $unit = ''; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var string a text value for the given property |
||
| 122 | * @ORM\Column(type="string", nullable=false) |
||
| 123 | */ |
||
| 124 | protected string $value_text = ''; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string the group this parameter belongs to |
||
| 128 | * @ORM\Column(type="string", nullable=false, name="param_group") |
||
| 129 | */ |
||
| 130 | protected string $group = ''; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Mapping is done in sub classes. |
||
| 134 | * |
||
| 135 | * @var AbstractDBElement|null the element to which this parameter belongs to |
||
| 136 | */ |
||
| 137 | protected $element; |
||
| 138 | |||
| 139 | public function __construct() |
||
| 140 | { |
||
| 141 | if ('' === static::ALLOWED_ELEMENT_CLASS) { |
||
|
|
|||
| 142 | throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!'); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | public function updateTimestamps(): void |
||
| 147 | { |
||
| 148 | parent::updateTimestamps(); |
||
| 149 | if ($this->element instanceof AbstractNamedDBElement) { |
||
| 150 | $this->element->updateTimestamps(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the element this parameter belongs to. |
||
| 156 | */ |
||
| 157 | public function getElement(): ?AbstractDBElement |
||
| 158 | { |
||
| 159 | return $this->element; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return a formatted string version of the values of the string. |
||
| 164 | * Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text]. |
||
| 165 | */ |
||
| 166 | public function getFormattedValue(): string |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets the element to which this parameter belongs to. |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setElement(AbstractDBElement $element): self |
||
| 209 | { |
||
| 210 | if (!is_a($element, static::ALLOWED_ELEMENT_CLASS)) { |
||
| 211 | throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS)); |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->element = $element; |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Sets the name of the specification. This value is required. |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function setName(string $name): AbstractNamedDBElement |
||
| 225 | { |
||
| 226 | $this->name = $name; |
||
| 227 | |||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the name of the group this parameter is associated to (e.g. Technical Parameters). |
||
| 233 | */ |
||
| 234 | public function getGroup(): string |
||
| 235 | { |
||
| 236 | return $this->group; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets the name of the group this parameter is associated to. |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setGroup(string $group): self |
||
| 245 | { |
||
| 246 | $this->group = $group; |
||
| 247 | |||
| 248 | return $this; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the mathematical symbol for this specification (e.g. "V_CB"). |
||
| 253 | */ |
||
| 254 | public function getSymbol(): string |
||
| 255 | { |
||
| 256 | return $this->symbol; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the mathematical symbol for this specification (e.g. "V_CB"). |
||
| 261 | * |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function setSymbol(string $symbol): self |
||
| 265 | { |
||
| 266 | $this->symbol = $symbol; |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns The guaranteed minimum value of this property. |
||
| 273 | */ |
||
| 274 | public function getValueMin(): ?float |
||
| 275 | { |
||
| 276 | return $this->value_min; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Sets the minimum value of this property. |
||
| 281 | * |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function setValueMin(?float $value_min): self |
||
| 285 | { |
||
| 286 | $this->value_min = $value_min; |
||
| 287 | |||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the typical value of this property. |
||
| 293 | */ |
||
| 294 | public function getValueTypical(): ?float |
||
| 295 | { |
||
| 296 | return $this->value_typical; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return a formatted version with the minimum value with the unit of this parameter. |
||
| 301 | */ |
||
| 302 | public function getValueTypicalWithUnit(): string |
||
| 303 | { |
||
| 304 | return $this->formatWithUnit($this->value_typical); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return a formatted version with the maximum value with the unit of this parameter. |
||
| 309 | */ |
||
| 310 | public function getValueMaxWithUnit(): string |
||
| 311 | { |
||
| 312 | return $this->formatWithUnit($this->value_max); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return a formatted version with the typical value with the unit of this parameter. |
||
| 317 | */ |
||
| 318 | public function getValueMinWithUnit(): string |
||
| 319 | { |
||
| 320 | return $this->formatWithUnit($this->value_min); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sets the typical value of this property. |
||
| 325 | * |
||
| 326 | * @param float|null $value_typical |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function setValueTypical(?float $value_typical): self |
||
| 331 | { |
||
| 332 | $this->value_typical = $value_typical; |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns the guaranteed maximum value. |
||
| 339 | */ |
||
| 340 | public function getValueMax(): ?float |
||
| 341 | { |
||
| 342 | return $this->value_max; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Sets the guaranteed maximum value. |
||
| 347 | * |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setValueMax(?float $value_max): self |
||
| 351 | { |
||
| 352 | $this->value_max = $value_max; |
||
| 353 | |||
| 354 | return $this; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the unit used by the value (e.g. "V"). |
||
| 359 | */ |
||
| 360 | public function getUnit(): string |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Sets the unit used by the value. |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function setUnit(string $unit): self |
||
| 371 | { |
||
| 372 | $this->unit = $unit; |
||
| 373 | |||
| 374 | return $this; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Returns the text value. |
||
| 379 | */ |
||
| 380 | public function getValueText(): string |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Sets the text value. |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | public function setValueText(string $value_text): self |
||
| 391 | { |
||
| 392 | $this->value_text = $value_text; |
||
| 393 | |||
| 394 | return $this; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Return a string representation and (if possible) with its unit. |
||
| 399 | */ |
||
| 400 | protected function formatWithUnit(float $value, string $format = '%g'): string |
||
| 408 | } |
||
| 409 | } |
||
| 410 |