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

Include and document MEQP2.Templates.ThisInTemplate #40

Merged
merged 3 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: php
php:
- 7.1
- 7.2
before_install:
- openssl aes-256-cbc -K $encrypted_2b7e0505597a_key -iv $encrypted_2b7e0505597a_iv -in .travis/deploy_rsa_encrypted -out ~/.ssh/id_rsa -d
- chmod 600 ~/.ssh/id_rsa
install: composer install --no-interaction --prefer-source
script:
- vendor/bin/phpunit
Binary file added .travis/deploy_rsa_encrypted
Binary file not shown.
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": "git",
"url": "https://github.com/magento/marketplace-eqp.git"
}
}
}