-
Notifications
You must be signed in to change notification settings - Fork 8
[WIP] Extended Zend Framework Coding Standard #1
Conversation
I'm a bit worried about this. This adds a lot of restrictions. I do think that most of them are a good thing, since there is a lot of coding style inconsistency in the current code base. However it's going to be a lot of work to fix all the code in all packages. With the 75 PR's that koopz opened last night, only 19 passed the new tests. I do think that if you change the ruleset it should be done in one go. One addition that might be useful: Add a sniff for the license header which could replace this PR and add it in one go for all packages. Also I like to see tests so we know the sniffs are working as intended. And if some sniffs are changed in PhpCodeSniffer, we can adjust accordingly. Maybe a daily travis test can help out with this. |
tbh i could've probably avoided most of the currently failing PRs (most of them are due to the newly introduced rule to have one space after the NOT operator) but i'm gonna work my way through them. I like @xtreamwayz' idea for the license header sniff. It should help us getting those updates through way faster. Most of the Sniffs seem to have the ability to automatically fix issues which would help a lot in case they're getting merged. |
Don't do this. Add exclusion rules for these files or add We can also introduce new methods to which the originals proxy, but we do not want to break compatibility just to make the code pass the standards at this point. |
Can't you mark some tests as warnings so they will be displayed, but won't fail the tests? |
phpcs returns error code 1 once a warning shows up and causes the build to fail. |
I just realized something regarding that License Header Sniff: |
- array(type1 => type2)
- added PHPUnit configuration - added Travis configuration - added Coveralls configuration - updated phpcs.xml to exclude .inc test files - updated .gitignore - added composer.lock - added .gitattributes - added abstract TestCase for Sniff tests
Hey, if you use this ruleset and you have annotations like: /**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", nullable=false, options={"unsigned"=true})
*/
protected $id; phpcs tells you that use Doctrine\ORM\Mapping as ORM; isn`t used. Seems like Annotations aren´t checked. Also the following isnt working: @ORM\Column(type="integer", tag is not allowed in member variable comment |
- added Squiz.Operators.ValidLogicalOperators - removed Squiz.WhiteSpace.CastSpacing.ContainsWhiteSpace - covered by ZendCodingStandard.PHP.TypeCasting
This sniffs cover 3 other sniffs (now excluded in ruleset): - Generic.PHP.LowerCaseKeyword - Squiz.ControlStructures.LowercaseDeclaration - Squiz.Functions.LowercaseFunctionKeywords
@codingstandards* tags are deprecated since PHP_CodeSniffer 3.2.0 We want to use only @phpcs: notation. We can't automatically change it in files which are whole ignored, and it should be changed there manually.
One space is required before and the => cannot be at the end of the line
I know, but currently it in Review status https://www.php-fig.org/psr/ where:
|
@shandyDev
Allowed, but not required. As such, we'd still comply. (Remember, this coding standard is for the ZF repos, and for enforcing consistency within our projects; we're choosing to enforce one way to handle imports. That choice is compatible with PSR-12.)
This one is currently being debated, with Larry Garfield and several others reviewing the spec suggesting that the format we use here makes more sense. Since this particular point is still being debated, we will leave it as-is, and revisit once it goes to a vote. |
Exactly, I agree Why you think what space between parentheses and colon makes more sense (even in RFC no spaces in examples))? |
Mostly for readability purposes. It's a heated topic, like most CS rules, but |
Well, if such expert "mastodon" like Ocramius for space, then I am fully convinced. )) |
Better analyze if throw tag should be added on the function
You would never see a space before ":" while writing which makes it feels really "unnatural" to do so. Atleast for me. That's why my vote is for There's also no space before ":" in a switch case, before ":" in if (): (short syntax) nor before a scope resolution operator (::). If you would describe the function in writing you would type |
You do see it with ternary statements, however: Personally, I find the whitespace surrounding it makes it harder to accidentally omit when adding your return type, leading to fewer mistakes. Regardless of either of our own personal preferences, we will follow the guidelines of PSR-12 once it's finalized. (Interestingly, this is one of the very topics that forced it back into draft status, as quite a number of people prefer spaces surrounding the |
Is this still a thing since #5 (that seems to fulfill the same purpose) was merged? |
It's not. All work done here has been moved to https://github.com/webimpress/coding-standard |
Work in progress
Just want to know what you guys thinks about it.
Basically @weierophinney asked me to add just detection for unused imports.
I went "a bit" more far with it and I've added much more rules.
I found easy way how we can write custom rules and we don't need to copy them to Standards directory.
List what we have there now:
Generic.Arrays.DisallowLongArraySyntax
,Generic.Formatting.SpaceAfterCast
(must be one space after cast:(bool) $var
),Generic.Formatting.SpaceAfterNot
,Generic.CodeAnalysis.EmptyStatement
,Generic.CodeAnalysis.ForLoopShouldBeWhileLoop
,Generic.CodeAnalysis.UnconditionalIfStatement
,Generic.CodeAnalysis.UnnecessaryFinalModifier
,Generic.NamingConventions.ConstructorName
,Generic.PHP.ForbiddenFunctions
,Generic.Files.OneTraitPerFile
,Generic.Strings.UnnecessaryStringConcat
,Squiz.Operators.ValidLogicalOperators
,Squiz.PHP.LowercasePHPFunctions
,Squiz.PHP.NonExecutableCode
,Squiz.Scope.StaticThisUsage
,Squiz.Strings.ConcatenationSpacing
,Squiz.Strings.DoubleQuoteUsage
,Squiz.WhiteSpace.LanguageConstructSpacing
,Squiz.WhiteSpace.OperatorSpacing
,Squiz.WhiteSpace.SemicolonSpacing
,Squiz.WhiteSpace.SuperfluousWhitespace
,ZendCodingStandard.Arrays.Format
(no space after[
and before]
, each element in new line for multi-line arrays, no empty lines, correct indents...),ZendCodingStandard.Arrays.TrailingArrayComma
(requires comma after last element in multiline arrays),ZendCodingStandard.Classes.AlphabeticallySortedTraits
,ZendCodingStandard.Classes.NoNullValues
- removenull
values for class properties,ZendCodingStandard.Classes.TraitUsage
,ZendCodingStandard.Commenting.DocComment
,ZendCodingStandard.Commenting.FunctionComment
- order of tags and empty lines between tags,ZendCodingStandard.Commenting.FunctionDataProviderTag
,ZendCodingStandard.Commenting.FunctionDisallowedTag
- using some tags is disallowed, for example@expectedException*
from PHPUnit annotations should be replaced with according function calls just before place, where the exception should be thrown,ZendCodingStandard.Commenting.NoInlineCommentAfterCurlyClose
,ZendCodingStandard.Commenting.TagCase
,ZendCodingStandard.Commenting.VariableComment
,ZendCodingStandard.Formatting.DoubleColon
- no spaces before and after,ZendCodingStandard.Formatting.NewKeyword
- one space after,ZendCodingStandard.Formatting.NoSpaceAfterSplat
- no space after...
,ZendCodingStandard.Formatting.Reference
- no space after reference operator&$var
,ZendCodingStandard.Formatting.ReturnType
(formating of return type on PHP 7 and 7.1 with nullable types),ZendCodingStandard.Formatting.UnnecessaryParentheses
(remove unnecessary parentheses where it does not misaligned the readability),ZendCodingStandard.Functions.Param
- checks method params and tags - required PHPDocs tags only when type hint is not provided or type hint needs specification (array
,iterable
,\Traversable
),ZendCodingStandard.Functions.ReturnType
- checks method return statement and return type, tag is required only when return type is not declared or needs specification (array
,iterable
,\Traversable
), if function returns void, return tag must be omitted,ZendCodingStandard.Functions.Throws
- checks throws tags and throw statement in the function - throw tag must be declared for each exception thrown explicitly in the function,ZendCodingStandard.Methods.LineAfter
(must be exactly one line after method in class, zero after last method in the class),ZendCodingStandard.Namespaces.AlphabeticallySortedUses
,ZendCodingStandard.Namespaces.ConstAndFunctionKeywords
- lower case keywords and single space after,ZendCodingStandard.Namespaces.UnusedUseStatement
(detects unused use statements),ZendCodingStandard.Namespaces.UseDoesNotStartWithBackslash
,ZendCodingStandard.NamingConventions.ValidVariableName
(all variables should be camelCase style),ZendCodingStandard.Operators.BooleanOperator
(boolean operators are not allowed at the end of the line),ZendCodingStandard.Operators.TernaryOperator
(if multi-line, each part should be in new line starting from operator?
or:
),ZendCodingStandard.PHP.CorrectClassNameCase
(checks case for defined classes),ZendCodingStandard.PHP.ImportInternalConstant
(import php built-in constants),ZendCodingStandard.PHP.ImportInternalFunction
(import php built-in functions),ZendCodingStandard.PHP.InstantiatingParenthesis
(PSR-12 - always parenthesis when instantiating a new class),ZendCodingStandard.PHP.LowerCaseKeyword
(extendsGeneric.PHP.LowerCaseKeyword
),ZendCodingStandard.PHP.RedundantSemicolon
(unnecessary semicolons - for example after closing curly bracket of "if" statement),ZendCodingStandard.PHP.TypeCasting
(disallow!!
casting, require(bool)
and(int)
instead of(boolean)
and(integer)
accordingly, disallow(unset)
cast, always lowercase),ZendCodingStandard.Strings.NoConcatenationAtTheEnd
,ZendCodingStandard.WhiteSpace.BlankLine
(only one blank line allowed between parts of the code),ZendCodingStandard.WhiteSpace.CommaSpacing
(no space before comma, one after, allowed more than one in multidimensional arrays),ZendCodingStandard.WhiteSpace.NoBlankLineAtStart
(disallow blank line(s) at start ofclass
,closure
,function
,interface
andtrait
),ZendCodingStandard.WhiteSpace.ScopeIndent
(experimental, stricter rules of indents).I would like to add also some more rules/sniffs:
use
keyword per declaration -ZendCodingStandard.Classes.TraitUsage
,ZendCodingStandard.Classes.TraitUsage
,ZendCodingStandard.Classes.AlphabeticallySortedTraits
,ZendCodingStandard.Namespaces.AlphabeticallySortedUses
,ZendCodingStandard.Namespaces.UnusedUseStatement
,const
andfunction
keywords -ZendCodingStandard.Namespaces.ConstAndFunctionKeywords
,const
andfunction
keywords -ZendCodingStandard.Namespaces.ConstAndFunctionKeywords
,ZendCodingStandard.Namespaces.UseDoesNotStartWithBackslash
,ZendCodingStandard.WhiteSpace.NoBlankLineAtStart
,ZendCodingStandard.WhiteSpace.BlankLine
,DateTime
instead ofDatetime
) -ZendCodingStandard.PHP.CorrectClassNameCase
,ZendCodingStandard.Strings.NoConcatenationAtTheEndSniff
,ZendCodingStandard.Operators.BooleanOperator
,@dataProvider
:test...
methods,@param
tags,@dataProvider ...
,or
andand
for logical operators (use||
and&&
accordingly) -Squiz.Operators.ValidLogicalOperators
,Methods with the same name as their class will not be constructors in a future version
) -Generic.NamingConventions.ConstructorName
,ZendCodingStandard.Arrays.Format
:[ 1, 2, 3 ]
),...$var
) -ZendCodingStandard.Formatting.NoSpaceAfterSplat
,ClassName::class
) -ZendCodingStandard.Formatting.DoubleColon
,new
-ZendCodingStandard.Formatting.NewKeyword
,if
curly closing bracket) -ZendCodingStandard.PHP.RedundantSemicolon
,&
-ZendCodingStandard.Formatting.Reference
.Of course would be nice to have also tests for all of our custom sniffs...
So as you can see it's quite long list. Can you think about anything else what will be worth to add?
Let me know what do you think. Thanks!
/cc @weierophinney @Koopzington @xtreamwayz