Conditions | 25 |
Paths | 1512 |
Total Lines | 100 |
Code Lines | 59 |
Lines | 20 |
Ratio | 20 % |
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 |
||
81 | public function trackLinksInField($fieldName) { |
||
82 | $record = $this->owner; |
||
83 | |||
84 | $linkedPages = array(); |
||
85 | $linkedFiles = array(); |
||
86 | |||
87 | $htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName); |
||
88 | $links = $this->parser->process($htmlValue); |
||
89 | |||
90 | // Highlight broken links in the content. |
||
91 | foreach ($links as $link) { |
||
92 | $classStr = trim($link['DOMReference']->getAttribute('class')); |
||
93 | if (!$classStr) { |
||
94 | $classes = array(); |
||
95 | } else { |
||
96 | $classes = explode(' ', $classStr); |
||
97 | } |
||
98 | |||
99 | // Add or remove the broken class from the link, depending on the link status. |
||
100 | if ($link['Broken']) { |
||
101 | $classes = array_unique(array_merge($classes, array('ss-broken'))); |
||
102 | } else { |
||
103 | $classes = array_diff($classes, array('ss-broken')); |
||
104 | } |
||
105 | |||
106 | if (!empty($classes)) { |
||
107 | $link['DOMReference']->setAttribute('class', implode(' ', $classes)); |
||
108 | } else { |
||
109 | $link['DOMReference']->removeAttribute('class'); |
||
110 | } |
||
111 | } |
||
112 | $record->$fieldName = $htmlValue->getContent(); |
||
113 | |||
114 | // Populate link tracking for internal links & links to asset files. |
||
115 | foreach ($links as $link) { |
||
116 | switch ($link['Type']) { |
||
117 | case 'sitetree': |
||
118 | if ($link['Broken']) { |
||
119 | $record->HasBrokenLink = true; |
||
|
|||
120 | } else { |
||
121 | $linkedPages[] = $link['Target']; |
||
122 | } |
||
123 | break; |
||
124 | |||
125 | case 'file': |
||
126 | if ($link['Broken']) { |
||
127 | $record->HasBrokenFile = true; |
||
128 | } else { |
||
129 | $linkedFiles[] = $link['Target']; |
||
130 | } |
||
131 | break; |
||
132 | |||
133 | default: |
||
134 | if ($link['Broken']) { |
||
135 | $record->HasBrokenLink = true; |
||
136 | } |
||
137 | break; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // Add file tracking for image references |
||
142 | if($images = $htmlValue->getElementsByTagName('img')) foreach($images as $img) { |
||
143 | // {@see HtmlEditorField} for data-fileid source |
||
144 | $fileID = $img->getAttribute('data-fileid'); |
||
145 | if(!$fileID) { |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | // Assuming a local file is linked, check if it's valid |
||
150 | if($image = File::get()->byID($fileID)) { |
||
151 | $linkedFiles[] = $image->ID; |
||
152 | } else { |
||
153 | $record->HasBrokenFile = true; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // Update the "LinkTracking" many_many |
||
158 | View Code Duplication | if($record->ID && $record->manyManyComponent('LinkTracking') && ($tracker = $record->LinkTracking())) { |
|
159 | $tracker->removeByFilter(array( |
||
160 | sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
||
161 | => array($fieldName, $record->ID) |
||
162 | )); |
||
163 | |||
164 | if($linkedPages) foreach($linkedPages as $item) { |
||
165 | $tracker->add($item, array('FieldName' => $fieldName)); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | // Update the "ImageTracking" many_many |
||
170 | View Code Duplication | if($record->ID && $record->manyManyComponent('ImageTracking') && ($tracker = $record->ImageTracking())) { |
|
171 | $tracker->removeByFilter(array( |
||
172 | sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
||
173 | => array($fieldName, $record->ID) |
||
174 | )); |
||
175 | |||
176 | if($linkedFiles) foreach($linkedFiles as $item) { |
||
177 | $tracker->add($item, array('FieldName' => $fieldName)); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
319 |
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@property
annotation 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.