Conditions | 11 |
Paths | 80 |
Total Lines | 42 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function IsTradeValid(Draft $draft, Trade $trade) { |
||
31 | $valid = true; |
||
32 | $errors = array(); |
||
33 | $assetCount = count($trade->trade_assets); |
||
34 | $uniqueAssetCount = count(array_unique($trade->trade_assets)); |
||
35 | |||
36 | if (empty($trade->trade_round) || $trade->trade_round < 0 || $trade->trade_round > $draft->draft_rounds) { |
||
|
|||
37 | $errors[] = "Invalid value for trade round."; |
||
38 | $valid = false; |
||
39 | } |
||
40 | |||
41 | foreach ($trade->trade_assets as $trade_asset) { |
||
42 | if ($trade_asset->oldmanager_id != $trade->manager1_id |
||
43 | && $trade_asset->oldmanager_id != $trade->manager2_id) { |
||
44 | $player_id = $trade_asset->player->player_id; |
||
45 | $errors[] = "Asset #$player_id does not belong to either manager."; |
||
46 | $valid = false; |
||
47 | } |
||
48 | |||
49 | if ($trade_asset->player->draft_id != $draft->draft_id) { |
||
50 | $player_id = $trade_asset->player->player_id; |
||
51 | $errors[] = "Asset #$player_id does not belong to draft #$draft->draft_id"; |
||
52 | $valid = false; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | if ($assetCount != $uniqueAssetCount) { |
||
57 | $errors[] = "One or more of the trade assets are duplicate."; |
||
58 | $valid = false; |
||
59 | } |
||
60 | |||
61 | if ($draft->draft_id != $trade->manager1->draft_id) { |
||
62 | $errors[] = "Manager 1 does not belong to draft #$draft->draft_id"; |
||
63 | $valid = false; |
||
64 | } |
||
65 | |||
66 | if ($draft->draft_id != $trade->manager2->draft_id) { |
||
67 | $errors[] = "Manager 2 does not belong to draft #$draft->draft_id"; |
||
68 | $valid = false; |
||
69 | } |
||
70 | |||
71 | return new PhpDraftResponse($valid, $errors); |
||
72 | } |
||
73 | } |