Conditions | 10 |
Paths | 76 |
Total Lines | 80 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
73 | public function getDiffList($highestVersion = null, $limit = 100) |
||
74 | { |
||
75 | // This can leak secured content if it was protected via inherited setting. |
||
76 | // For now the users will need to be aware about this shortcoming. |
||
77 | $offset = $highestVersion ? "AND \"SiteTree_Versions\".\"Version\"<='".(int)$highestVersion."'" : ''; |
||
78 | // Get just enough elements for diffing. We need one more than desired to have something to compare to. |
||
79 | $qLimit = (int)$limit + 1; |
||
80 | $versions = $this->owner->allVersions( |
||
81 | "\"WasPublished\"='1' AND \"CanViewType\" IN ('Anyone', 'Inherit') $offset", |
||
82 | "\"SiteTree\".\"LastEdited\" DESC, \"SiteTree\".\"ID\" DESC", |
||
83 | $qLimit |
||
84 | ); |
||
85 | |||
86 | // Process the list to add the comparisons. |
||
87 | $changeList = new ArrayList(); |
||
88 | $previous = null; |
||
89 | $count = 0; |
||
90 | foreach ($versions as $version) { |
||
91 | $changed = false; |
||
92 | |||
93 | // Check if we have something to compare with. |
||
94 | if (isset($previous)) { |
||
95 | // Produce the diff fields for use in the template. |
||
96 | if ($version->Title != $previous->Title) { |
||
97 | $diffTitle = Diff::compareHTML($version->Title, $previous->Title); |
||
98 | |||
99 | $version->DiffTitle = DBField::create_field('HTMLText', null); |
||
100 | $version->DiffTitle->setValue( |
||
101 | sprintf( |
||
102 | '<div><em>%s</em> ' . $diffTitle . '</div>', |
||
103 | _t(__CLASS__ . '.TITLECHANGED', 'Title has changed:') |
||
104 | ) |
||
105 | ); |
||
106 | $changed = true; |
||
107 | } |
||
108 | |||
109 | if ($version->Content != $previous->Content) { |
||
110 | $diffContent = Diff::compareHTML($version->Content, $previous->Content); |
||
111 | |||
112 | $version->DiffContent = DBField::create_field('HTMLText', null); |
||
113 | $version->DiffContent->setValue('<div>'.$diffContent.'</div>'); |
||
114 | $changed = true; |
||
115 | } |
||
116 | |||
117 | // Copy the link so it can be cached. |
||
118 | $oldPage = $version->getField('object'); |
||
119 | if (!$oldPage instanceof SiteTree) { |
||
120 | // We only need enough info to generate the link... |
||
121 | $oldPage = SiteTree::create([ |
||
122 | 'ID' => $oldPage->ID, |
||
123 | 'URLSegment' => $oldPage->URLSegment, |
||
124 | 'ParentID' => $oldPage->ParentID |
||
125 | ]); |
||
126 | } |
||
127 | $version->GeneratedLink = $oldPage->AbsoluteLink(); |
||
128 | } |
||
129 | |||
130 | // Omit the versions that haven't been visibly changed (only takes the above fields into consideration). |
||
131 | if ($changed) { |
||
132 | $changeList->push($version); |
||
133 | $count++; |
||
134 | } |
||
135 | |||
136 | // Store the last version for comparison. |
||
137 | $previous = $version; |
||
138 | } |
||
139 | |||
140 | // Make sure enough diff items have been generated to satisfy the $limit. If we ran out, add the final, |
||
141 | // non-diffed item (the initial version). This will also work for a single-diff request: if we are requesting |
||
142 | // a diff on the initial version we will just get that version, verbatim. |
||
143 | if ($previous && $versions->count()<$qLimit) { |
||
144 | $first = clone($previous); |
||
145 | $first->DiffContent = DBField::create_field('HTMLText', null); |
||
146 | $first->DiffContent->setValue('<div>' . $first->Content . '</div>'); |
||
147 | // Copy the link so it can be cached. |
||
148 | $first->GeneratedLink = $first->AbsoluteLink(); |
||
149 | $changeList->push($first); |
||
150 | } |
||
151 | |||
152 | return $changeList; |
||
153 | } |
||
240 |