| Total Complexity | 47 |
| Total Lines | 615 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PlaceOrderRequest 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 PlaceOrderRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class PlaceOrderRequest extends AbstractParameters implements IPlaceOrderRequestInterface |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Symbol name |
||
| 21 | * @var string $symbol |
||
| 22 | */ |
||
| 23 | protected string $symbol; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Buy,Sell |
||
| 27 | * @var string $side |
||
| 28 | */ |
||
| 29 | protected string $side; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Market, Limit |
||
| 33 | * @var string $orderType |
||
| 34 | */ |
||
| 35 | protected string $orderType; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Order quantity |
||
| 39 | * @var float $qty |
||
| 40 | */ |
||
| 41 | protected float $qty; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @var string $timeInForce |
||
| 46 | */ |
||
| 47 | protected string $timeInForce = EnumTimeInForce::GOOD_TILL_CANCELED_FULL; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Order price. Invalid if orderType=Market |
||
| 51 | * @var float $price |
||
| 52 | */ |
||
| 53 | protected float $price; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Conditional order param. Used to identify the expected direction of the conditional order. |
||
| 57 | * 1: triggered when market price rises to triggerPrice |
||
| 58 | * 2: triggered when market price falls to triggerPrice |
||
| 59 | * @var int $triggerDirection |
||
| 60 | */ |
||
| 61 | protected int $triggerDirection; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Conditional order param. If you expect the price to rise to trigger your conditional order, make sure: |
||
| 65 | * triggerPrice > market price |
||
| 66 | * Else, triggerPrice < market price |
||
| 67 | * @var string $triggerPrice |
||
| 68 | */ |
||
| 69 | protected string $triggerPrice; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Trigger price type. default:LastPrice. |
||
| 73 | * @var string $triggerBy |
||
| 74 | */ |
||
| 75 | protected string $triggerBy = EnumTriggerPriceType::PRICE_TYPE_LAST; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Position index. It is required for hedge position mode |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | protected int $positionIdx; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * User customised order id. A max of 36 characters. Combinations of numbers, letters (upper and lower cases), dashes, and underscores are supported. |
||
| 85 | * rule of future: |
||
| 86 | * optional param |
||
| 87 | * The same orderLinkId can be used for both USDC PERP and USDT PERP. |
||
| 88 | * An orderLinkId can be reused once the original order is either Filled or Cancelled |
||
| 89 | * rule of option: |
||
| 90 | * This param is required |
||
| 91 | * always unique |
||
| 92 | * @var string $orderLinkId |
||
| 93 | */ |
||
| 94 | protected string $orderLinkId; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Take profit price |
||
| 98 | * @var float $takeProfit |
||
| 99 | */ |
||
| 100 | protected float $takeProfit; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Stop loss price |
||
| 104 | * @var float $stopLoss |
||
| 105 | */ |
||
| 106 | protected float $stopLoss; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The price type to trigger take profit. default:LastPrice |
||
| 110 | * @var string $tpTriggerBy |
||
| 111 | */ |
||
| 112 | protected string $tpTriggerBy; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The price type to trigger stop loss. default:LastPrice |
||
| 116 | * @var string $slTriggerBy |
||
| 117 | */ |
||
| 118 | protected string $slTriggerBy; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * What is a reduce-only order? true means your position can only reduce in size if this order is triggered. |
||
| 122 | * When reduce_only is true, take profit/stop loss cannot be set |
||
| 123 | * @var bool $reduceOnly |
||
| 124 | */ |
||
| 125 | protected bool $reduceOnly; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string $smpType |
||
| 129 | */ |
||
| 130 | protected string $smpType; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * What is a close on trigger order? For a closing order. It can only reduce your position, not increase it. |
||
| 134 | * If the account has insufficient available balance when the closing order is triggered, then other active orders |
||
| 135 | * of similar contracts will be cancelled or reduced. It can be used to ensure your stop loss reduces your position |
||
| 136 | * regardless of current available margin. |
||
| 137 | * @var bool $closeOnTrigger |
||
| 138 | */ |
||
| 139 | protected bool $closeOnTrigger; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * TP/SL mode |
||
| 143 | * Full: entire position for TP/SL. Then, tpOrderType or slOrderType must be Market |
||
| 144 | * Partial: partial position tp/sl. Limit TP/SL order are supported. Note: When create limit tp/sl, |
||
| 145 | * tpslMode is required and it must be Partial |
||
| 146 | * @var string $tpslMode |
||
| 147 | */ |
||
| 148 | protected string $tpslMode; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * The limit order price when take profit price is triggered. Only works when tpslMode=Partial and tpOrderType=Limit |
||
| 152 | * @var string $tpLimitPrice |
||
| 153 | */ |
||
| 154 | protected string $tpLimitPrice; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The limit order price when stop loss price is triggered. Only works when tpslMode=Partial and slOrderType=Limit |
||
| 158 | * @var string $slLimitPrice |
||
| 159 | */ |
||
| 160 | protected string $slLimitPrice; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * The order type when take profit is triggered. Market(default), Limit. For tpslMode=Full, |
||
| 164 | * it only supports tpOrderType=Market |
||
| 165 | * @var string $tpOrderType |
||
| 166 | */ |
||
| 167 | protected string $tpOrderType; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The order type when stop loss is triggered. Market(default), Limit. For tpslMode=Full, |
||
| 171 | * it only supports slOrderType=Market |
||
| 172 | * @var string $slOrderType |
||
| 173 | */ |
||
| 174 | protected string $slOrderType; |
||
| 175 | |||
| 176 | public function __construct() |
||
| 177 | { |
||
| 178 | $this |
||
| 179 | ->setRequiredField('symbol') |
||
| 180 | ->setRequiredField('side') |
||
| 181 | ->setRequiredField('orderType') |
||
| 182 | ->setRequiredField('qty'); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getSymbol(): string |
||
| 189 | { |
||
| 190 | return $this->symbol; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $symbol |
||
| 195 | * @return PlaceOrderRequest |
||
| 196 | */ |
||
| 197 | public function setSymbol(string $symbol): self |
||
| 198 | { |
||
| 199 | $this->symbol = $symbol; |
||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getSide(): string |
||
| 207 | { |
||
| 208 | return $this->side; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $side |
||
| 213 | * @return PlaceOrderRequest |
||
| 214 | * @throws SDKException |
||
| 215 | */ |
||
| 216 | public function setSide(string $side): self |
||
| 217 | { |
||
| 218 | ArrayHelper::checkValueWithStack($side, EnumSide::ORDER_SIDE_LIST); |
||
| 219 | $this->side = $side; |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getOrderType(): string |
||
| 228 | { |
||
| 229 | return $this->orderType; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $orderType |
||
| 234 | * @return PlaceOrderRequest |
||
| 235 | * @throws SDKException |
||
| 236 | */ |
||
| 237 | public function setOrderType(string $orderType): self |
||
| 238 | { |
||
| 239 | ArrayHelper::checkValueWithStack($orderType, EnumOrderType::ORDER_TYPE_LIST); |
||
| 240 | $this->orderType = $orderType; |
||
| 241 | |||
| 242 | return $this; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return float |
||
| 247 | */ |
||
| 248 | public function getQty(): float |
||
| 249 | { |
||
| 250 | return $this->qty; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param float $quantity |
||
| 255 | * @return PlaceOrderRequest |
||
| 256 | * @throws SDKException |
||
| 257 | */ |
||
| 258 | public function setQty(float $quantity): self |
||
| 259 | { |
||
| 260 | NumericHelper::checkValueMoreThan($quantity, 0.00000001); |
||
| 261 | $this->qty = $quantity; |
||
| 262 | |||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getTimeInForce(): string |
||
| 270 | { |
||
| 271 | return $this->timeInForce; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $timeInForce |
||
| 276 | * @return PlaceOrderRequest |
||
| 277 | */ |
||
| 278 | public function setTimeInForce(string $timeInForce): self |
||
| 279 | { |
||
| 280 | ArrayHelper::checkValueWithStack($timeInForce, EnumTimeInForce::TIME_IN_FORCE_LIST_FULL); |
||
| 281 | $this->timeInForce = $timeInForce; |
||
| 282 | return $this; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return float |
||
| 287 | */ |
||
| 288 | public function getPrice(): float |
||
| 289 | { |
||
| 290 | return $this->price; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param float $price |
||
| 295 | * @return PlaceOrderRequest |
||
| 296 | * @throws SDKException |
||
| 297 | */ |
||
| 298 | public function setPrice(float $price): self |
||
| 299 | { |
||
| 300 | NumericHelper::checkValueMoreThan($price, 0.000001); |
||
| 301 | $this->price = $price; |
||
| 302 | return $this; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return int |
||
| 307 | */ |
||
| 308 | public function getTriggerDirection(): int |
||
| 309 | { |
||
| 310 | return $this->triggerDirection; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param int $triggerDirection |
||
| 315 | * @return PlaceOrderRequest |
||
| 316 | * @throws SDKException |
||
| 317 | */ |
||
| 318 | public function setTriggerDirection(int $triggerDirection): self |
||
| 319 | { |
||
| 320 | ArrayHelper::checkValueWithStack($triggerDirection, EnumTriggerDirection::DIRECTION_LIST); |
||
| 321 | $this->triggerDirection = $triggerDirection; |
||
|
|
|||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getTriggerPrice(): string |
||
| 330 | { |
||
| 331 | return $this->triggerPrice; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $triggerPrice |
||
| 336 | * @return PlaceOrderRequest |
||
| 337 | * @throws SDKException |
||
| 338 | */ |
||
| 339 | public function setTriggerPrice(string $triggerPrice): self |
||
| 340 | { |
||
| 341 | NumericHelper::checkValueMoreThan($triggerPrice, 0.000001); |
||
| 342 | $this->triggerPrice = $triggerPrice; |
||
| 343 | return $this; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getTriggerBy(): string |
||
| 350 | { |
||
| 351 | return $this->triggerBy; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $triggerBy |
||
| 356 | * @return PlaceOrderRequest |
||
| 357 | * @throws SDKException |
||
| 358 | */ |
||
| 359 | public function setTriggerBy(string $triggerBy): self |
||
| 360 | { |
||
| 361 | ArrayHelper::checkValueWithStack($triggerBy, EnumTriggerPriceType::PRICE_TYPE_LIST); |
||
| 362 | $this->triggerBy = $triggerBy; |
||
| 363 | |||
| 364 | return $this; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | public function getPositionIdx(): int |
||
| 371 | { |
||
| 372 | return $this->positionIdx; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param int $positionIdx |
||
| 377 | * @return PlaceOrderRequest |
||
| 378 | */ |
||
| 379 | public function setPositionIdx(int $positionIdx): self |
||
| 380 | { |
||
| 381 | $this->positionIdx = $positionIdx; |
||
| 382 | return $this; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function getOrderLinkId(): string |
||
| 389 | { |
||
| 390 | return $this->orderLinkId; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $orderLinkId |
||
| 395 | * @return PlaceOrderRequest |
||
| 396 | */ |
||
| 397 | public function setOrderLinkId(string $orderLinkId): self |
||
| 398 | { |
||
| 399 | $this->orderLinkId = $orderLinkId; |
||
| 400 | return $this; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return float |
||
| 405 | */ |
||
| 406 | public function getTakeProfit(): float |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param float $takeProfit |
||
| 413 | * @return PlaceOrderRequest |
||
| 414 | * @throws SDKException |
||
| 415 | */ |
||
| 416 | public function setTakeProfit(float $takeProfit): self |
||
| 417 | { |
||
| 418 | NumericHelper::checkValueMoreThan($takeProfit, 0.000001); |
||
| 419 | $this->takeProfit = $takeProfit; |
||
| 420 | |||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return float |
||
| 426 | */ |
||
| 427 | public function getStopLoss(): float |
||
| 428 | { |
||
| 429 | return $this->stopLoss; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param float $stopLoss |
||
| 434 | * @return PlaceOrderRequest |
||
| 435 | * @throws SDKException |
||
| 436 | */ |
||
| 437 | public function setStopLoss(float $stopLoss): self |
||
| 438 | { |
||
| 439 | NumericHelper::checkValueMoreThan($stopLoss, 0.000001); |
||
| 440 | $this->stopLoss = $stopLoss; |
||
| 441 | return $this; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getTpTriggerBy(): string |
||
| 448 | { |
||
| 449 | return $this->tpTriggerBy; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $tpTriggerBy |
||
| 454 | * @return PlaceOrderRequest |
||
| 455 | */ |
||
| 456 | public function setTpTriggerBy(string $tpTriggerBy): self |
||
| 457 | { |
||
| 458 | $this->tpTriggerBy = $tpTriggerBy; |
||
| 459 | return $this; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getSlTriggerBy(): string |
||
| 466 | { |
||
| 467 | return $this->slTriggerBy; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $slTriggerBy |
||
| 472 | * @return PlaceOrderRequest |
||
| 473 | */ |
||
| 474 | public function setSlTriggerBy(string $slTriggerBy): self |
||
| 475 | { |
||
| 476 | $this->slTriggerBy = $slTriggerBy; |
||
| 477 | return $this; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return bool |
||
| 482 | */ |
||
| 483 | public function isReduceOnly(): bool |
||
| 484 | { |
||
| 485 | return $this->reduceOnly; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param bool $reduceOnly |
||
| 490 | * @return PlaceOrderRequest |
||
| 491 | */ |
||
| 492 | public function setReduceOnly(bool $reduceOnly): self |
||
| 493 | { |
||
| 494 | $this->reduceOnly = $reduceOnly; |
||
| 495 | return $this; |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function getSmpType(): string |
||
| 502 | { |
||
| 503 | return $this->smpType; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $smpType |
||
| 508 | * @return PlaceOrderRequest |
||
| 509 | */ |
||
| 510 | public function setSmpType(string $smpType): self |
||
| 511 | { |
||
| 512 | $this->smpType = $smpType; |
||
| 513 | return $this; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | public function isCloseOnTrigger(): bool |
||
| 520 | { |
||
| 521 | return $this->closeOnTrigger; |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param bool $closeOnTrigger |
||
| 526 | * @return PlaceOrderRequest |
||
| 527 | */ |
||
| 528 | public function setCloseOnTrigger(bool $closeOnTrigger): self |
||
| 529 | { |
||
| 530 | $this->closeOnTrigger = $closeOnTrigger; |
||
| 531 | return $this; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | public function getTpslMode(): string |
||
| 538 | { |
||
| 539 | return $this->tpslMode; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param string $tpslMode |
||
| 544 | * @return PlaceOrderRequest |
||
| 545 | * @throws SDKException |
||
| 546 | */ |
||
| 547 | public function setTpslMode(string $tpslMode): self |
||
| 548 | { |
||
| 549 | ArrayHelper::checkValueWithStack($tpslMode, EnumTPSLModes::MODE_LIST); |
||
| 550 | $this->tpslMode = $tpslMode; |
||
| 551 | |||
| 552 | return $this; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function getTpLimitPrice(): string |
||
| 559 | { |
||
| 560 | return $this->tpLimitPrice; |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param string $tpLimitPrice |
||
| 565 | * @return PlaceOrderRequest |
||
| 566 | */ |
||
| 567 | public function setTpLimitPrice(string $tpLimitPrice): self |
||
| 568 | { |
||
| 569 | $this->tpLimitPrice = $tpLimitPrice; |
||
| 570 | return $this; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | public function getSlLimitPrice(): string |
||
| 577 | { |
||
| 578 | return $this->slLimitPrice; |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param string $slLimitPrice |
||
| 583 | * @return PlaceOrderRequest |
||
| 584 | */ |
||
| 585 | public function setSlLimitPrice(string $slLimitPrice): self |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function getTpOrderType(): string |
||
| 595 | { |
||
| 596 | return $this->tpOrderType; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param string $tpOrderType |
||
| 601 | * @return PlaceOrderRequest |
||
| 602 | * @throws SDKException |
||
| 603 | */ |
||
| 604 | public function setTpOrderType(string $tpOrderType): self |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | public function getSlOrderType(): string |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @param string $slOrderType |
||
| 622 | * @return PlaceOrderRequest |
||
| 623 | * @throws SDKException |
||
| 624 | */ |
||
| 625 | public function setSlOrderType(string $slOrderType): self |
||
| 626 | { |
||
| 627 | ArrayHelper::checkValueWithStack($slOrderType, EnumOrderType::ORDER_TYPE_LIST); |
||
| 628 | $this->slOrderType = $slOrderType; |
||
| 631 | } |
||
| 632 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.