Skip to content

8.4 | Asymmetric Visibility support #851

Open
@DanielEScherzer

Description

@DanielEScherzer

I figured instead of putting all of the discussions on #734, it might be helpful to have a dedicated task to discuss this - let me know if I was wrong


  • Asymmetric Visibility v2

    • Proposal needed on how to handle this for the tokenizer
    • Tokenizer changes needed - property declarations
    • Tokenizer changes needed - constructor property promotion
    • Updates needed to the predefined token collections in the Tokens class
    • Updates needed to utility functions - getMemberProperties(), getMethodParameters() (for constructor property promotion)
      These updates should include special handling for the abbreviated form.
    • Updates needed to abstract sniffs ?
      This needs investigation, but I imagine the AbstractScopeSniff and the AbstractVariableSniff might need updates.
    • Sniff updates needed

Proposal:

tokenizer

  • New tokens T_PUBLIC_SET, T_PROTECTED_SET, and T_PRIVATE_SET are defined if not already defined by PHP (for use when running on lower versions)
  • They are added to PHP_CodeSniffer\Util\Tokens::$scopeModifiers and ::$contextSensitiveKeywords
  • They are added to PHP_CodeSniffer\Tokenizers\PHP::$knownLengths

getMemberProperties()

Currently:

* <code>
* array(
* 'scope' => string, // Public, private, or protected.
* 'scope_specified' => boolean, // TRUE if the scope was explicitly specified.
* 'is_static' => boolean, // TRUE if the static keyword was found.
* 'is_readonly' => boolean, // TRUE if the readonly keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer|false, // The stack pointer to the start of the type
* // or FALSE if there is no type.
* 'type_end_token' => integer|false, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the nullability
* // operator.
* );
* </code>

New:
Returns (the is_static, is_readonly, is_final, type, type_token, type_end_token, and nullable_type are unchanged and not repeated here)

Does not use asymmetric visibilitity

scope would remain one of public, private, or protected.
The get_scope, get_scope_specified, and set_scope are not included in the array if asymmetric visibility is not used.

return [
    'scope' => 'public', // or private or protected,
    'scope_specified' => true, // can also be false
    ...
];

Uses asymmetric visibility

If get and set visibility are both explicitly provided (or just set visibility public(set) is provided, and the get visibility of public is implicit) then even if the visibilities are the same asymmetric visibility handling is triggered:

  • scope is set to asymmetric
  • scope_specified is true
  • get_scope is public/protected/private
  • get_scope_specified is based on if the get scope is specified, or just implicitly public because only the set scope was specified
  • set_scope is public/protected/private [there is no set_scope_specified because it must be specified for asymmetric properties]

For the

These updates should include special handling for the abbreviated form.

Abbreviated form results in get_scope_specified being false

return [
    'scope' => 'asymmetric', // always (for asymmetric properties)
    'scope_specified' => true, // always (for asymmetric properties)
    'get_scope' => 'public', // or protected, or private
    'get_scope_specified' => true, // can also be false
    'set_scope' => 'private', // or public, or protected
    ...
];

getMethodParameters()

Currently:

* Parameters declared using PHP 8 constructor property promotion, have these additional array indexes:
* 'property_visibility' => string, // The property visibility as declared.
* 'visibility_token' => integer|false, // The stack pointer to the visibility modifier token
* // or FALSE if the visibility is not explicitly declared.
* 'property_readonly' => boolean, // TRUE if the readonly keyword was found.
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.

Does not use asymmetric visibility

Unchanged

Uses asymmetric visibility

  • property_visibility is set to asymmetric
  • visibility_token is set to false
  • new array key property_set_visibility is one of public/protected/private
  • new array key set_visibility_token is an integer for the stack pointer, can never be false
  • new array key property_get_visibility is one of public/protected/private
  • new array key get_visibility_token is an integer for the stack pointer, or false if public is implied via short form

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions