Conditions | 29 |
Paths | 8894 |
Total Lines | 124 |
Code Lines | 80 |
Lines | 12 |
Ratio | 9.68 % |
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 |
||
58 | public function execute(XmlConfigDomDocument $document) |
||
59 | { |
||
60 | $dayMap = array( |
||
61 | 'sun' => DateDefinitions::SUNDAY, |
||
62 | 'mon' => DateDefinitions::MONDAY, |
||
63 | 'tue' => DateDefinitions::TUESDAY, |
||
64 | 'wed' => DateDefinitions::WEDNESDAY, |
||
65 | 'thu' => DateDefinitions::THURSDAY, |
||
66 | 'fri' => DateDefinitions::FRIDAY, |
||
67 | 'sat' => DateDefinitions::SATURDAY, |
||
68 | ); |
||
69 | |||
70 | $dataTree = $document->documentElement; |
||
71 | |||
72 | $parsedData = array(); |
||
73 | $data = array(); |
||
74 | |||
75 | foreach ($dataTree->getChild('currencyData') as $currencyNode) { |
||
|
|||
76 | if ($currencyNode->localName == 'fractions') { |
||
77 | foreach ($currencyNode as $info) { |
||
78 | $data['fractions'][$info->getAttribute('iso4217')] = array( |
||
79 | 'digits' => $info->getAttribute('digits', 2), |
||
80 | 'rounding' => $info->getAttribute('rounding', 1), |
||
81 | ); |
||
82 | } |
||
83 | } elseif ($currencyNode->localName == 'region') { |
||
84 | foreach ($currencyNode as $currency) { |
||
85 | if ($currency->getName() == 'currency') { |
||
86 | $data['territories'][$currencyNode->getAttribute('iso3166')]['currencies'][$currency->getAttribute('iso4217')] = array( |
||
87 | 'currency' => $currency->getAttribute('iso4217'), |
||
88 | 'from' => $currency->getAttribute('from'), |
||
89 | 'to' => $currency->getAttribute('to'), |
||
90 | ); |
||
91 | } else { |
||
92 | throw new AgaviException('Invalid tag ' . $currency->localName . ' in region tag'); |
||
93 | } |
||
94 | } |
||
95 | } else { |
||
96 | throw new AgaviException('Invalid tag ' . $currencyNode->localName . ' in currencyData tag'); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | foreach ($dataTree->getChild('territoryContainment') as $group) { |
||
101 | if ($group->localName == 'group') { |
||
102 | $data['territoryContainment'][$group->getAttribute('type')] = explode(' ', $group->getAttribute('contains')); |
||
103 | } else { |
||
104 | throw new AgaviException('Invalid tag ' . $group->localName . ' in territoryContainment tag'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | foreach ($dataTree->getChild('languageData') as $language) { |
||
109 | if ($language->localName == 'language') { |
||
110 | $lang = $language->getAttribute('type'); |
||
111 | $scripts = explode(' ', $language->getAttribute('scripts')); |
||
112 | $territories = explode(' ', $language->getAttribute('territories')); |
||
113 | $alt = $language->getAttribute('alt', 'primary'); |
||
114 | |||
115 | foreach ($scripts as $script) { |
||
116 | $parsedData['languages'][$lang][$alt][$script] = $territories; |
||
117 | } |
||
118 | |||
119 | foreach ($territories as $territory) { |
||
120 | $data['territories'][$territory]['languages'][$alt][$lang] = $scripts; |
||
121 | } |
||
122 | } else { |
||
123 | throw new AgaviException('Invalid tag ' . $language->getName() . ' in languageData tag'); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | // set the default calendar to gregorian for all territories first |
||
128 | foreach ($data['territories'] as &$territoryData) { |
||
129 | $territoryData['calendar'] = 'gregorian'; |
||
130 | } |
||
131 | |||
132 | foreach ($dataTree->getChild('calendarData') as $calendar) { |
||
133 | $type = $calendar->getAttribute('type'); |
||
134 | foreach (explode(' ', $calendar->getAttribute('territories')) as $territory) { |
||
135 | $data['territories'][$territory]['calendar'] = $type; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | foreach ($dataTree->getChild('weekData') as $entry) { |
||
140 | $entryName = $entry->localName; |
||
141 | if ($entryName == 'minDays') { |
||
142 | View Code Duplication | foreach (explode(' ', $entry->getAttribute('territories')) as $territory) { |
|
143 | $countries = $this->resolveTerritoryToCountries($data['territoryContainment'], $territory); |
||
144 | foreach ($countries as $country) { |
||
145 | $data['territories'][$country]['week'][$entryName] = $entry->getAttribute('count'); |
||
146 | } |
||
147 | } |
||
148 | } elseif ($entryName == 'firstDay' || $entryName == 'weekendStart' || $entryName == 'weekendEnd') { |
||
149 | if (!$entry->hasAttribute('alt')) { |
||
150 | View Code Duplication | foreach (explode(' ', $entry->getAttribute('territories')) as $territory) { |
|
151 | $countries = $this->resolveTerritoryToCountries($data['territoryContainment'], $territory); |
||
152 | foreach ($countries as $country) { |
||
153 | $data['territories'][$country]['week'][$entryName] = $dayMap[$entry->getAttribute('day')]; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } else { |
||
158 | throw new AgaviException('Invalid tag ' . $entry->localName . ' in weekData tag'); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $data['timezones'] = array('territories' => array(), 'multiZones' => array()); |
||
163 | foreach (explode(' ', $dataTree->getChild('timezoneData')->getChild('zoneFormatting')->getAttribute('multizone')) as $zone) { |
||
164 | $data['timezones']['multiZones'][$zone] = true; |
||
165 | } |
||
166 | |||
167 | foreach ($dataTree->getChild('timezoneData')->getChild('zoneFormatting') as $zoneItem) { |
||
168 | if ($zoneItem->localName == 'zoneItem') { |
||
169 | $zone = $zoneItem->getAttribute('type'); |
||
170 | $territory = $zoneItem->getAttribute('territory'); |
||
171 | $data['timezones']['territories'][$zone] = $territory; |
||
172 | } else { |
||
173 | throw new AgaviException('Invalid tag ' . $language->localName . ' in zoneFormatting tag'); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | $code = array(); |
||
178 | $code[] = 'return ' . var_export($data, true) . ';'; |
||
179 | |||
180 | return $this->generate($code, $document->documentURI); |
||
181 | } |
||
182 | |||
212 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: