Conditions | 8 |
Paths | 8 |
Total Lines | 76 |
Lines | 76 |
Ratio | 100 % |
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 |
||
93 | View Code Duplication | public function load($path = null, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
|
|
|||
94 | { |
||
95 | |||
96 | // initialize the core config data |
||
97 | $coreConfigData = array( |
||
98 | MemberNames::PATH => $path, |
||
99 | MemberNames::SCOPE => $scope, |
||
100 | MemberNames::SCOPE_ID => $scopeId |
||
101 | ); |
||
102 | |||
103 | // generate the UID from the passed data |
||
104 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
||
105 | |||
106 | // iterate over the core config data and try to find the requested configuration value |
||
107 | if (isset($this->coreConfigData[$uniqueIdentifier])) { |
||
108 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
||
109 | } |
||
110 | |||
111 | // query whether or not we've to query for the configuration value on fallback level 'websites' also |
||
112 | if ($scope === ScopeKeys::SCOPE_STORES) { |
||
113 | // query whether or not the website with the passed ID is available |
||
114 | foreach ($this->storeWebsites as $storeWebsite) { |
||
115 | if ($storeWebsite[MemberNames::WEBSITE_ID] === $scopeId) { |
||
116 | // replace scope with 'websites' and website ID |
||
117 | $coreConfigData = array_merge( |
||
118 | $coreConfigData, |
||
119 | array( |
||
120 | MemberNames::SCOPE => ScopeKeys::SCOPE_WEBSITES, |
||
121 | MemberNames::SCOPE_ID => $storeWebsite[MemberNames::WEBSITE_ID] |
||
122 | ) |
||
123 | ); |
||
124 | |||
125 | // generate the UID from the passed data, merged with the 'websites' scope and ID |
||
126 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
||
127 | |||
128 | // query whether or not, the configuration value on 'websites' level |
||
129 | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
||
130 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // replace scope with 'default' and scope ID '0' |
||
137 | $coreConfigData = array_merge( |
||
138 | $coreConfigData, |
||
139 | array( |
||
140 | MemberNames::SCOPE => ScopeKeys::SCOPE_DEFAULT, |
||
141 | MemberNames::SCOPE_ID => 0 |
||
142 | ) |
||
143 | ); |
||
144 | |||
145 | // generate the UID from the passed data, merged with the 'default' scope and ID 0 |
||
146 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
||
147 | |||
148 | // query whether or not, the configuration value on 'default' level |
||
149 | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
||
150 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
||
151 | } |
||
152 | |||
153 | // if not, return the passed default value |
||
154 | if ($default !== null) { |
||
155 | return $default; |
||
156 | } |
||
157 | |||
158 | // throw an exception if no value can be found |
||
159 | // in the Magento configuration |
||
160 | throw new \Exception( |
||
161 | sprintf( |
||
162 | 'Can\'t find a value for configuration "%s-%s-%d" in "core_config_data"', |
||
163 | $path, |
||
164 | $scope, |
||
165 | $scopeId |
||
166 | ) |
||
167 | ); |
||
168 | } |
||
169 | } |
||
170 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.