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