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

Add documentation for AssignmentInTernaryCondition sniff #2488

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Nic-Sevic
Copy link

Related to #1722

  • /docs/CodeAnalyis/AssignmentInTernaryConditionStandard.xml

Copy link
Collaborator

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this PR, @Nic-Sevic! I left some comments for us to discuss. Please take into account that I'm not a maintainer of the project so I might have missed something or suggest something that the maintainers might disagree.

@@ -0,0 +1,19 @@
<documentation title="Assignment In Ternary Condition">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding the XML version before the <document> tag and also adding the xmlns:xsi and xsi:noNamespaceSchemaLocation attributes to the <document> similar to what is done in the XML files (maybe there are some that don't do that?). Example:

https://github.com/WordPress/WordPress-Coding-Standards/blob/develop/WordPress/Docs/Arrays/ArrayIndentationStandard.xml#L1-L5

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -0,0 +1,19 @@
<documentation title="Assignment In Ternary Condition">
<standard>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs four spaces for indentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<documentation title="Assignment In Ternary Condition">
<standard>
<![CDATA[
Checks that variable assignment does not occur in conditional statement of ternary. Condition must be in parentheses to be checked; Ternaries lacking parentheses around condition are skipped. If incorrect will throw: "Variable assignment found within a condition. Did you mean to do a comparison?"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this line should have four spaces less.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 (not sure why I can't mark those comments as resolved)

<documentation title="Assignment In Ternary Condition">
<standard>
<![CDATA[
Checks that variable assignment does not occur in conditional statement of ternary. Condition must be in parentheses to be checked; Ternaries lacking parentheses around condition are skipped. If incorrect will throw: "Variable assignment found within a condition. Did you mean to do a comparison?"
Copy link
Collaborator

@rodrigoprimo rodrigoprimo Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks that variable assignment does not occur in conditional statement of ternary.

Usually, the standard descriptions are quite explicit/directive. Similar to the error messages. So maybe instead of saying what the sniffs checks, I would suggest describing what the sniff expects. Something like:

"There should be no assignments in the condition part of ternary."

This might need some more polishing but should give the general idea. Checking other descriptions might help as well.

If incorrect will throw: "Variable assignment found within a condition. Did you mean to do a comparison?"

I believe that the sniff documentation usually does not include the error message displayed, so I suggest not including it here unless there is a reason to include it for this particular sniff.

Copy link
Author

@Nic-Sevic Nic-Sevic Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to clarify standard description and improve readability

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving the description. I just realized that I misled you with the text I suggested. This sniff throws a warning and not an error, so we should not use should. Instead, we could say something like it is recommended or some other variation. Could you please update the description to account for that?

]]>
</standard>
<code_comparison>
<code title="Valid: Variable assigned with comparison in parentheses.">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence seems a bit confusing to me because of the use of the word "assigned". The sniff is not about assigning the result of a ternary comparison to a variable. The sniff is triggered whether or not the result of the ternary is assigned to a variable. At the same time, the sniff is precisely checking if there is no assignment in the condition part of the ternary.

Maybe this could be rephrased to avoid this confusion? I also wonder if it would be better to include just the ternary without the echo and the assignment to variable in the examples.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to clarify

<code_comparison>
<code title="Valid: Variable assigned with comparison in parentheses.">
<![CDATA[
echo $mode = ( <em>$a == 'a'</em> ) ? 'b' : 'c';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, I don't think that having an assignment and then echoing in the same line is necessary to illustrate a valid example and, in my opinion, adds a layer of complexity that makes it a bit harder to understand this example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a misunderstanding on my part from mirroring another file; removed

</code>
<code title="Invalid: Parentheses used but variable assignment instead of comparison.">
<![CDATA[
echo $mode = ( <em>$a = 'a'</em> ) ? 'b' : 'c';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should highlight just the assignment operator (and the compison operator above)? I'm not sure to be honest. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, adjusted emphasis here

Copy link
Collaborator

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving this PR, @Nic-Sevic. It is looking good! I left some final comments for us to discuss before I approve it.

>
<standard>
<![CDATA[
Variables should not be assigned in conditional statement of ternary.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Variables should not be assigned in conditional statement of ternary.
Variables should not be assigned in the condition of a ternary operator.

I believe it is more common to use "condition" instead of "conditional statement" to refer to the first expression of a ternary, but I could be wrong. Reference: https://en.wikipedia.org/wiki/Ternary_conditional_operator. I found no mention of "condition" or "conditional statement" in the php.net page about ternaries: https://www.php.net/manual/en/language.operators.comparison.php. We might need to update the text for the valid and invalid examples as well.

Also, the sentence reads better to me if the article "a" is added before "ternary". That being said, I'm not a native speaker, so let me know if that is not the case.

<standard>
<![CDATA[
Variables should not be assigned in conditional statement of ternary.
Condition must be in parentheses to be checked; ternaries lacking parentheses around condition are skipped.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Condition must be in parentheses to be checked; ternaries lacking parentheses around condition are skipped.
The condition must be in parentheses to be checked; ternaries lacking parentheses around the condition are skipped.

Similar to the above, the sentence reads better to me if we add the article "the", but feel free to ignore this comment if that is not the case.

<code_comparison>
<code title="Valid: Conditional statement in parentheses and does not include variable assignment.">
<![CDATA[
$mode = ( $a <em>==</em> 'a' ) ? 'b' : 'c';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$mode = ( $a <em>==</em> 'a' ) ? 'b' : 'c';
$mode = ( $a <em>==</em> 'a' ) ? 'b' : 'c';

Just a nitpick, but usually, the code examples are not indented and should have no spaces. So, the initial version was correct. It doesn't have a noticeable impact in this case, and this is just a single line, but for multiple line code samples, it makes a difference.

<code_comparison>
<code title="Valid: Conditional statement in parentheses and does not include variable assignment.">
<![CDATA[
$mode = ( $a <em>==</em> 'a' ) ? 'b' : 'c';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$mode = ( $a <em>==</em> 'a' ) ? 'b' : 'c';
$mode = ( $a <em>===</em> 'a' ) ? 'b' : 'c';

This is another nitpick, but I suggest using a strict comparison instead of a loose comparison in the valid example so that it follows coding best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants