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