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

Proposed removal of some static checks for phtml template files #160

Merged
merged 1 commit into from
May 15, 2019

Conversation

nathanjosiah
Copy link
Contributor

No description provided.

@magento-cicd2
Copy link

magento-cicd2 commented May 10, 2019

CLA assistant check
All committers have signed the CLA.

@nathanjosiah
Copy link
Contributor Author

These changes were already approved by @buskamuza and @piotrekkaminski in an internal meeting. PR opened for further discussion

|-----|---------|
| `Magento2.Files.LineLength` |HTML is frequently much cleaner when kept on single lines even if they are longer. This sniff also incorrectly counts lines containing PHP.|
| <ul><li>`Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace`</li><li> `Squiz.WhiteSpace.ScopeClosingBrace`</li><li>`PEAR.ControlStructures.ControlSignature`</li></ul>|Inline PHP can be cleaner and easier to read in many cases within a PHTML document. <br/><br/> Additionally, the `PEAR.ControlStructures.ControlSignature.Found` sniff disallows any inline control structures but allows the alternative control syntax (if (foo): blah endif; ) as well as ternaries (foo ? bar : baz) which are the same thing. <br/> <br/> These sniffs also incorrectly detect the control signatures leading to a mixed set of cases where it is actually flagged. It seems to only flag some of these when the endif or } is on the same line as the if|
| `Squiz.Operators.IncrementDecrementUsage`|Very common in templating|
Copy link
Member

Choose a reason for hiding this comment

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

What does this one disallow? $i++, $i--?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@melnikovi Yes, as well as += and -=

Copy link
Member

Choose a reason for hiding this comment

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

Understood, agree that we need this in templates.

Copy link
Contributor

Choose a reason for hiding this comment

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

Very common in any loop. Simply remove this rule completely.


|Sniff|Rationale|
|-----|---------|
| `Magento2.Files.LineLength` |HTML is frequently much cleaner when kept on single lines even if they are longer. This sniff also incorrectly counts lines containing PHP.|
Copy link
Member

Choose a reason for hiding this comment

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

Could you please explain what does Squiz.WhiteSpace.ScopeClosingBrace check? Do you propose completely remove PEAR.ControlStructures.ControlSignature? I think alternative syntax is more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works fine for regular php files where it asserts that control structures look like this:

if ($foo) {
  //something
}

However for phtml files it gives different results based on the context but essentially something like this:

<foo <?php if ($foo) { ?>checked<?php } ?>
<foo <?php if ($foo) : ?>checked<?php endif; ?>

is required to be turned into

<foo <?php if ($foo) { 
          ?>checked<?php } ?>
<foo <?php if ($foo) : 
          ?>checked<?php endif; ?>

or some other weird format.

Choose a reason for hiding this comment

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

phpcbf automatically convert it to this at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@michaelyu0123 Yes I understand but the purpose was for code to be cleaner, this is messy.

Copy link
Member

Choose a reason for hiding this comment

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

So PEAR.ControlStructures.ControlSignature requires to add new line? Then I'm ok with removing it. Does Squiz.WhiteSpace.ScopeClosingBrace check that whitespace exist or doesn't exist before closing tag?

I think that this syntax
<?php if ($foo): ?><h2>Hello world</h2><?php endif ?>
or

<?php if ($foo): ?>
    <h2>Hello world</h2>
<?php endif ?>

both are good examples. Do we preserve this style?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@melnikovi

So PEAR.ControlStructures.ControlSignature requires to add new line?

Yes, and only for some cases so it's inconsistent.

Does Squiz.WhiteSpace.ScopeClosingBrace check that whitespace exist...

There are different checks for proper (non-newline) whitespace.

Do we preserve this style?

Yes, that's the idea for the PR is that I think we should preserve these styles


### Design

To address this issue for the immediate efforts as well as for all developers in the future I am proposing to remove the following sniffs
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that you also propose to start removing @codingStandardsIgnore which would require fixing existing errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, and all files affected by the security efforts (hundreds of phtml files) will be cleaned up as part of our initial merges in the next couple weeks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I may be wrong, but I believe there is already a backlog item to remove all @codingStandardsIgnore annotations.

@melnikovi melnikovi self-assigned this May 10, 2019
Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

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

Please don't touch any sniffs which belong to PSR-2. Such code needs to be adopted.

