| Conditions | 25 |
| Paths | 1512 |
| Total Lines | 100 |
| Code Lines | 59 |
| Lines | 20 |
| Ratio | 20 % |
| 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 |
||
| 100 | public function trackLinksInField($fieldName) { |
||
| 101 | $record = $this->owner; |
||
| 102 | |||
| 103 | $linkedPages = array(); |
||
| 104 | $linkedFiles = array(); |
||
| 105 | |||
| 106 | $htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName); |
||
| 107 | $links = $this->parser->process($htmlValue); |
||
| 108 | |||
| 109 | // Highlight broken links in the content. |
||
| 110 | foreach ($links as $link) { |
||
| 111 | $classStr = trim($link['DOMReference']->getAttribute('class')); |
||
| 112 | if (!$classStr) { |
||
| 113 | $classes = array(); |
||
| 114 | } else { |
||
| 115 | $classes = explode(' ', $classStr); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Add or remove the broken class from the link, depending on the link status. |
||
| 119 | if ($link['Broken']) { |
||
| 120 | $classes = array_unique(array_merge($classes, array('ss-broken'))); |
||
| 121 | } else { |
||
| 122 | $classes = array_diff($classes, array('ss-broken')); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!empty($classes)) { |
||
| 126 | $link['DOMReference']->setAttribute('class', implode(' ', $classes)); |
||
| 127 | } else { |
||
| 128 | $link['DOMReference']->removeAttribute('class'); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $record->$fieldName = $htmlValue->getContent(); |
||
| 132 | |||
| 133 | // Populate link tracking for internal links & links to asset files. |
||
| 134 | foreach ($links as $link) { |
||
| 135 | switch ($link['Type']) { |
||
| 136 | case 'sitetree': |
||
| 137 | if ($link['Broken']) { |
||
| 138 | $record->HasBrokenLink = true; |
||
|
|
|||
| 139 | } else { |
||
| 140 | $linkedPages[] = $link['Target']; |
||
| 141 | } |
||
| 142 | break; |
||
| 143 | |||
| 144 | case 'file': |
||
| 145 | if ($link['Broken']) { |
||
| 146 | $record->HasBrokenFile = true; |
||
| 147 | } else { |
||
| 148 | $linkedFiles[] = $link['Target']; |
||
| 149 | } |
||
| 150 | break; |
||
| 151 | |||
| 152 | default: |
||
| 153 | if ($link['Broken']) { |
||
| 154 | $record->HasBrokenLink = true; |
||
| 155 | } |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Add file tracking for image references |
||
| 161 | if($images = $htmlValue->getElementsByTagName('img')) foreach($images as $img) { |
||
| 162 | // {@see HtmlEditorField} for data-fileid source |
||
| 163 | $fileID = $img->getAttribute('data-fileid'); |
||
| 164 | if(!$fileID) { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Assuming a local file is linked, check if it's valid |
||
| 169 | if($image = File::get()->byID($fileID)) { |
||
| 170 | $linkedFiles[] = $image->ID; |
||
| 171 | } else { |
||
| 172 | $record->HasBrokenFile = true; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // Update the "LinkTracking" many_many |
||
| 177 | View Code Duplication | if($record->ID && $record->manyManyComponent('LinkTracking') && ($tracker = $record->LinkTracking())) { |
|
| 178 | $tracker->removeByFilter(array( |
||
| 179 | sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
||
| 180 | => array($fieldName, $record->ID) |
||
| 181 | )); |
||
| 182 | |||
| 183 | if($linkedPages) foreach($linkedPages as $item) { |
||
| 184 | $tracker->add($item, array('FieldName' => $fieldName)); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | // Update the "ImageTracking" many_many |
||
| 189 | View Code Duplication | if($record->ID && $record->manyManyComponent('ImageTracking') && ($tracker = $record->ImageTracking())) { |
|
| 190 | $tracker->removeByFilter(array( |
||
| 191 | sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
||
| 192 | => array($fieldName, $record->ID) |
||
| 193 | )); |
||
| 194 | |||
| 195 | if($linkedFiles) foreach($linkedFiles as $item) { |
||
| 196 | $tracker->add($item, array('FieldName' => $fieldName)); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 338 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.