Conditions | 8 |
Paths | 21 |
Total Lines | 51 |
Code Lines | 36 |
Lines | 8 |
Ratio | 15.69 % |
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 |
||
41 | public function process($value, $format = '%b %e, %Y', $default = null) |
||
42 | { |
||
43 | if (!empty($value)) { |
||
44 | // convert if it's not a valid unix timestamp |
||
45 | if (preg_match('#^-?\d{1,10}$#', $value) === 0) { |
||
46 | $value = strtotime($value); |
||
47 | } |
||
48 | } elseif (!empty($default)) { |
||
49 | // convert if it's not a valid unix timestamp |
||
50 | if (preg_match('#^-?\d{1,10}$#', $default) === 0) { |
||
51 | $value = strtotime($default); |
||
52 | } else { |
||
53 | $value = $default; |
||
54 | } |
||
55 | } else { |
||
56 | return ''; |
||
57 | } |
||
58 | |||
59 | // Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin |
||
60 | if (DIRECTORY_SEPARATOR == '\\') { |
||
61 | $_win_from = array( |
||
62 | '%D', |
||
63 | '%h', |
||
64 | '%n', |
||
65 | '%r', |
||
66 | '%R', |
||
67 | '%t', |
||
68 | '%T' |
||
69 | ); |
||
70 | $_win_to = array( |
||
71 | '%m/%d/%y', |
||
72 | '%b', |
||
73 | "\n", |
||
74 | '%I:%M:%S %p', |
||
75 | '%H:%M', |
||
76 | "\t", |
||
77 | '%H:%M:%S' |
||
78 | ); |
||
79 | View Code Duplication | if (strpos($format, '%e') !== false) { |
|
|
|||
80 | $_win_from[] = '%e'; |
||
81 | $_win_to[] = sprintf('%\' 2d', date('j', $value)); |
||
82 | } |
||
83 | View Code Duplication | if (strpos($format, '%l') !== false) { |
|
84 | $_win_from[] = '%l'; |
||
85 | $_win_to[] = sprintf('%\' 2d', date('h', $value)); |
||
86 | } |
||
87 | $format = str_replace($_win_from, $_win_to, $format); |
||
88 | } |
||
89 | |||
90 | return strftime($format, $value); |
||
91 | } |
||
92 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.