| Conditions | 14 |
| Paths | 61 |
| Total Lines | 71 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 71 | public function render() |
||
| 72 | { |
||
| 73 | $result = Yii::$app->cache->get($this->cacheKey); |
||
| 74 | if ($result) { |
||
| 75 | return $result; |
||
| 76 | } |
||
| 77 | $urls = $this->generateUrls(); |
||
| 78 | $parts = ceil(count($urls) / $this->maxSectionUrl); |
||
| 79 | if ($parts > 1) { |
||
| 80 | $xml = new XMLWriter(); |
||
| 81 | $xml->openMemory(); |
||
| 82 | $xml->startDocument('1.0', 'UTF-8'); |
||
| 83 | $xml->startElement('sitemapindex'); |
||
| 84 | $xml->writeAttribute('xmlns', $this->schemas['xmlns']); |
||
| 85 | for ($i = 1; $i <= $parts; $i++) { |
||
| 86 | $xml->startElement('sitemap'); |
||
| 87 | $xml->writeElement('loc', Url::to(['/' . $this->moduleId . '/web/index', 'id' =>$i], true)); |
||
| 88 | $xml->writeElement('lastmod', static::dateToW3C(time())); |
||
| 89 | $xml->endElement(); |
||
| 90 | } |
||
| 91 | $xml->endElement(); |
||
| 92 | $result[0]['xml'] = $xml->outputMemory(); |
||
| 93 | } |
||
| 94 | $urlItem = 0; |
||
| 95 | for ($i = 1; $i <= $parts; $i++) { |
||
| 96 | $xml = new XMLWriter(); |
||
| 97 | $xml->openMemory(); |
||
| 98 | $xml->startDocument('1.0', 'UTF-8'); |
||
| 99 | $xml->startElement('urlset'); |
||
| 100 | foreach ($this->schemas as $attr => $schemaUrl) { |
||
| 101 | $xml->writeAttribute($attr, $schemaUrl); |
||
| 102 | } |
||
| 103 | for (; ($urlItem < $i * $this->maxSectionUrl) && ($urlItem < count($urls)); $urlItem++) { |
||
| 104 | $xml->startElement('url'); |
||
| 105 | foreach ($urls[$urlItem] as $urlKey => $urlValue) { |
||
| 106 | if (is_array($urlValue)) { |
||
| 107 | switch ($urlKey) { |
||
| 108 | case 'news': |
||
| 109 | $namespace = 'news:'; |
||
| 110 | $xml->startElement($namespace.$urlKey); |
||
| 111 | static::hashToXML($urlValue, $xml, $namespace); |
||
| 112 | $xml->endElement(); |
||
| 113 | break; |
||
| 114 | case 'images': |
||
| 115 | $namespace = 'image:'; |
||
| 116 | foreach ($urlValue as $image) { |
||
| 117 | $xml->startElement($namespace.'image'); |
||
| 118 | static::hashToXML($image, $xml, $namespace); |
||
| 119 | $xml->endElement(); |
||
| 120 | } |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | $xml->writeElement($urlKey, $urlValue); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | $xml->endElement(); |
||
| 128 | } |
||
| 129 | |||
| 130 | $xml->endElement(); // urlset |
||
| 131 | $xml->endElement(); // document |
||
| 132 | $result[$i]['xml'] = $xml->outputMemory(); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($parts == 1) { |
||
| 136 | $result[0] = $result[1]; |
||
| 137 | unset($result[1]); |
||
| 138 | } |
||
| 139 | Yii::$app->cache->set($this->cacheKey, $result, $this->cacheExpire); |
||
| 140 | return $result; |
||
| 141 | } |
||
| 142 | |||
| 250 |