Skip to content

Commit 061f546

Browse files
author
Leon Helmus
committed
Added magento/magento-coding-standard to keep closer to Magento standards.
* Added support for phtml templates. * Added Fisheye coding standards. * Updated current rules and added comments why it was added.
1 parent 094be80 commit 061f546

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

CHANGELOG.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 2.0.0
8+
### Added
9+
- Composer package youwe/coding-standard which is now included in the Youwe coding standards for Magento 2.
10+
- Comments for why the ruleset was added.
11+
- Coding standards for phtml templates.
12+
- Package [magento/magento-coding-standard](https://github.com/magento/magento-coding-standard) as a requirement.
13+
The version v25 is used as a base. If not compatible please update the `phpcs.xml` in project root.
14+
15+
### Removed
16+
- Rule PSR2.Methods.MethodDeclaration.Underscore since this is already excluded by Magento.
17+
- Argument colors and added it in the common Youwe coding standards.
18+
719
## 1.7.0 - 2021-12-24
820
### Changed
921
- Vendor from MediactMagento2 to Magento2
1022
- Copyrights.
1123

1224
### Removed
13-
- Dependency squizlabs/php_codesniffer.
25+
- Dependency squizlabs/php_codesniffer.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ a workable and fast standard.
99
Use composer to require the standard in a project.
1010

1111
```shell
12-
composer require --dev youwe/coding-standard-magento2
12+
composer require --dev youwe/coding-standard-magento2 -W
1313
```
1414

1515
To let PHPCS know that this standard should be used add a phpcs.xml file in the
@@ -18,7 +18,7 @@ root of the project.
1818
```xml
1919
<?xml version="1.0"?>
2020
<ruleset>
21-
<rule ref="./vendor/youwe/coding-standard-magento2/src/YouweMagento2"/>
21+
<rule ref="YouweMagento2"/>
2222
</ruleset>
2323
```
2424

src/YouweMagento2/ruleset.xml

+66-10
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,100 @@
33
/**
44
* Copyright Youwe. All rights reserved.
55
* https://www.youweagency.com
6+
*
7+
* TODO:: Create a separate ruleset for phtml files, since a lot of exceptions need to be made just for adding phtml to code sniffer.
68
*/
79
-->
810
<ruleset name="YouweMagento2">
911
<description>Youwe coding standards for Magento2 websites and modules</description>
10-
11-
<arg name="colors" />
12-
<arg name="extensions" value="php/PHP,phtml/PHP,js/JS,css/CSS" />
12+
<!-- TODO:: Check if these values are correct. Did not see anything about how to configure this in documentation -->
13+
<arg name="extensions" value="php/PHP,phtml/PHP,js/JS,css/CSS,xml/XML,less/CSS" />
1314

1415
<!-- Base rules on Youwe -->
1516
<rule ref="Youwe">
17+
<!-- Magento 2 does not use return types everywhere in the code base yet. So we cannot make this mandatory. -->
1618
<exclude name="GlobalCommon.Php7.ReturnType" />
17-
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
19+
<!-- Magento 2 still uses $_ to tell if it's protected, so we cannot mandate this. -->
20+
<exclude name="GlobalCommon.NamingConventions.ValidVariableName" />
21+
<!-- TODO:: Check if this rule still needs to be excluded. Seems logical that all classes should start with camelcase -->
1822
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
23+
<!--TODO:: Check if this should not be excluded. Seems logical to add throws in function comment. -->
1924
<exclude name="Squiz.Commenting.FunctionComment.EmptyThrows" />
25+
<!-- Magento 2 still does not strict type arguments of functions. This is why this rule is excluded. -->
2026
<exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing" />
27+
<!-- Magento 2 still does not strict type arguments of functions. This is why this rule is excluded. -->
2128
<exclude name="Squiz.Commenting.FunctionComment.TypeHintMissing" />
2229
</rule>
2330

2431
<!-- Import most rules from official Magento2 coding standard -->
2532
<rule ref="Magento2">
33+
<!-- Excluded because short descriptions are not mandatory in Youwe for a method. -->
2634
<exclude name="Magento2.Annotation" />
35+
<!--
36+
Excluded because in PHP 8.1 it's already possible to declare what type it is.
37+
Also most class properties are self-explanatory.
38+
-->
2739
<exclude name="Magento2.Commenting.ClassPropertyPHPDocFormatting" />
2840
</rule>
2941

30-
<rule ref="Generic.Files.LineLength.TooLong">
31-
<exclude-pattern>*.phtml,*.xml</exclude-pattern>
42+
<!-- Detects function that use a for-loops call in their function. -->
43+
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed">
44+
<exclude-pattern>*.phtml</exclude-pattern>
3245
</rule>
3346

34-
<rule ref="Squiz.ControlStructures.ControlSignature">
47+
<rule ref="Generic.Commenting.DocComment">
48+
<!-- default Magento 2 templates also not cohere to this. -->
3549
<exclude-pattern>*.phtml</exclude-pattern>
3650
</rule>
3751

38-
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace">
52+
<rule ref="Generic.ControlStructures.InlineControlStructure.NotAllowed">
53+
<!-- Magento 2 templates do not cohere to this rule. -->
3954
<exclude-pattern>*.phtml</exclude-pattern>
4055
</rule>
4156

42-
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed">
57+
<rule ref="Generic.Files.LineLength.TooLong">
58+
<!-- Magento 2 phtml & xml both have long lines think of checkout_index_index.xml -->
59+
<exclude-pattern>*.phtml</exclude-pattern>
60+
<exclude-pattern>*.xml</exclude-pattern>
61+
</rule>
62+
63+
<rule ref="PEAR.Functions.FunctionCallSignature">
64+
<!-- default Magento 2 templates also not cohere to this. -->
4365
<exclude-pattern>*.phtml</exclude-pattern>
4466
</rule>
4567

4668
<rule ref="PSR2.Classes.PropertyDeclaration.Underscore">
69+
<!-- In some cases Magento 2 still uses underscores for their properties. -->
70+
<exclude-pattern>*.phtml</exclude-pattern>
71+
</rule>
72+
73+
<rule ref="PSR2.ControlStructures.ControlStructureSpacing">
74+
<!-- default Magento 2 templates also not cohere to this. -->
75+
<exclude-pattern>*.phtml</exclude-pattern>
76+
</rule>
77+
78+
<rule ref="PSR12.ControlStructures.ControlStructureSpacing">
79+
<!-- default Magento 2 templates also not cohere to this. -->
80+
<exclude-pattern>*.phtml</exclude-pattern>
81+
</rule>
82+
83+
<rule ref="PSR12.Files.FileHeader.SpacingAfterBlock">
84+
<!-- default Magento 2 templates also not cohere to this. -->
85+
<exclude-pattern>*.phtml</exclude-pattern>
86+
</rule>
87+
88+
<rule ref="PSR12.Operators.OperatorSpacing">
89+
<!-- default Magento 2 templates also not cohere to this. -->
90+
<exclude-pattern>*.phtml</exclude-pattern>
91+
</rule>
92+
93+
<rule ref="Squiz.ControlStructures.ControlSignature">
94+
<!-- Magento 2 uses a different notation for patterns like do { / if { for phtml that's why this was excluded. -->
95+
<exclude-pattern>*.phtml</exclude-pattern>
96+
</rule>
97+
98+
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing">
99+
<!-- default Magento 2 templates also not cohere to this. -->
47100
<exclude-pattern>*.phtml</exclude-pattern>
48101
</rule>
49102

@@ -53,5 +106,8 @@
53106
</properties>
54107
</rule>
55108

56-
<rule ref="PSR12.Functions.ReturnTypeDeclaration" />
109+
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace">
110+
<!-- Magento 2 phptml files end differently and use endif; for example. -->
111+
<exclude-pattern>*.phtml</exclude-pattern>
112+
</rule>
57113
</ruleset>

0 commit comments

Comments
 (0)