Total Complexity | 43 |
Total Lines | 254 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like TParameterizeBehavior often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TParameterizeBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class TParameterizeBehavior extends \Prado\Util\TBehavior |
||
39 | { |
||
40 | /** |
||
41 | * @var string the key to the application parameter |
||
42 | */ |
||
43 | private $_parameter; |
||
44 | |||
45 | /** |
||
46 | * @var bool whether or not a null parameter value should be set on the property |
||
47 | */ |
||
48 | private $_validNullValue; |
||
49 | |||
50 | /** |
||
51 | * @var string the key to the application parameter |
||
52 | */ |
||
53 | protected $_property; |
||
54 | |||
55 | /** |
||
56 | * @var string the default value of the property if there is no Parameter |
||
57 | */ |
||
58 | protected $_defaultValue; |
||
59 | |||
60 | /** |
||
61 | * @var bool should the value be localized |
||
62 | */ |
||
63 | protected $_localize; |
||
64 | |||
65 | /** |
||
66 | * @var object {@link TMapRouteBehavior} that routes changes from the parameter to the property |
||
67 | */ |
||
68 | private $_paramBehavior; |
||
69 | |||
70 | /** |
||
71 | * @var string the name of the installed behavior. |
||
72 | */ |
||
73 | protected $_routeBehaviorName; |
||
74 | |||
75 | /** @var bool is the behavior attached */ |
||
76 | private $_initialized = false; |
||
77 | |||
78 | /** |
||
79 | * This method sets the Owner Property to the Application Parameter of Parameter. When |
||
80 | * {@link getRouteBehaviorName} is set, a {@link TMapRouteBehavior} is attached to |
||
81 | * the Application Parameter on the key so any changes are also routed to the Property. |
||
82 | * @param object $owner the object to which this behavior is being attached |
||
83 | * @throws TConfigurationException when missing the parameter, property, or property is not able to set |
||
84 | */ |
||
85 | public function attach($owner) |
||
86 | { |
||
87 | parent::attach($owner); |
||
88 | |||
89 | if (!$this->getEnabled()) { |
||
90 | $this->_initialized = true; |
||
91 | return; |
||
92 | } |
||
93 | if (!$this->_parameter) { |
||
94 | throw new TConfigurationException('parameterizebehavior_no_parameter'); |
||
95 | } |
||
96 | if (!$this->_property) { |
||
97 | throw new TConfigurationException('parameterizebehavior_no_property'); |
||
98 | } |
||
99 | if (!$owner->canSetProperty($this->_property)) { |
||
100 | if ($owner->canGetProperty($this->_property)) { |
||
101 | throw new TConfigurationException('parameterizebehavior_owner_get_only_property', $this->_property); |
||
102 | } else { |
||
103 | throw new TConfigurationException('parameterizebehavior_owner_has_no_property', $this->_property); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $appParams = Prado::getApplication()->getParameters(); |
||
108 | if (($value = $appParams->itemAt($this->_parameter)) !== null || $this->getValidNullValue()) { |
||
109 | if ($this->_localize && $value && is_string($value)) { |
||
110 | $value = Prado::localize($value); |
||
111 | } |
||
112 | $owner->setSubProperty($this->_property, $value); |
||
113 | } elseif ($this->_defaultValue !== null) { |
||
114 | $value = $this->_defaultValue; |
||
115 | if ($this->_localize && is_string($value)) { |
||
116 | $value = Prado::localize($value); |
||
117 | } |
||
118 | $owner->setSubProperty($this->_property, $value); |
||
119 | } |
||
120 | $this->_initialized = true; |
||
121 | |||
122 | $this->attachParamMapRoute(); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * |
||
127 | * |
||
128 | * @param $owner The owner to receive parameter changes |
||
|
|||
129 | */ |
||
130 | protected function attachParamMapRoute() |
||
131 | { |
||
132 | if ($this->_routeBehaviorName) { |
||
133 | $owner = $this->getOwner(); |
||
134 | if ($this->_localize) { |
||
135 | $_property = $this->_property; |
||
136 | $this->_paramBehavior = new TMapRouteBehavior($this->_parameter, function ($v) use ($owner, $_property) { |
||
137 | $owner->$_property = Prado::localize($v); |
||
138 | }); |
||
139 | } else { |
||
140 | $this->_paramBehavior = new TMapRouteBehavior($this->_parameter, [$owner, 'set' . $this->_property]); |
||
141 | } |
||
142 | $appParams = Prado::getApplication()->getParameters(); |
||
143 | $appParams->attachBehavior($this->_routeBehaviorName, $this->_paramBehavior); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | |||
148 | /** |
||
149 | * This attaches and detaches the routing behavior on the Application Parameters. |
||
150 | * @param bool $enabled whether this behavior is enabled |
||
151 | */ |
||
152 | public function setEnabled($enabled) |
||
153 | { |
||
154 | if ($enabled == true && !$this->_paramBehavior) { |
||
155 | $this->attachParamMapRoute(); |
||
156 | } elseif ($enabled == false && $this->_paramBehavior) { |
||
157 | Prado::getApplication()->getParameters()->detachBehavior($this->_routeBehaviorName); |
||
158 | $this->_paramBehavior = null; |
||
159 | } |
||
160 | parent::setEnabled($enabled); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * This removes the Application Parameter handler behavior |
||
165 | * @param object $owner the object that this behavior is attached to. |
||
166 | */ |
||
167 | public function detach($owner) |
||
168 | { |
||
169 | if ($this->_paramBehavior) { |
||
170 | Prado::getApplication()->getParameters()->detachBehavior($this->_routeBehaviorName); |
||
171 | $this->_routeBehaviorName = null; |
||
172 | } |
||
173 | $this->_initialized = false; |
||
174 | parent::detach($owner); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return string Application parameter key to set the property. |
||
179 | */ |
||
180 | public function getParameter() |
||
181 | { |
||
182 | return $this->_parameter; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string $value Application parameter key to set the property. |
||
187 | */ |
||
188 | public function setParameter($value) |
||
189 | { |
||
190 | $this->_parameter = TPropertyValue::ensureString($value); |
||
191 | if ($this->_paramBehavior) { |
||
192 | if (!strlen($value)) { |
||
193 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_parameter_to_blank_after_attach'); |
||
194 | } |
||
195 | $this->_paramBehavior->setParameter($value); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return string Application parameter key to set the property. |
||
201 | */ |
||
202 | public function getValidNullValue() |
||
203 | { |
||
204 | return $this->_validNullValue; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string $value Application parameter key to set the property. |
||
209 | */ |
||
210 | public function setValidNullValue($value) |
||
211 | { |
||
212 | if ($this->_initialized) { |
||
213 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_validNullValue_after_attach'); |
||
214 | } |
||
215 | $this->_validNullValue = TPropertyValue::ensureBoolean($value); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return string Application parameter key to set the property. |
||
220 | */ |
||
221 | public function getProperty() |
||
222 | { |
||
223 | return $this->_property; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param string $value Application parameter key to set the property. |
||
228 | */ |
||
229 | public function setProperty($value) |
||
230 | { |
||
231 | if ($this->_initialized) { |
||
232 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_property_after_attach'); |
||
233 | } |
||
234 | $this->_property = TPropertyValue::ensureString($value); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return string The default value when there is no property and ValidNullValue is false. |
||
239 | */ |
||
240 | public function getDefaultValue() |
||
241 | { |
||
242 | return $this->_defaultValue; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param string $value The default value when there is no property and ValidNullValue is false. |
||
247 | */ |
||
248 | public function setDefaultValue($value) |
||
249 | { |
||
250 | if ($this->_initialized) { |
||
251 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_defaultValue_after_attach'); |
||
252 | } |
||
253 | $this->_defaultValue = TPropertyValue::ensureString($value); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @return string should the parameter or defaultValue be localized. |
||
258 | */ |
||
259 | public function getLocalize() |
||
260 | { |
||
261 | return $this->_localize; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $value should the parameter or defaultValue be localized. |
||
266 | */ |
||
267 | public function setLocalize($value) |
||
268 | { |
||
269 | if ($this->_initialized) { |
||
270 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_localize_after_attach'); |
||
271 | } |
||
272 | $this->_localize = TPropertyValue::ensureBoolean($value); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return string The TMap Routing Behavior Name for changes on the Parameter key updating the Property. |
||
277 | */ |
||
278 | public function getRouteBehaviorName() |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $value The TMap Routing Behavior Name for changes on the Parameter key updating the Property. |
||
285 | */ |
||
286 | public function setRouteBehaviorName($value) |
||
287 | { |
||
288 | if ($this->_initialized) { |
||
289 | throw new TInvalidOperationException('parameterizebehavior_cannot_set_routeBehaviorName_after_attach'); |
||
290 | } |
||
292 | } |
||
293 | } |
||
294 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths