Conditions | 29 |
Paths | 363 |
Total Lines | 94 |
Code Lines | 69 |
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 |
||
15 | public function onBeforeWrite() |
||
16 | { |
||
17 | if ($this->stillNeedsToReplace && $this->owner instanceof Image && $this->owner->ID) { |
||
18 | $this->stillNeedsToReplace = false; |
||
19 | $oldObject = File::get()->byID($this->owner->ID); |
||
20 | $oldFileName = ""; |
||
21 | if ($oldObject->Name) { |
||
22 | $oldFileName = $oldObject->Name; |
||
23 | } |
||
24 | if ($oldFileName) { |
||
25 | $oldPath = ""; |
||
26 | if (isset($oldObject->Filename)) { |
||
27 | $oldPath = $oldObject->Filename; |
||
28 | } |
||
29 | if (!$oldPath) { |
||
30 | $oldPath = $oldObject->getFilename(); |
||
31 | } |
||
32 | $oldPath = str_replace($oldFileName, "", $oldPath); |
||
33 | $newFileName = $this->owner->Name; |
||
34 | $newPath = $this->owner->getRelativePath(); |
||
35 | $newPath = str_replace($newFileName, "", $newPath); |
||
36 | if (($oldFileName != $newFileName) || ($oldPath != $oldPath)) { |
||
37 | $checks = MetaTagCMSControlFileUse::get() |
||
38 | ->filter( |
||
39 | array( |
||
40 | "ConnectionType" => "DB", |
||
41 | "IsLiveVersion" => "0" |
||
42 | ) |
||
43 | ); |
||
44 | $siteTreeItemsToChange = SiteTree::get() |
||
45 | ->where("\"SiteTree_ImageTracking\".\"FileID\" = ".$this->owner->ID) |
||
46 | ->innerJoin("SiteTree_ImageTracking", "\"SiteTree_ImageTracking\".\"SiteTreeID\" = \"SiteTree\".\"ID\""); |
||
47 | if ($siteTreeItemsToChange && $siteTreeItemsToChange->count()) { |
||
48 | foreach ($siteTreeItemsToChange as $siteTreeItemToChange) { |
||
49 | $trackings = $siteTreeItemToChange->ImageTracking(); |
||
50 | if ($trackings && $trackings->count()) { |
||
51 | foreach ($trackings as $tracking) { |
||
52 | $array = array( |
||
53 | "DataObjectClassName" => "SiteTree", |
||
54 | "DataObjectFieldName" => $tracking->FieldName, |
||
55 | "FileID" => $tracking->ID, |
||
56 | "TrackedID" => $siteTreeItemToChange->ID, |
||
57 | "IsImageTracking" => true |
||
58 | ); |
||
59 | $item = new ArrayData($array); |
||
60 | $this->imageTrackingAddAgain[] = $array; |
||
61 | $checks->push($item); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | if ($checks && $checks->count()) { |
||
67 | foreach ($checks as $check) { |
||
68 | $className = $check->DataObjectClassName; |
||
69 | $fieldName = $check->DataObjectFieldName; |
||
70 | if (isset($check->IsImageTracking) && $check->IsImageTracking) { |
||
71 | $dosToChange = $className::get()->filter(array("ID" => $check->TrackedID)); |
||
72 | } else { |
||
73 | $dosToChange = $className::get()->where("LOCATE('$oldFileName', \"".$fieldName."\") > 0"); |
||
74 | } |
||
75 | if ($dosToChange && $dosToChange->count()) { |
||
76 | foreach ($dosToChange as $doToChange) { |
||
77 | $dom = new DOMDocument; |
||
78 | $dom->loadHTML('<?xml encoding="UTF-8">'.$doToChange->$fieldName); |
||
79 | foreach ($dom->getElementsByTagName('img') as $node) { |
||
80 | $oldSrc = $node->getAttribute('src'); |
||
81 | $newSrc = preg_replace('/'.str_replace("/", "\/", $oldPath).'(.*?)'.$oldFileName.'/', ''.$newPath.'$1'.$newFileName, $oldSrc); |
||
82 | if ($oldSrc != $newSrc) { |
||
83 | $oldFilePath = Director::baseFolder()."/".$oldSrc; |
||
84 | $newFilePath = Director::baseFolder()."/".$newSrc; |
||
85 | if (file_exists($oldFilePath) && !file_exists($newFilePath)) { |
||
86 | rename($oldFilePath, $newFilePath); |
||
87 | } |
||
88 | $node->setAttribute('src', $newSrc); |
||
89 | } |
||
90 | } |
||
91 | $doToChange->encoding = 'UTF-8'; |
||
92 | $doToChange->$fieldName = $dom->saveHTML(); |
||
93 | $data = preg_replace('/'.str_replace("/", "\/", $oldPath).'(.*?)'.$oldFileName.'/', ''.$newPath.'$1'.$newFileName, $doToChange->$fieldName); |
||
94 | //$this->owner->generateFormattedImage($format, $arg1, $arg2) |
||
95 | if ($doToChange instanceof SiteTree) { |
||
96 | $doToChange->writeToStage('Stage'); |
||
97 | $doToChange->publish('Stage', 'Live'); |
||
98 | } else { |
||
99 | $doToChange->write(); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
130 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.