Among all described sniffs Magento2.Security.LanguageConstruct.DirectOutput was implemented wrongly in the first and all other seem to be adoptable.

### Overview

At the time of this writing only 85% of the `*.phtml` files in Magento core are being statically
tested due to `@codingStandardsIgnore` present in over 700 templates. There is a current security initiative
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you meant @codingStandardsIgnoreFile here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes


At the time of this writing only 85% of the `*.phtml` files in Magento core are being statically
tested due to `@codingStandardsIgnore` present in over 700 templates. There is a current security initiative
that requires a large number of templates to be modified and they need to be covered by static checks moving
Copy link
Contributor

Choose a reason for hiding this comment

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

Link to initiative, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think maintainers have access to JIRA after acquisition by Adobe anymore.

Is it too sensitive for public discussion or at least in some private Slack channel?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's sort of sensitive. The changes will go to 2.3-develop. The changes essentially involve cleaning certain things up in every module.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I believe it would be enough to share what kind of changes to code and tests is supposed to be done so that it can be aligned with other coding standard compliance activities.

Copy link
Contributor

Choose a reason for hiding this comment

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

In scope of this task the changes are described in this proposal - cleanup templates (by removing phpcs suppression).

forward as part of the Acceptance Criteria of the initiate.

However, due to the lack of current static template checks the codebase has thousands of static template errors and will require many
hours to address every issue in every file. Additionally, many of the tests are not reasonable, checking incorrectly, or simply gratuitous for `*.phtml` templates.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not true as it does not require a lot of manual work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately this is not true. We have about five teams that have attempted to adopt the principles and it is tripling the amount of time needed to complete these tasks.

Copy link
Contributor

Choose a reason for hiding this comment

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

LOL :) I did it once for 2.2-develop before in magento/magento2#9367 already. That sounds like a good challenge to me, want to get all the details and solve the problem then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like a big effort thank you for that contribution. Unfortunately we have other more important business priorities than cleaning up every file and potentially breaking things. Plus, the purpose of this request is to lower the bar for entry and make it easier write and change templates without massive efforts such as the one you linked to.


|Sniff|Rationale|
|-----|---------|
| `Magento2.Files.LineLength` |HTML is frequently much cleaner when kept on single lines even if they are longer. This sniff also incorrectly counts lines containing PHP.|
Copy link
Contributor

Choose a reason for hiding this comment

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

It may mean that there are too much nesting levels. Such violations may be fixed with automated tooling.

Needs to be provided some insight on current situation to decide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have no standards for HTML nesting level requirements that we in any way claim to follow. Therefore the comment about "too much nesting levels" seems like a personal opinion rather than a requirement. Please share with me any requirement if there is one documented. All of the automated tooling works for PHP or HTML but I've never seen anything that works for both. Additionally, our existing tooling only works for PHP.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is not what I was saying :) I didn't say a word regarding nesting level enforcement.

  1. Line length is a good limitation to start from as high nesting level by implication leads to longer lines
  2. I did some experiments before with automated code formatting by setting absolute line limit to 80 or 120 characters and it worked smoothly

Copy link
Contributor Author

@nathanjosiah nathanjosiah May 14, 2019

Choose a reason for hiding this comment

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

Here's a very common example of cases where line length is arbitrary and would be messy to try and break it apart:

                                <label class="admin__field-label"
                                       for="sidebar-<?= $block->escapeHtmlAttr($block->getSidebarStorageAction()) ?>-<?= $block->escapeHtmlAttr($block->getIdentifierId($_item)) ?>">
                                </label>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

I do share the sentiment that every error could be addressed in some way however it doesn't make too much sense to do it. High levels of nesting in PHP are a code smell and a sign that it can be refactored to have less code paths and run more efficiently. Long lines have a similar idea. However, with HTML nesting is a natural part of the language and at most there can be a mild correlation between nesting level and good design. So, forcing arbitrary constraints on PHP because of HTML structure is where I disagree with this approach.

Also, to reiterate what I've said elsewhere, the purpose of this PR is to make templates easier to write and refactor without risking too much change. I understand that in some cases this may come at cost of slightly longer lines but engineering is all about finding reasonable compromises and solutions.

