Conditions | 22 |
Paths | 88 |
Total Lines | 86 |
Code Lines | 61 |
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 |
||
59 | public function getCampaignMonitorSignupField($listPage = null, $fieldName = "", $fieldTitle = "") |
||
60 | { |
||
61 | if (!is_object($listPage)) { |
||
62 | $listPage = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listPage))->first(); |
||
63 | } |
||
64 | $field = null; |
||
65 | if (!$fieldName) { |
||
66 | $fieldName = Config::inst()->get("CampaignMonitorMemberDOD", "campaign_monitor_signup_fieldname"); |
||
67 | } |
||
68 | $api = $this->getCMAPI(); |
||
69 | $currentValues = null; |
||
70 | if ($listPage) { |
||
71 | if ($listPage->ReadyToReceiveSubscribtions()) { |
||
72 | $currentSelection = "Subscribe"; |
||
73 | $optionArray = array(); |
||
74 | $optionArray["Subscribe"] = _t("CampaignMonitorSignupPage.SUBSCRIBE_TO", "subscribe to")." ".$listPage->getListTitle(); |
||
75 | $optionArray["Unsubscribe"] = _t("CampaignMonitorSignupPage.UNSUBSCRIBE_FROM", "unsubscribe from ")." ".$listPage->getListTitle(); |
||
76 | if ($this->owner->exists()) { |
||
77 | if ($api->getSubscriberCanReceiveEmailsForThisList($listPage->ListID, $this->owner)) { |
||
78 | $currentValues = $api->getSubscriber($listPage->ListID, $this->owner); |
||
79 | //$currentSelection = "Unsubscribe"; |
||
80 | } |
||
81 | } |
||
82 | if (!$fieldTitle) { |
||
83 | $fieldTitle = _t("CampaignMonitorSignupPage.SIGNUP_FOR", "Sign up for ")." ".$listPage->getListTitle(); |
||
84 | } |
||
85 | $subscribeField = OptionsetField::create($fieldName, $fieldTitle, $optionArray, $currentSelection); |
||
86 | $field = CompositeField::create($subscribeField); |
||
87 | $field->addExtraClass("CMFieldsCustomFieldsHolder"); |
||
88 | //add custom fields |
||
89 | $linkedMemberFields = Config::inst()->get("CampaignMonitorMemberDOD", "custom_fields_member_field_or_method_map"); |
||
90 | $customFields = $listPage->CampaignMonitorCustomFields()->filter(array("Visible" => 1)); |
||
91 | foreach ($customFields as $customField) { |
||
92 | $valueSet = false; |
||
93 | $customFormField = $customField->getFormField("CMCustomField"); |
||
94 | if ($currentValues && isset($currentValues->CustomFields)) { |
||
95 | foreach ($currentValues->CustomFields as $customFieldObject) { |
||
96 | if ($customFieldObject->Key == $customField->Title) { |
||
97 | if ($value = $customFieldObject->Value) { |
||
98 | $valueSet = true; |
||
99 | } |
||
100 | $customFormField->setValue($value); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | if (isset($linkedMemberFields[$customFormField->Code]) && !$valueSet) { |
||
105 | $fieldOrMethod = $linkedMemberFields[$custom->Code]; |
||
106 | if ($this->owner->hasMethod($fieldOrMethod)) { |
||
107 | $value = $this->owner->$fieldOrMethod(); |
||
108 | } else { |
||
109 | $value = $this->owner->$fieldOrMethod; |
||
110 | } |
||
111 | if ($value) { |
||
112 | $customFormField->setValue($value); |
||
113 | } |
||
114 | } |
||
115 | $field->push($customFormField); |
||
116 | } |
||
117 | } |
||
118 | } else { |
||
119 | if (!$fieldTitle) { |
||
120 | $fieldTitle = _t("CampaignMonitorMemberDOD.NEWSLETTERSIGNUP", "Newsletter sign-up"); |
||
121 | } |
||
122 | $lists = CampaignMonitorSignupPage::get_ready_ones(); |
||
123 | $array = array(); |
||
124 | foreach ($lists as $list) { |
||
125 | $array[$list->ListID] = $list->getListTitle(); |
||
126 | } |
||
127 | if (count($array)) { |
||
128 | $field = new CheckboxSetField( |
||
129 | $fieldName, |
||
130 | $fieldTitle, |
||
131 | $array |
||
132 | ); |
||
133 | $field->setDefaultItems($this->owner->CampaignMonitorSignupPageIDs()); |
||
134 | } |
||
135 | } |
||
136 | if (!$field) { |
||
137 | $field = ReadonlyField::create( |
||
138 | $fieldName, |
||
139 | $fieldTitle, |
||
140 | _t("CampaignMonitorMemberDOD.NO_LISTS_AVAILABLE", "No lists available right now. Please come back soon.") |
||
141 | ); |
||
142 | } |
||
143 | return $field; |
||
144 | } |
||
145 | |||
327 |
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.