Repository navigation
Conversation
PHP tokenizes a catch type list (catch (ValueError|TypeError $e)) with plain T_BITWISE_OR tokens, so PSR12.Operators.OperatorSpacing read it as a bitwise operator and demanded a space on both sides - the opposite of the type separator rule from Universal.Operators.TypeSeparatorSpacing, which only listens for T_TYPE_UNION and never sees a catch list. Downstream projects such as cakephp/cakephp had to silence both NoSpaceBefore and NoSpaceAfter in their phpcs.xml, which also silenced the real bitwise operators. The new PhpCollective.Operators.OperatorSpacing extends the PSR12 sniff, keeps every inherited check, and re-rules the catch-list | like a union type separator: no space, fixable. The ruleset swaps in the subclass and excludes the vendor sniff. Closes php-collective#88.
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #88.
Problem
A multi-catch type list is written without spaces, exactly like a union type:
PHP tokenizes the catch list with plain
T_BITWISE_ORtokens (notT_TYPE_UNION), soUniversal.Operators.TypeSeparatorSpacingnever sees it while the inheritedPSR12.Operators.OperatorSpacingreads it as a bitwise operator and demands a space on both sides - the opposite of the type separator rule. Downstream projects such as cakephp/cakephp#19740 had to silence both PSR12 codes in theirphpcs.xml, and that also silenced the real bitwise operators.Fix
A new
PhpCollective.Operators.OperatorSpacingextends the PSR12 sniff. It keeps every inherited check and treats the catch-list|like a union type separator: no space, fixable. The|inside the catch parenthesis is recognized via the innermost enclosing parenthesis being owned by thecatchkeyword, so a real bitwise operator in the catch body (and a DNF&inside the catch list) keeps the inherited spacing behavior.The ruleset swaps in the subclass and excludes the vendor
PSR12.Operators.OperatorSpacing.Verification
catch (ValueError|TypeError $e)-> no operator errors;catch (ValueError | TypeError $e)-> 2 fixablePhpCollective.Operators.OperatorSpacing.SpaceBefore/SpaceAftererrors, and phpcbf strips the spaces$a|$bin the catch body still reports the inherited NoSpaceBefore/NoSpaceAfter|gets the type separator rule, the inner&keeps the inherited rule