|-----|---------|
| `Magento2.Files.LineLength` |HTML is frequently much cleaner when kept on single lines even if they are longer. This sniff also incorrectly counts lines containing PHP.|
| <ul><li>`Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace`</li><li> `Squiz.WhiteSpace.ScopeClosingBrace`</li><li>`PEAR.ControlStructures.ControlSignature`</li></ul>|Inline PHP can be cleaner and easier to read in many cases within a PHTML document. <br/><br/> Additionally, the `PEAR.ControlStructures.ControlSignature.Found` sniff disallows any inline control structures but allows the alternative control syntax (if (foo): blah endif; ) as well as ternaries (foo ? bar : baz) which are the same thing. <br/> <br/> These sniffs also incorrectly detect the control signatures leading to a mixed set of cases where it is actually flagged. It seems to only flag some of these when the endif or } is on the same line as the if|
| `Squiz.Operators.IncrementDecrementUsage`|Very common in templating|
Copy link
Contributor

Choose a reason for hiding this comment

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

Very common in any loop. Simply remove this rule completely.

| `Magento2.Files.LineLength` |HTML is frequently much cleaner when kept on single lines even if they are longer. This sniff also incorrectly counts lines containing PHP.|
| <ul><li>`Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace`</li><li> `Squiz.WhiteSpace.ScopeClosingBrace`</li><li>`PEAR.ControlStructures.ControlSignature`</li></ul>|Inline PHP can be cleaner and easier to read in many cases within a PHTML document. <br/><br/> Additionally, the `PEAR.ControlStructures.ControlSignature.Found` sniff disallows any inline control structures but allows the alternative control syntax (if (foo): blah endif; ) as well as ternaries (foo ? bar : baz) which are the same thing. <br/> <br/> These sniffs also incorrectly detect the control signatures leading to a mixed set of cases where it is actually flagged. It seems to only flag some of these when the endif or } is on the same line as the if|
| `Squiz.Operators.IncrementDecrementUsage`|Very common in templating|
| `Magento2.Security.LanguageConstruct.DirectOutput`|This makes sense for PHP files but not for templates where the whole purpose is to output content.|
Copy link
Contributor

Choose a reason for hiding this comment

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

The sniff must specify that it is only applicable to php then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is the whole purpose of this PR, these removals only affect PHTML files.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean there is no reason to do any changes to standard, just specify in Magento2.Security.LanguageConstruct.DirectOutput sniff that it do not touch phtml.

Copy link
Contributor

@buskamuza buskamuza May 14, 2019

Choose a reason for hiding this comment

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

Can you elaborate? I think this is what is recommended - do not run the rule for phtml templates. You want to put this logic in the rule itself instead of limiting it in the ruleset? If yes, why?

#### Acceptance Criteria Fulfillment

1. Acceptance Criteria 1
The proposed sniffs exclude `*phtml` files preferably in a way that doesn't affect the `magento-coding-standards` repo.
Copy link
Contributor

Choose a reason for hiding this comment

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

This totally does not make sense. For the sake of "unification" MEQP standard was renamed to "Magento2 Coding Standard" without a proper adoption of core codebase.

Now because this "new" coding standard was placed in a separate repo (for no reason, as you all probably remember) we are trying to not "affect" it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may not actually be possible due to how our CICD tooling works. It is only a desired goal.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm saying that such goal sounds absurd. We should have only one comprehensive coding standard for all situations and adjust it properly until this is achieved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. However, because of how our CICD tooling validates builds I'm not sure this will be feasible at the moment.

@nathanjosiah
Copy link
Contributor Author

@orlangur All of these sniff removals are only going to affect PHTML templates. Since PSR2 only applies to PHP files (that is to say, they define no rules surrounding PHP formatting in HTML templates) we could theoretically disable all PHTML sniffs for PSR2 and still be in compliance.

@orlangur
Copy link
Contributor

@nathanjosiah,

Since PSR2 only applies to PHP files

This is a huge misunderstanding. PSR2 is fully applicable to PHTML files (it was not possible to use it before until PHPCS 1.x contained some bugs but now totally possible).

@melnikovi
Copy link
Member

@orlangur I'm going to accept this PR as it's approved by two people. If you have any suggestions please submit PR that alters this proposal.

@melnikovi melnikovi merged commit 0811f81 into magento:master May 15, 2019
@orlangur
Copy link
Contributor

@melnikovi ROFL 🤣 Hope someday I'll get used to this game.

@nathanjosiah nathanjosiah deleted the static-checks-for-templates branch May 16, 2019 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants