Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long method #126

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
50 changes: 24 additions & 26 deletions src/main/java/com/shopify/model/ShopifyProductUpdateRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,57 +148,55 @@ public PublishedStep withVariantRequests(final List<ShopifyVariantRequest> varia
changed = true;
}

final List<ShopifyVariant> shopifyVariants = new ArrayList<>(variantRequests.size());
final List<Integer> positions = new ArrayList<>(variantRequests.size());
List<ShopifyVariant> shopifyVariants = new ArrayList<>(variantRequests.size());
List<Integer> positions = new ArrayList<>(variantRequests.size());

for (int i = 0; i < variantRequests.size(); i++) {
final ShopifyVariantRequest shopifyVariantRequestForPosition = variantRequests.get(i);
positions.add(shopifyVariantRequestForPosition.getRequest().getPosition());

}

int maxPosition = variantRequests.stream().map(ShopifyVariantRequest::getRequest)
.map(ShopifyVariant::getPosition).max(Comparator.naturalOrder()).get();
int maxPosition = positions.stream().max(Comparator.naturalOrder()).get();

Collections.sort(variantRequests, new ShopifyVariantRequestOption1Comparator());

updateVariants(variantRequests, shopifyVariants, positions, maxPosition);

shopifyProduct.setVariants(shopifyVariants);

return this;
}

private void updateVariants(final List<ShopifyVariantRequest> variantRequests,
final List<ShopifyVariant> shopifyVariants,
final List<Integer> positions,
int maxPosition) {
for (int i = 0; i < variantRequests.size(); i++) {
final ShopifyVariantRequest shopifyVariantRequest = variantRequests.get(i);

if (shopifyVariantRequest.hasChanged()) {
changed = true;
}

final ShopifyVariant shopifyVariant = shopifyVariantRequest.getRequest();

if (shopifyVariant.getPosition() == 0) {

maxPosition = maxPosition + 1;
maxPosition += 1;
shopifyVariant.setPosition(maxPosition);
}

if (shopifyVariantRequest.hasImageSource()) {
final String imageSource = shopifyVariantRequest.getImageSource();
shopifyProduct.getImages().stream().filter(image -> image.getSource().equals(imageSource))
.findFirst().ifPresent(image -> {
variantPositionToImagePosition.put(shopifyVariant.getPosition(), image.getPosition());
});
shopifyProduct.getImages().stream()
.filter(image -> image.getSource().equals(imageSource))
.findFirst()
.ifPresent(
image -> variantPositionToImagePosition.put(shopifyVariant.getPosition(),
image.getPosition()));
}

}

Collections.sort(variantRequests, new ShopifyVariantRequestPositionComparator());

for (int i = 0; i < variantRequests.size(); i++) {
final ShopifyVariantRequest shopifyVariantRequest = variantRequests.get(i);
if (shopifyVariantRequest.hasChanged()) {
changed = true;
}

final ShopifyVariant shopifyVariant = shopifyVariantRequest.getRequest();
shopifyVariants.add(shopifyVariant);

}

shopifyProduct.setVariants(shopifyVariants);
return this;
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions src/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
This ruleset checks my code for bad stuff
</description>
<rule ref="category/java/design.xml/ExcessiveParameterList" />
<rule ref="category/java/design.xml/ExcessiveClassLength">
<properties>
<property name="minimum" value="20" />
</properties>
</rule>
<rule ref="category/java/design.xml/ExcessiveMethodLength">
<properties>
<property name="minimum" value="30" />
</properties>
</rule>
<rule ref="category/java/design.xml/GodClass" />
<rule ref="category/java/design.xml/DataClass" />
<rule ref="category/java/design.xml/TooManyMethods" />
</ruleset>