| Conditions | 16 |
| Paths | 2504 |
| Total Lines | 90 |
| Code Lines | 45 |
| 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 |
||
| 72 | function getDateCond( $year, $month, $day = -1 ) { |
||
| 73 | $year = intval( $year ); |
||
| 74 | $month = intval( $month ); |
||
| 75 | $day = intval( $day ); |
||
| 76 | |||
| 77 | // Basic validity checks for year and month |
||
| 78 | $this->mYear = $year > 0 ? $year : false; |
||
| 79 | $this->mMonth = ( $month > 0 && $month < 13 ) ? $month : false; |
||
| 80 | |||
| 81 | // If year and month are false, don't update the mOffset |
||
| 82 | if ( !$this->mYear && !$this->mMonth ) { |
||
| 83 | return null; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Given an optional year, month, and day, we need to generate a timestamp |
||
| 87 | // to use as "WHERE rev_timestamp <= result" |
||
| 88 | // Examples: year = 2006 equals < 20070101 (+000000) |
||
| 89 | // year=2005, month=1 equals < 20050201 |
||
| 90 | // year=2005, month=12 equals < 20060101 |
||
| 91 | // year=2005, month=12, day=5 equals < 20051206 |
||
| 92 | if ( $this->mYear ) { |
||
| 93 | $year = $this->mYear; |
||
| 94 | } else { |
||
| 95 | // If no year given, assume the current one |
||
| 96 | $timestamp = MWTimestamp::getInstance(); |
||
| 97 | $year = $timestamp->format( 'Y' ); |
||
| 98 | // If this month hasn't happened yet this year, go back to last year's month |
||
| 99 | if ( $this->mMonth > $timestamp->format( 'n' ) ) { |
||
| 100 | $year--; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if ( $this->mMonth ) { |
||
| 105 | $month = $this->mMonth; |
||
| 106 | |||
| 107 | // Day validity check after we have month and year checked |
||
| 108 | $this->mDay = checkdate( $month, $day, $year ) ? $day : false; |
||
| 109 | |||
| 110 | if ( $this->mDay ) { |
||
| 111 | // If we have a day, we want up to the day immediately afterward |
||
| 112 | $day = $this->mDay + 1; |
||
| 113 | |||
| 114 | // Did we overflow the current month? |
||
| 115 | if ( !checkdate( $month, $day, $year ) ) { |
||
| 116 | $day = 1; |
||
| 117 | $month++; |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | // If no day, assume beginning of next month |
||
| 121 | $day = 1; |
||
| 122 | $month++; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Did we overflow the current year? |
||
| 126 | if ( $month > 12 ) { |
||
| 127 | $month = 1; |
||
| 128 | $year++; |
||
| 129 | } |
||
| 130 | |||
| 131 | } else { |
||
| 132 | // No month implies we want up to the end of the year in question |
||
| 133 | $month = 1; |
||
| 134 | $day = 1; |
||
| 135 | $year++; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Y2K38 bug |
||
| 139 | if ( $year > 2032 ) { |
||
| 140 | $year = 2032; |
||
| 141 | } |
||
| 142 | |||
| 143 | $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day ); |
||
| 144 | |||
| 145 | if ( $ymd > 20320101 ) { |
||
| 146 | $ymd = 20320101; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Treat the given time in the wiki timezone and get a UTC timestamp for the database lookup |
||
| 150 | $timestamp = MWTimestamp::getInstance( "${ymd}000000" ); |
||
| 151 | $timestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) ); |
||
| 152 | |||
| 153 | try { |
||
| 154 | $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() ); |
||
| 155 | } catch ( TimestampException $e ) { |
||
| 156 | // Invalid user provided timestamp (T149257) |
||
| 157 | return null; |
||
| 158 | } |
||
| 159 | |||
| 160 | return $this->mOffset; |
||
| 161 | } |
||
| 162 | } |
||
| 163 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: