Complex classes like ProductSpec 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ProductSpec, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class ProductSpec extends ObjectBehavior |
||
| 28 | { |
||
| 29 | function let() |
||
| 34 | |||
| 35 | function it_implements_product_interface(): void |
||
| 39 | |||
| 40 | function it_implements_toggleable_interface(): void |
||
| 44 | |||
| 45 | function it_has_no_id_by_default(): void |
||
| 49 | |||
| 50 | function it_has_no_name_by_default(): void |
||
| 54 | |||
| 55 | function its_name_is_mutable(): void |
||
| 60 | |||
| 61 | function it_has_no_slug_by_default(): void |
||
| 65 | |||
| 66 | function its_slug_is_mutable(): void |
||
| 71 | |||
| 72 | function it_has_no_description_by_default(): void |
||
| 76 | |||
| 77 | function its_description_is_mutable(): void |
||
| 82 | |||
| 83 | function it_initializes_attribute_collection_by_default(): void |
||
| 87 | |||
| 88 | function it_adds_attribute(ProductAttributeValueInterface $attribute): void |
||
| 95 | |||
| 96 | function it_removes_attribute(ProductAttributeValueInterface $attribute): void |
||
| 108 | |||
| 109 | function it_refuses_to_add_non_product_attribute(AttributeValueInterface $attribute): void |
||
| 114 | |||
| 115 | function it_refuses_to_remove_non_product_attribute(AttributeValueInterface $attribute): void |
||
| 119 | |||
| 120 | function it_returns_attributes_by_a_locale_without_a_base_locale( |
||
| 121 | ProductAttributeInterface $attribute, |
||
| 122 | ProductAttributeValueInterface $attributeValueEN, |
||
| 123 | ProductAttributeValueInterface $attributeValuePL |
||
| 124 | ): void { |
||
| 125 | $attribute->getCode()->willReturn('colour'); |
||
| 126 | |||
| 127 | $attributeValueEN->setProduct($this)->shouldBeCalled(); |
||
| 128 | $attributeValueEN->getLocaleCode()->willReturn('en_US'); |
||
| 129 | $attributeValueEN->getAttribute()->willReturn($attribute); |
||
|
|
|||
| 130 | $attributeValueEN->getCode()->willReturn('colour'); |
||
| 131 | $attributeValueEN->getValue()->willReturn('Blue'); |
||
| 132 | |||
| 133 | $attributeValuePL->setProduct($this)->shouldBeCalled(); |
||
| 134 | $attributeValuePL->getLocaleCode()->willReturn('pl_PL'); |
||
| 135 | $attributeValuePL->getAttribute()->willReturn($attribute); |
||
| 136 | $attributeValuePL->getCode()->willReturn('colour'); |
||
| 137 | $attributeValuePL->getValue()->willReturn('Niebieski'); |
||
| 138 | |||
| 139 | $this->addAttribute($attributeValueEN); |
||
| 140 | $this->addAttribute($attributeValuePL); |
||
| 141 | |||
| 142 | $this |
||
| 143 | ->getAttributesByLocale('pl_PL', 'en_US') |
||
| 144 | ->shouldIterateAs([$attributeValuePL->getWrappedObject()]) |
||
| 145 | ; |
||
| 146 | } |
||
| 147 | |||
| 148 | function it_returns_attributes_by_a_locale_with_a_base_locale( |
||
| 149 | ProductAttributeInterface $attribute, |
||
| 150 | ProductAttributeValueInterface $attributeValueEN, |
||
| 151 | ProductAttributeValueInterface $attributeValuePL, |
||
| 152 | ProductAttributeValueInterface $attributeValueFR |
||
| 153 | ): void { |
||
| 154 | $attribute->getCode()->willReturn('colour'); |
||
| 155 | |||
| 156 | $attributeValueEN->setProduct($this)->shouldBeCalled(); |
||
| 157 | $attributeValueEN->getLocaleCode()->willReturn('en_US'); |
||
| 158 | $attributeValueEN->getAttribute()->willReturn($attribute); |
||
| 159 | $attributeValueEN->getCode()->willReturn('colour'); |
||
| 160 | $attributeValueEN->getValue()->willReturn('Blue'); |
||
| 161 | |||
| 162 | $attributeValuePL->setProduct($this)->shouldBeCalled(); |
||
| 163 | $attributeValuePL->getLocaleCode()->willReturn('pl_PL'); |
||
| 164 | $attributeValuePL->getAttribute()->willReturn($attribute); |
||
| 165 | $attributeValuePL->getCode()->willReturn('colour'); |
||
| 166 | $attributeValuePL->getValue()->willReturn('Niebieski'); |
||
| 167 | |||
| 168 | $attributeValueFR->setProduct($this)->shouldBeCalled(); |
||
| 169 | $attributeValueFR->getLocaleCode()->willReturn('fr_FR'); |
||
| 170 | $attributeValueFR->getAttribute()->willReturn($attribute); |
||
| 171 | $attributeValueFR->getCode()->willReturn('colour'); |
||
| 172 | $attributeValueFR->getValue()->willReturn('Bleu'); |
||
| 173 | |||
| 174 | $this->addAttribute($attributeValueEN); |
||
| 175 | $this->addAttribute($attributeValuePL); |
||
| 176 | $this->addAttribute($attributeValueFR); |
||
| 177 | |||
| 178 | $this |
||
| 179 | ->getAttributesByLocale('pl_PL', 'en_US', 'fr_FR') |
||
| 180 | ->shouldIterateAs([$attributeValuePL->getWrappedObject()]) |
||
| 181 | ; |
||
| 182 | } |
||
| 183 | |||
| 184 | function it_returns_attributes_by_a_fallback_locale_when_there_is_no_value_for_a_given_locale( |
||
| 185 | ProductAttributeInterface $attribute, |
||
| 186 | ProductAttributeValueInterface $attributeValueEN |
||
| 187 | ): void { |
||
| 188 | $attribute->getCode()->willReturn('colour'); |
||
| 189 | |||
| 190 | $attributeValueEN->setProduct($this)->shouldBeCalled(); |
||
| 191 | $attributeValueEN->getLocaleCode()->willReturn('en_US'); |
||
| 192 | $attributeValueEN->getAttribute()->willReturn($attribute); |
||
| 193 | $attributeValueEN->getCode()->willReturn('colour'); |
||
| 194 | $attributeValueEN->getValue()->willReturn('Blue'); |
||
| 195 | |||
| 196 | $this->addAttribute($attributeValueEN); |
||
| 197 | |||
| 198 | $this |
||
| 199 | ->getAttributesByLocale('pl_PL', 'en_US') |
||
| 200 | ->shouldIterateAs([$attributeValueEN->getWrappedObject()]) |
||
| 201 | ; |
||
| 202 | } |
||
| 203 | |||
| 204 | function it_returns_attributes_by_a_fallback_locale_when_there_is_an_empty_value_for_a_given_locale( |
||
| 205 | ProductAttributeInterface $attribute, |
||
| 206 | ProductAttributeValueInterface $attributeValueEN, |
||
| 207 | ProductAttributeValueInterface $attributeValuePL |
||
| 208 | ): void { |
||
| 209 | $attribute->getCode()->willReturn('colour'); |
||
| 210 | |||
| 211 | $attributeValueEN->setProduct($this)->shouldBeCalled(); |
||
| 212 | $attributeValueEN->getLocaleCode()->willReturn('en_US'); |
||
| 213 | $attributeValueEN->getAttribute()->willReturn($attribute); |
||
| 214 | $attributeValueEN->getCode()->willReturn('colour'); |
||
| 215 | $attributeValueEN->getValue()->willReturn('Blue'); |
||
| 216 | |||
| 217 | $attributeValuePL->setProduct($this)->shouldBeCalled(); |
||
| 218 | $attributeValuePL->getLocaleCode()->willReturn('pl_PL'); |
||
| 219 | $attributeValuePL->getAttribute()->willReturn($attribute); |
||
| 220 | $attributeValuePL->getCode()->willReturn('colour'); |
||
| 221 | $attributeValuePL->getValue()->willReturn(''); |
||
| 222 | |||
| 223 | $this->addAttribute($attributeValueEN); |
||
| 224 | $this->addAttribute($attributeValuePL); |
||
| 225 | |||
| 226 | $this |
||
| 227 | ->getAttributesByLocale('pl_PL', 'en_US') |
||
| 228 | ->shouldIterateAs([$attributeValueEN->getWrappedObject()]) |
||
| 229 | ; |
||
| 230 | } |
||
| 231 | |||
| 232 | function it_returns_attributes_by_a_base_locale_when_there_is_no_value_for_a_given_locale_or_a_fallback_locale( |
||
| 233 | ProductAttributeInterface $attribute, |
||
| 234 | ProductAttributeValueInterface $attributeValueFR |
||
| 235 | ): void { |
||
| 236 | $attribute->getCode()->willReturn('colour'); |
||
| 237 | |||
| 238 | $attributeValueFR->setProduct($this)->shouldBeCalled(); |
||
| 239 | $attributeValueFR->getLocaleCode()->willReturn('fr_FR'); |
||
| 240 | $attributeValueFR->getAttribute()->willReturn($attribute); |
||
| 241 | $attributeValueFR->getCode()->willReturn('colour'); |
||
| 242 | $attributeValueFR->getValue()->willReturn('Bleu'); |
||
| 243 | |||
| 244 | $this->addAttribute($attributeValueFR); |
||
| 245 | |||
| 246 | $this |
||
| 247 | ->getAttributesByLocale('pl_PL', 'en_US', 'fr_FR') |
||
| 248 | ->shouldIterateAs([$attributeValueFR->getWrappedObject()]) |
||
| 249 | ; |
||
| 250 | } |
||
| 251 | |||
| 252 | function it_returns_attributes_by_a_base_locale_when_there_is_an_empty_value_for_a_given_locale_or_a_fallback_locale( |
||
| 253 | ProductAttributeInterface $attribute, |
||
| 254 | ProductAttributeValueInterface $attributeValueEN, |
||
| 255 | ProductAttributeValueInterface $attributeValuePL, |
||
| 256 | ProductAttributeValueInterface $attributeValueFR |
||
| 257 | ): void { |
||
| 258 | $attribute->getCode()->willReturn('colour'); |
||
| 259 | |||
| 260 | $attributeValueEN->setProduct($this)->shouldBeCalled(); |
||
| 261 | $attributeValueEN->getLocaleCode()->willReturn('en_US'); |
||
| 262 | $attributeValueEN->getAttribute()->willReturn($attribute); |
||
| 263 | $attributeValueEN->getCode()->willReturn('colour'); |
||
| 264 | $attributeValueEN->getValue()->willReturn(''); |
||
| 265 | |||
| 266 | $attributeValuePL->setProduct($this)->shouldBeCalled(); |
||
| 267 | $attributeValuePL->getLocaleCode()->willReturn('pl_PL'); |
||
| 268 | $attributeValuePL->getAttribute()->willReturn($attribute); |
||
| 269 | $attributeValuePL->getCode()->willReturn('colour'); |
||
| 270 | $attributeValuePL->getValue()->willReturn(null); |
||
| 271 | |||
| 272 | $attributeValueFR->setProduct($this)->shouldBeCalled(); |
||
| 273 | $attributeValueFR->getLocaleCode()->willReturn('fr_FR'); |
||
| 274 | $attributeValueFR->getAttribute()->willReturn($attribute); |
||
| 275 | $attributeValueFR->getCode()->willReturn('colour'); |
||
| 276 | $attributeValueFR->getValue()->willReturn('Bleu'); |
||
| 277 | |||
| 278 | $this->addAttribute($attributeValueEN); |
||
| 279 | $this->addAttribute($attributeValuePL); |
||
| 280 | $this->addAttribute($attributeValueFR); |
||
| 281 | |||
| 282 | $this |
||
| 283 | ->getAttributesByLocale('pl_PL', 'en_US', 'fr_FR') |
||
| 284 | ->shouldIterateAs([$attributeValueFR->getWrappedObject()]) |
||
| 285 | ; |
||
| 286 | } |
||
| 287 | |||
| 288 | function it_has_no_variants_by_default(): void |
||
| 292 | |||
| 293 | function its_says_it_has_variants_only_if_multiple_variants_are_defined( |
||
| 304 | |||
| 305 | function it_initializes_variants_collection_by_default(): void |
||
| 309 | |||
| 310 | function it_does_not_include_unavailable_variants_in_available_variants(ProductVariantInterface $variant): void |
||
| 316 | |||
| 317 | function it_returns_available_variants( |
||
| 327 | |||
| 328 | function it_initializes_options_collection_by_default(): void |
||
| 332 | |||
| 333 | function it_has_no_options_by_default(): void |
||
| 337 | |||
| 338 | function its_says_it_has_options_only_if_any_option_defined(ProductOptionInterface $option): void |
||
| 343 | |||
| 344 | function it_adds_option_properly(ProductOptionInterface $option): void |
||
| 349 | |||
| 350 | function it_removes_option_properly(ProductOptionInterface $option): void |
||
| 358 | |||
| 359 | function it_initializes_creation_date_by_default(): void |
||
| 363 | |||
| 364 | function its_creation_date_is_mutable(\DateTime $creationDate): void |
||
| 369 | |||
| 370 | function it_has_no_last_update_date_by_default(): void |
||
| 374 | |||
| 375 | function its_last_update_date_is_mutable(\DateTime $updateDate): void |
||
| 380 | |||
| 381 | function it_is_enabled_by_default(): void |
||
| 385 | |||
| 386 | function it_is_toggleable(): void |
||
| 394 | |||
| 395 | function it_adds_association(ProductAssociationInterface $association): void |
||
| 402 | |||
| 403 | function it_allows_to_remove_association(ProductAssociationInterface $association): void |
||
| 413 | |||
| 414 | function it_is_simple_if_it_has_one_variant_and_no_options(ProductVariantInterface $variant): void |
||
| 422 | |||
| 423 | function it_is_configurable_if_it_has_at_least_two_variants( |
||
| 435 | |||
| 436 | function it_is_configurable_if_it_has_one_variant_and_at_least_one_option( |
||
| 447 | } |
||
| 448 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.