| Conditions | 13 |
| Paths | 205 |
| Total Lines | 64 |
| Code Lines | 30 |
| 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 declare(strict_types=1); |
||
| 196 | private function getDateFromParsely(): ?\DateTime { |
||
| 197 | $dt = null; |
||
| 198 | |||
| 199 | // JSON-LD |
||
| 200 | $nodes = $this->article()->getRawDoc()->find('script[type="application/ld+json"]'); |
||
| 201 | |||
| 202 | /* @var $node Element */ |
||
| 203 | foreach ($nodes as $node) { |
||
| 204 | try { |
||
| 205 | $json = json_decode($node->text()); |
||
| 206 | if (isset($json->dateCreated)) { |
||
| 207 | $dt = new \DateTime($json->dateCreated); |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | catch (\Exception $e) { |
||
| 212 | // Do nothing here in case the node has unrecognizable date information. |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | if (!is_null($dt)) { |
||
| 217 | return $dt; |
||
| 218 | } |
||
| 219 | |||
| 220 | // <meta> tags |
||
| 221 | $nodes = $this->article()->getRawDoc()->find('meta[name="parsely-pub-date"]'); |
||
| 222 | |||
| 223 | /* @var $node Element */ |
||
| 224 | foreach ($nodes as $node) { |
||
| 225 | try { |
||
| 226 | if ($node->hasAttribute('content')) { |
||
| 227 | $dt = new \DateTime($node->getAttribute('content')); |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | catch (\Exception $e) { |
||
| 232 | // Do nothing here in case the node has unrecognizable date information. |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | if (!is_null($dt)) { |
||
| 237 | return $dt; |
||
| 238 | } |
||
| 239 | |||
| 240 | // parsely-page |
||
| 241 | $nodes = $this->article()->getRawDoc()->find('meta[name="parsely-page"]'); |
||
| 242 | |||
| 243 | /* @var $node Element */ |
||
| 244 | foreach ($nodes as $node) { |
||
| 245 | try { |
||
| 246 | if ($node->hasAttribute('content')) { |
||
| 247 | $json = json_decode($node->getAttribute('content')); |
||
| 248 | if (isset($json->pub_date)) { |
||
| 249 | $dt = new \DateTime($json->pub_date); |
||
| 250 | break; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | catch (\Exception $e) { |
||
| 255 | // Do nothing here in case the node has unrecognizable date information. |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | return $dt; |
||
| 260 | } |
||
| 262 |