Conditions | 12 |
Paths | 20 |
Total Lines | 51 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
96 | protected function validateOBSource(?int $accountId, ?string $accountName): bool |
||
97 | { |
||
98 | Log::debug(sprintf('Now in validateOBSource(%d, "%s")', $accountId, $accountName)); |
||
99 | Log::debug(sprintf('The account name is null: %s', var_export(null === $accountName, true))); |
||
100 | $result = null; |
||
101 | // source can be any of the following types. |
||
102 | $validTypes = array_keys($this->combinations[$this->transactionType]); |
||
103 | |||
104 | if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { |
||
105 | // if both values are NULL return false, |
||
106 | // because the source of a deposit can't be created. |
||
107 | // (this never happens). |
||
108 | $this->sourceError = (string) trans('validation.ob_source_need_data'); |
||
109 | $result = false; |
||
110 | } |
||
111 | |||
112 | // if the user submits an ID only but that ID is not of the correct type, |
||
113 | // return false. |
||
114 | if (null !== $accountId && null === $accountName) { |
||
115 | Log::debug('Source ID is not null, but name is null.'); |
||
116 | $search = $this->accountRepository->findNull($accountId); |
||
117 | |||
118 | // the source resulted in an account, but it's not of a valid type. |
||
119 | if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { |
||
120 | $message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type); |
||
121 | Log::debug($message); |
||
122 | $this->sourceError = $message; |
||
123 | $result = false; |
||
124 | } |
||
125 | // the source resulted in an account, AND it's of a valid type. |
||
126 | if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { |
||
127 | Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name)); |
||
128 | $this->source = $search; |
||
129 | $result = true; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // if the account can be created anyway we don't need to search. |
||
134 | if (null === $result && true === $this->canCreateTypes($validTypes)) { |
||
135 | Log::debug('Result is still null.'); |
||
136 | $result = true; |
||
137 | |||
138 | // set the source to be a (dummy) initial balance account. |
||
139 | $account = new Account; |
||
140 | $accountType = AccountType::whereType(AccountType::INITIAL_BALANCE)->first(); |
||
141 | $account->accountType = $accountType; |
||
142 | $this->source = $account; |
||
143 | } |
||
144 | $result = $result ?? false; |
||
145 | |||
146 | return $result; |
||
147 | } |
||
148 | } |