Skip to content

Commit

Permalink
Include and document MEQP2.Templates.ThisInTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
schmengler committed May 28, 2018
1 parent e611dd3 commit 3c26dcd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Extdn/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<description>Magento 2 Additional Rules by ExtDN</description>
<rule ref="PSR1"/>
<rule ref="PSR2"/>
<rule ref="MEQP2.Templates.ThisInTemplate">
<exclude-pattern>*.php</exclude-pattern>
</rule>
<rule ref="Extdn.Blocks.SetTemplateInBlock"/>
<rule ref="Extdn.Classes.StrictTypes"/>
<rule ref="Extdn.Templates.TemplateObjectManager"/>
Expand Down
13 changes: 13 additions & 0 deletions MEQP2/Sniffs/Templates/ThisInTemplateSniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Rule: Do not use `$this` in templates
## Background
In PHTML templates, the current block is available as `$this` and `$block`. The alternative with `$this` has been deprecated and should not be used anymore.

## Reasoning
`$this` in templates is a legacy from Magento 1. It still works, however this can change any time, should templates and blocks be further decoupled. That's why for new code you should always use `$block` and restrict it to public methods.

## How it works
Any occurence of `$this` in PHTML files (via file pattern in ruleset.xml) raises a warning.

## How to fix

Replace `$this` with `$block`. If you use private or protected methods, make them public.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ As an example, you can use the `SetTemplateInBlockSniff` within the folder `Extd

To run the sniff unit tests from a main repository, where the rules are installed via composer, first configure phpcs to find the rules:

vendor/bin/phpcs --config-set installed_paths vendor/extdn/phpcs/Extdn
vendor/bin/phpcs --config-set installed_paths vendor/extdn/phpcs/Extdn,vendor/magento/marketplace-eqp/MEQP2

Then tests can be run like this:

Expand Down
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"require": {
"php": ">=7.1.0",
"squizlabs/php_codesniffer": "3.*"
"squizlabs/php_codesniffer": "3.*",
"magento/marketplace-eqp": "dev-master"
},
"autoload": {
"psr-4": {
Expand All @@ -31,5 +32,11 @@
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths ../../..",
"post-update-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths ../../..",
"test": "vendor/bin/phpunit"
},
"repositories": {
"meqp": {
"type": "vcs",
"url": "git@github.com:magento/marketplace-eqp.git"
}
}
}

0 comments on commit 3c26dcd

Please sign in to comment.