Conditions | 17 |
Paths | 99 |
Total Lines | 59 |
Code Lines | 23 |
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 |
||
114 | * If either date is invalid return -1. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | public function ageDays(): int |
||
119 | { |
||
120 | if ($this->is_valid) { |
||
121 | return $this->total_days; |
||
122 | } |
||
123 | |||
124 | return -1; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * How many years between two events? |
||
129 | * If either date is invalid return -1. |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | public function ageYears(): int |
||
134 | { |
||
135 | if ($this->is_valid) { |
||
136 | return $this->years; |
||
137 | } |
||
138 | |||
139 | return -1; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param bool $living |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function ageAtEvent(bool $living): string |
||
148 | { |
||
149 | $age = $this->ageString(); |
||
150 | |||
151 | if ($age === '') { |
||
152 | return ''; |
||
153 | } |
||
154 | |||
155 | if ($living) { |
||
156 | /* I18N: The current age of a living individual */ |
||
157 | return I18N::translate('(age %s)', $age); |
||
158 | } |
||
159 | |||
160 | /* I18N: The age of an individual at a given date */ |
||
161 | return I18N::translate('(aged %s)', $age); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Similar to age-at-event, but for events such as burial, cremation, etc. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function timeAfterDeath(): string |
||
170 | { |
||
171 | if (!$this->is_valid) { |
||
172 | return ''; |
||
173 | } |
||
186 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: