| Conditions | 16 |
| Paths | 224 |
| Total Lines | 74 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 declare(strict_types=1); |
||
| 76 | private function getDateFromSchemaOrg(): ?\DateTime { |
||
| 77 | $dt = null; |
||
| 78 | |||
| 79 | // Check for HTML tags (<meta>, <time>, etc.) |
||
| 80 | $nodes = $this->article()->getRawDoc()->find('*[itemprop="datePublished"]'); |
||
| 81 | |||
| 82 | /* @var $node Element */ |
||
| 83 | foreach ($nodes as $node) { |
||
| 84 | try { |
||
| 85 | if ($node->hasAttribute('datetime')) { |
||
| 86 | $dt = new \DateTime($node->getAttribute('datetime')); |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | if ($node->hasAttribute('content')) { |
||
| 90 | $dt = new \DateTime($node->getAttribute('content')); |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | catch (\Exception $e) { |
||
| 95 | // Do nothing here in case the node has unrecognizable date information. |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!is_null($dt)) { |
||
| 100 | return $dt; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Check for JSON-LD |
||
| 104 | $nodes = $this->article()->getRawDoc()->find('script[type="application/ld+json"]'); |
||
| 105 | |||
| 106 | /* @var $node Element */ |
||
| 107 | foreach ($nodes as $node) { |
||
| 108 | try { |
||
| 109 | $json = json_decode($node->text()); |
||
| 110 | |||
| 111 | // Extract the published date from the Schema.org meta data |
||
| 112 | if (isset($json->{'@graph'}) && is_array($json->{'@graph'})) { |
||
| 113 | foreach ($json->{'@graph'} as $graphData) { |
||
| 114 | $graphData = (array)$graphData; |
||
| 115 | |||
| 116 | if (!isset($graphData['datePublished'])) { |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $date = @$graphData['datePublished']; |
||
| 121 | |||
| 122 | try { |
||
| 123 | $dt = new \DateTime($date); |
||
| 124 | } catch (\Error $ex) { |
||
| 125 | // Do nothing here in case the node has unrecognizable date information. |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | if (isset($json->datePublished)) { |
||
| 131 | $date = is_array($json->datePublished) |
||
| 132 | ? array_shift($json->datePublished) |
||
| 133 | : $json->datePublished; |
||
| 134 | |||
| 135 | try { |
||
| 136 | $dt = new \DateTime($date); |
||
| 137 | } catch (\Error $ex) { |
||
| 138 | // Do nothing here in case the node has unrecognizable date information. |
||
| 139 | } |
||
| 140 | |||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | catch (\Exception $e) { |
||
| 145 | // Do nothing here in case the node has unrecognizable date information. |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return $dt; |
||
| 150 | } |
||
| 293 |