Conditions | 10 |
Paths | 16 |
Total Lines | 47 |
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 |
||
53 | public function onBeforeWrite() |
||
54 | { |
||
55 | parent::onBeforeWrite(); |
||
56 | $imageAdded = false; |
||
57 | if ($this->AutomaticallyIncludedFolderID) { |
||
58 | if ($folder = Folder::get()->byID($this->AutomaticallyIncludedFolderID)) { |
||
59 | if ($files = Image::get()->filter("ParentID", $folder->ID)) { |
||
60 | foreach ($files as $file) { |
||
61 | if (ImageGalleryEntry::get()->filter(array("ImageID" => $file->ID, "ParentID" => $this->ID))->count()) { |
||
62 | //do nothing |
||
63 | //debug::log("already exists"); |
||
64 | } else { |
||
65 | $ImageGalleryEntry = new ImageGalleryEntry(); |
||
66 | $ImageGalleryEntry->Title = $file->Title; |
||
67 | $ImageGalleryEntry->ImageID = $file->ID; |
||
68 | $ImageGalleryEntry->ParentID = $this->ID; |
||
69 | $ImageGalleryEntry->write(); |
||
70 | $imageAdded = true; |
||
71 | //debug::log("writing"); |
||
72 | } |
||
73 | } |
||
74 | } else { |
||
75 | //debug::log("D"); |
||
76 | } |
||
77 | } else { |
||
78 | //debug::log("C"); |
||
79 | } |
||
80 | } else { |
||
81 | //debug::log("B"); |
||
82 | } |
||
83 | if ($ImageGalleryEntries = ImageGalleryEntry::get()->filter(array("ParentID" => $this->ID))) { |
||
84 | foreach ($ImageGalleryEntries as $ImageGalleryEntry) { |
||
85 | $image = Image::get() |
||
86 | ->filter(array("ID" => $ImageGalleryEntry->ImageID)) |
||
87 | ->exclude(array("Title" => $ImageGalleryEntry->Title)) |
||
88 | ->First(); |
||
89 | if ($image) { |
||
90 | $image->Title = $image->Name = $ImageGalleryEntry->Title; |
||
91 | $image->write(); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | if ($imageAdded) { |
||
96 | //LeftAndMain::force_reload(); |
||
97 | } |
||
98 | $this->AutomaticallyIncludedFolderID = 0; |
||
99 | } |
||
100 | |||
150 |
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.