Conditions | 16 |
Paths | 13 |
Total Lines | 63 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
79 | public function __construct( |
||
80 | $requestId, |
||
81 | $type, |
||
82 | $createdAt, |
||
83 | $sequenceId, |
||
84 | $merchantUserId, |
||
85 | $score, |
||
86 | $accept, |
||
87 | $reject, |
||
88 | $manual, |
||
89 | $reason = null, |
||
90 | $action = null, |
||
91 | $customResponse = null |
||
92 | ) { |
||
93 | if (!is_int($requestId)) { |
||
94 | throw new \InvalidArgumentException('Request ID must be integer'); |
||
95 | } |
||
96 | if (!is_string($type)) { |
||
97 | throw new \InvalidArgumentException('Type must be string'); |
||
98 | } |
||
99 | if (!is_int($createdAt)) { |
||
100 | throw new \InvalidArgumentException('Created At must be integer'); |
||
101 | } |
||
102 | if (!is_string($sequenceId)) { |
||
103 | throw new \InvalidArgumentException('Sequence Id must be string'); |
||
104 | } |
||
105 | if (!is_string($merchantUserId)) { |
||
106 | throw new \InvalidArgumentException('Merchant User Id must be string'); |
||
107 | } |
||
108 | if (!is_int($score)) { |
||
109 | throw new \InvalidArgumentException('Score must be integer'); |
||
110 | } |
||
111 | if (!is_bool($accept)) { |
||
112 | throw new \InvalidArgumentException('Accept flag must be boolean'); |
||
113 | } |
||
114 | if (!is_bool($reject)) { |
||
115 | throw new \InvalidArgumentException('Reject flag must be boolean'); |
||
116 | } |
||
117 | if (!is_bool($manual)) { |
||
118 | throw new \InvalidArgumentException('Manual flag must be boolean'); |
||
119 | } |
||
120 | if ($reason !== null && !is_string($reason)) { |
||
121 | throw new \InvalidArgumentException('Reason must be string'); |
||
122 | } |
||
123 | if ($action !== null && !is_string($action)) { |
||
124 | throw new \InvalidArgumentException('Action must be string'); |
||
125 | } |
||
126 | if ($customResponse !== null && !is_array($customResponse)) { |
||
127 | throw new \InvalidArgumentException('Custom Response must be array'); |
||
128 | } |
||
129 | $this->requestId = $requestId; |
||
130 | $this->type = $type; |
||
131 | $this->createdAt = $createdAt; |
||
132 | $this->sequenceId = $sequenceId; |
||
133 | $this->merchantUserId = $merchantUserId; |
||
134 | $this->score = $score; |
||
135 | $this->accept = $accept; |
||
136 | $this->reject = $reject; |
||
137 | $this->manual = $manual; |
||
138 | $this->reason = $reason; |
||
139 | $this->action = $action; |
||
140 | $this->customResponse = $customResponse; |
||
141 | } |
||
142 | |||
239 |