Conditions | 13 |
Paths | 120 |
Total Lines | 57 |
Code Lines | 37 |
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 |
||
22 | public function extract() |
||
23 | { |
||
24 | $matchedParts = [ |
||
25 | 'month' => null, |
||
26 | 'day' => null, |
||
27 | 'year' => null, |
||
28 | 'hour' => null, |
||
29 | 'minute' => null, |
||
30 | 'seconds' => null, |
||
31 | 'timezone' => null, |
||
32 | 'meridiem' => null, |
||
33 | ]; |
||
34 | $text = $this->text; |
||
35 | // replace all non-whitespace with space |
||
36 | $text = preg_replace('/[^\w:\/\\-]+/', ' ', $text); |
||
37 | // normalize spaces |
||
38 | $text = preg_replace('/\s+/', ' ', $text); |
||
39 | $parts = explode(' ', $text); |
||
40 | foreach ($parts as $key => $part) { |
||
41 | foreach ($matchedParts as $term => $value) { |
||
42 | /* |
||
43 | * We re-run the test method each time because Dec 01 01:01:01 could match only the first 01. So we |
||
44 | * have to be a bit greedy here. |
||
45 | */ |
||
46 | $fn = 'test' . ucfirst($term); |
||
47 | if ($this->$fn($part) && $matchedParts[$term] === null) { |
||
48 | $matchedParts[$term] = $key; |
||
49 | } |
||
50 | |||
51 | } |
||
52 | } |
||
53 | $max = false; |
||
54 | $min = PHP_INT_MAX; |
||
55 | foreach ($matchedParts as $part) { |
||
56 | if ($part === null) continue; |
||
57 | if ($part >= $max) { |
||
58 | $max = $part; |
||
59 | } |
||
60 | if ($part <= $min) { |
||
61 | $min = $part; |
||
62 | } |
||
63 | } |
||
64 | if ($max === false) { |
||
65 | // Didn't find a date |
||
66 | return; |
||
67 | } |
||
68 | $foundDate = ''; |
||
69 | for ($i = (int)$min; $i <= (int)$max; $i++) { |
||
70 | $foundDate .= ' ' . $parts[$i]; |
||
71 | } |
||
72 | $parse = date_parse(trim($foundDate)); |
||
73 | if (isset($parse['error_count']) && $parse['error_count'] == 0) { |
||
74 | $start = strpos($this->text, $parts[$min]); |
||
75 | $end = strpos($this->text, $parts[$max]) + strlen($parts[$max]); |
||
76 | $this->dateString = substr($this->text, $start, ($end - $start)); |
||
77 | } |
||
78 | } |
||
79 | |||
207 |
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.