Conditions | 9 |
Paths | 9 |
Total Lines | 58 |
Code Lines | 34 |
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 |
||
48 | public function replace(string $placeholder, object $label_target, array $options = []): ?string |
||
49 | { |
||
50 | if ($placeholder === "[[INSTALL_NAME]]") { |
||
51 | return $this->partdb_title; |
||
52 | } |
||
53 | |||
54 | |||
55 | $user = $this->security->getUser(); |
||
56 | if ($placeholder === "[[USERNAME]]") { |
||
57 | if ($user instanceof User) { |
||
58 | return $user->getName(); |
||
59 | } |
||
60 | return 'anonymous'; |
||
61 | } |
||
62 | |||
63 | if ($placeholder === "[[USERNAME_FULL]]") { |
||
64 | if ($user instanceof User) { |
||
65 | return $user->getFullName(true); |
||
66 | } |
||
67 | return 'anonymous'; |
||
68 | } |
||
69 | |||
70 | $now = new \DateTime(); |
||
71 | |||
72 | if ($placeholder === '[[DATETIME]]') { |
||
73 | $formatter = IntlDateFormatter::create( |
||
74 | Locale::getDefault(), |
||
75 | IntlDateFormatter::SHORT, |
||
76 | IntlDateFormatter::SHORT, |
||
77 | $now->getTimezone() |
||
|
|||
78 | ); |
||
79 | |||
80 | return $formatter->format($now); |
||
81 | } |
||
82 | |||
83 | if ($placeholder === '[[DATE]]') { |
||
84 | $formatter = IntlDateFormatter::create( |
||
85 | Locale::getDefault(), |
||
86 | IntlDateFormatter::SHORT, |
||
87 | IntlDateFormatter::NONE, |
||
88 | $now->getTimezone() |
||
89 | ); |
||
90 | |||
91 | return $formatter->format($now); |
||
92 | } |
||
93 | |||
94 | if ($placeholder === '[[TIME]]') { |
||
95 | $formatter = IntlDateFormatter::create( |
||
96 | Locale::getDefault(), |
||
97 | IntlDateFormatter::NONE, |
||
98 | IntlDateFormatter::SHORT, |
||
99 | $now->getTimezone() |
||
100 | ); |
||
101 | |||
102 | return $formatter->format($now); |
||
103 | } |
||
104 | |||
105 | return null; |
||
106 | } |
||
107 | } |