| Conditions | 15 |
| Paths | 9 |
| Total Lines | 66 |
| Code Lines | 42 |
| 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 |
||
| 71 | public function format(AbstractLogEntry $context): string |
||
| 72 | { |
||
| 73 | if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) { |
||
| 74 | return sprintf( |
||
| 75 | "<i>%s</i>: %s", |
||
| 76 | $this->translator->trans('log.user_login.ip'), |
||
| 77 | htmlspecialchars($context->getIPAddress()) |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($context instanceof ExceptionLogEntry) { |
||
| 82 | return sprintf( |
||
| 83 | '<i>%s</i> %s:%d : %s', |
||
| 84 | htmlspecialchars($context->getExceptionClass()), |
||
| 85 | htmlspecialchars($context->getFile()), |
||
| 86 | $context->getLine(), |
||
| 87 | htmlspecialchars($context->getMessage()) |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($context instanceof DatabaseUpdatedLogEntry) { |
||
| 92 | return sprintf( |
||
| 93 | '<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s', |
||
| 94 | $this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'), |
||
| 95 | $context->getOldVersion(), |
||
| 96 | $context->getNewVersion() |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) { |
||
| 101 | return sprintf( |
||
| 102 | '<i>%s</i>: %s', |
||
| 103 | $this->translator->trans('log.element_created.original_instock'), |
||
| 104 | $context->getCreationInstockValue() |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($context instanceof ElementDeletedLogEntry) { |
||
| 109 | return sprintf( |
||
| 110 | '<i>%s</i>: %s', |
||
| 111 | $this->translator->trans('log.element_deleted.old_name'), |
||
| 112 | $context->getOldName() |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($context instanceof ElementEditedLogEntry && !empty($context->getMessage())) { |
||
| 117 | return htmlspecialchars($context->getMessage()); |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($context instanceof InstockChangedLogEntry) { |
||
| 121 | return sprintf( |
||
| 122 | '<i>%s</i>; %s <i class="fas fa-long-arrow-alt-right"></i> %s (%s); %s: %s', |
||
| 123 | $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'), |
||
| 124 | $context->getOldInstock(), |
||
| 125 | $context->getNewInstock(), |
||
| 126 | (!$context->isWithdrawal() ? '+' : '-') . $context->getDifference(true), |
||
| 127 | $this->translator->trans('log.instock_changed.comment'), |
||
| 128 | htmlspecialchars($context->getComment()) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($context instanceof UserNotAllowedLogEntry) { |
||
| 133 | return htmlspecialchars($context->getMessage()); |
||
| 134 | } |
||
| 135 | |||
| 136 | return ""; |
||
| 137 | } |
||
| 138 | } |