| Conditions | 5 |
| Paths | 9 |
| Total Lines | 119 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 55 | public function validProvider() { |
||
| 56 | $gregorian = 'http://www.wikidata.org/entity/Q1985727'; |
||
| 57 | $julian = 'http://www.wikidata.org/entity/Q1985786'; |
||
| 58 | |||
| 59 | $baseOptions = new FormatterOptions(); |
||
| 60 | $baseOptions->setOption( TimeFormatter::OPT_CALENDARNAMES, array( |
||
| 61 | $gregorian => '<Gregorian>', |
||
| 62 | $julian => '<Julian>', |
||
| 63 | ) ); |
||
| 64 | |||
| 65 | $timestampFormatterOptions = new FormatterOptions(); |
||
| 66 | $timestampFormatterOptions->setOption( |
||
| 67 | TimeFormatter::OPT_TIME_ISO_FORMATTER, |
||
| 68 | $this->getTimestampFormatter() |
||
| 69 | ); |
||
| 70 | |||
| 71 | $tests = array( |
||
| 72 | '2013-07-16' => array( |
||
| 73 | '+2013-07-16T00:00:00Z', |
||
| 74 | ), |
||
| 75 | |||
| 76 | // Custom timestamp formatter |
||
| 77 | '<timestamp>' => array( |
||
| 78 | '+2013-07-16T00:00:00Z', |
||
| 79 | TimeValue::PRECISION_DAY, |
||
| 80 | $gregorian, |
||
| 81 | $timestampFormatterOptions, |
||
| 82 | ), |
||
| 83 | |||
| 84 | // Different calendar models |
||
| 85 | '1701-12-14' => array( |
||
| 86 | '+1701-12-14T00:00:00Z', |
||
| 87 | TimeValue::PRECISION_DAY, |
||
| 88 | $julian, |
||
| 89 | ), |
||
| 90 | '1702-12-14' => array( |
||
| 91 | '+1702-12-14T00:00:00Z', |
||
| 92 | TimeValue::PRECISION_DAY, |
||
| 93 | 'Stardate', |
||
| 94 | ), |
||
| 95 | |||
| 96 | // Different years |
||
| 97 | "\xE2\x88\x9210000-01-01" => array( |
||
| 98 | '-010000-01-01T00:00:00Z', |
||
| 99 | ), |
||
| 100 | "\xE2\x88\x920001-01-01" => array( |
||
| 101 | '-1-01-01T00:00:00Z', |
||
| 102 | ), |
||
| 103 | "\xE2\x88\x920100-01-01" => array( |
||
| 104 | '-100-01-01T00:00:00Z', |
||
| 105 | ), |
||
| 106 | "\xE2\x88\x920000-01-01" => array( |
||
| 107 | '-0-01-01T00:00:00Z', |
||
| 108 | ), |
||
| 109 | '0000-01-01' => array( |
||
| 110 | '+0-01-01T00:00:00Z', |
||
| 111 | ), |
||
| 112 | '0001-01-01' => array( |
||
| 113 | '+1-01-01T00:00:00Z', |
||
| 114 | ), |
||
| 115 | '0100-01-01' => array( |
||
| 116 | '+100-01-01T00:00:00Z', |
||
| 117 | ), |
||
| 118 | '10000-01-01' => array( |
||
| 119 | '+010000-01-01T00:00:00Z', |
||
| 120 | ), |
||
| 121 | |||
| 122 | // Different precisions |
||
| 123 | '2000' => array( |
||
| 124 | '+2000-01-01T00:00:00Z', |
||
| 125 | TimeValue::PRECISION_YEAR1G, |
||
| 126 | ), |
||
| 127 | '2008' => array( |
||
| 128 | '+2008-01-08T00:00:00Z', |
||
| 129 | TimeValue::PRECISION_YEAR10, |
||
| 130 | ), |
||
| 131 | '2009' => array( |
||
| 132 | '+2009-01-09T00:00:00Z', |
||
| 133 | TimeValue::PRECISION_YEAR, |
||
| 134 | ), |
||
| 135 | '2010-07' => array( |
||
| 136 | '+2010-07-10T00:00:00Z', |
||
| 137 | TimeValue::PRECISION_MONTH, |
||
| 138 | ), |
||
| 139 | '2011-07-11' => array( |
||
| 140 | '+2011-07-11T00:00:00Z', |
||
| 141 | TimeValue::PRECISION_DAY, |
||
| 142 | ), |
||
| 143 | '2012-07-12T00' => array( |
||
| 144 | '+2012-07-12T00:00:00Z', |
||
| 145 | TimeValue::PRECISION_HOUR, |
||
| 146 | ), |
||
| 147 | '2013-07-13T00:00' => array( |
||
| 148 | '+2013-07-13T00:00:00Z', |
||
| 149 | TimeValue::PRECISION_MINUTE, |
||
| 150 | ), |
||
| 151 | '2014-07-14T00:00:00' => array( |
||
| 152 | '+2014-07-14T00:00:00Z', |
||
| 153 | TimeValue::PRECISION_SECOND, |
||
| 154 | ), |
||
| 155 | ); |
||
| 156 | |||
| 157 | $argLists = array(); |
||
| 158 | |||
| 159 | foreach ( $tests as $expected => $args ) { |
||
| 160 | $timestamp = $args[0]; |
||
| 161 | $precision = isset( $args[1] ) ? $args[1] : TimeValue::PRECISION_DAY; |
||
| 162 | $calendarModel = isset( $args[2] ) ? $args[2] : $gregorian; |
||
| 163 | $options = isset( $args[3] ) ? $args[3] : $baseOptions; |
||
| 164 | |||
| 165 | $argLists[] = array( |
||
| 166 | new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ), |
||
| 167 | (string)$expected, |
||
| 168 | $options |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $argLists; |
||
| 173 | } |
||
| 174 | |||
| 176 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.