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

[apex] Invalid Apex in Cognitive Complexity tests #2358

Closed
rsoesemann opened this issue Mar 17, 2020 · 5 comments · Fixed by #2383
Closed

[apex] Invalid Apex in Cognitive Complexity tests #2358

rsoesemann opened this issue Mar 17, 2020 · 5 comments · Fixed by #2383
Assignees
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@rsoesemann
Copy link
Member

rsoesemann commented Mar 17, 2020

The Cognitive Complexity rule added by @gwilymatgearset in PMD 6.22 (#2297) has text sample code that is no valid Apex.

Please check and fix ALL instances but here are a few issues:

  • Uses double quotes instead of single quotes for Strings
  • Uses Lowercase Type names string instead of String
  • method setPhoneNumberIfNotExisting is missing a return;

Here's the example code, that needs to be fixed:

public class Foo {
// Has a cognitive complexity of 0
public void createAccount() {
Account account = new Account(Name = 'PMD');
insert account;
}
// Has a cognitive complexity of 1
public Boolean setPhoneNumberIfNotExisting(Account a, String phone) {
if (a.Phone == null) { // +1
a.Phone = phone;
update a;
}
}
// Has a cognitive complexity of 5
public void updateContacts(List<Contact> contacts) {
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact contact : contacts) { // +1
if (contact.Department == 'Finance') { // +2
contact.Title = 'Finance Specialist';
contactsToUpdate.add(contact);
} else if (contact.Department == 'Sales') { // +2
contact.Title = 'Sales Specialist';
contactsToUpdate.add(contact);
}
}
update contactsToUpdate;
}
}

@rsoesemann rsoesemann self-assigned this Mar 17, 2020
@rsoesemann rsoesemann changed the title [APEX [apex] Cognitive Complexity rules tests contain invalid Apex code Mar 17, 2020
@rsoesemann rsoesemann removed their assignment Mar 17, 2020
@rsoesemann rsoesemann added the a:bug PMD crashes or fails to analyse a file. label Mar 17, 2020
@rsoesemann rsoesemann added this to the 6.23.0 milestone Mar 17, 2020
@rsoesemann rsoesemann changed the title [apex] Cognitive Complexity rules tests contain invalid Apex code [apex] Cognitive Complexity tests us invalid Apex Mar 17, 2020
@rsoesemann rsoesemann changed the title [apex] Cognitive Complexity tests us invalid Apex [apex] Invalid Apex in Cognitive Complexity tests Mar 17, 2020
@adangel adangel added help-wanted good first issue A great starting point for new contributors labels Mar 20, 2020
@rsoesemann
Copy link
Member Author

@adangel @gwilymatgearset will work on this. The original contributor. I couldnt assign it to him. Can you?

@adangel
Copy link
Member

adangel commented Mar 20, 2020

I've invited him now as a outside collaborator. @gwilymatgearset can you accept the invite? Then I can assign the issue to you... thanks

@adangel adangel removed good first issue A great starting point for new contributors help-wanted labels Mar 20, 2020
@gwilymatgearset gwilymatgearset self-assigned this Mar 27, 2020
@gwilymatgearset
Copy link
Collaborator

@rsoesemann I'm slightly confused by 2 of your comments :)

Uses double quotes instead of single quotes for Strings

That makes it invalid apex doesn't it?
image

Uses Lowercase Type names string instead of String

I went for upper case rather than lower case here because that's what salesforce do in all of their documentation (and what they do in trailhead)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_variables.htm

@rsoesemann
Copy link
Member Author

@gwilymatgearset Sorry for the confusion. Your examples just dont compile because of the missing returns but other Apex examples in the same XML are faulty. I mixed this up. If you have time to fix them in one go this would be awesome.

@gwilymatgearset
Copy link
Collaborator

Can do :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants