Closed michalbundyra closed 5 years ago
Allowed, but not required.
Exactly, I agree
Why you think what space between parentheses and colon makes more sense (even in RFC no spaces in examples))? Awaiting PSR12 & ZCS ;)
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 doctrine/coding-standard
also enforces the ) : foo {
style
Well, if such expert "mastodon" like Ocramius for space, then I am fully convinced. ))
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 function(Foo $bar): Baz {}
.
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
The function Foo should return: Bar
not
The function Foo should return : Bar
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 function(Foo $bar): Baz {}.
You do see it with ternary statements, however: $x ? $y : $z
.
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 :
when defining return types; the WG for the spec is debating that, and several other items, currently.)
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