Skip to content

Modernize: use class constants for constant arrays (public API) #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: phpcs-4.0/feature/squiz-variablecomment-make-tagnotallowed-modular
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 21, 2025

Description

⚠️ This PR depends on PR #1039, which needs to be merged first. ⚠️


This PR modernizes various usages of constant arrays in the public API to use class constants instead of properties.

The properties still exist, but are now deprecated and will be removed in PHPCS 5.0.

In a separate PR, which will be pulled at a later point in time (but before the 4.0.0 release), the same will be done for additional usages of constant arrays, which are not in the public API.


Modernize: Util\Common: use class constant for constant array

As the property was in the public API, the class constant is also public and the property has been deprecated, to be removed in PHPCS 5.0.

Includes changing the array format from a list of strings to an associative array with the key and value for each entry holding the same string to allow for using isset() instead of in_array().

Modernize: Tokenizers\PHP: use class constant for constant array

Note: there are a couple of other properties in this class, which only contain static information. These, by nature, should be constants. however, those properties are overloading empty array properties from the abstract parent Tokenizer class, so I'm leaving those alone for now.

Modernize: AbstractVariableSniff: use class constant for constant array

As the property was in the public API, the class constant is also protected and the property has been deprecated, to be removed in PHPCS 5.0.

Modernize: Generic/CamelCapsFunctionName: use class constant for constant array

Modernize: Generic/various sniffs: use class constant for constant array

Modernize: Generic/SubversionProperties: use class constant for constant array

Modernize: PEAR/FileComment: use class constant for constant array

Note: this also affects the PEAR.Commenting.ClassComment sniff which extends this sniff.
That sniff doesn't access the deprecated property directly, but if other sniffs extend the ClassComment sniff, they may be affected.

Modernize: PEAR/ValidFunctionName: use class constant for constant array

Note: this also affects the Squiz.NamingConventions.ValidFunctionName sniff which extends this sniff.
That sniff doesn't access the deprecated property directly, but if other sniffs extend the Squiz ValidFunctionName sniff, they may be affected.

Modernize: Squiz/DisallowSizeFunctionsInLoops: use class constant for constant array

Suggested changelog entry

Deprecated:

  • PHP_CodeSniffer\Util\Common::$allowedTypes. Use PHP_CodeSniffer\Util\Common::ALLOWED_TYPES instead.
  • PHP_CodeSniffer\Tokenizers\PHP::$tstringContexts. Use PHP_CodeSniffer\Tokenizers\PHP::T_STRING_CONTEXTS instead.
  • PHP_CodeSniffer\Sniffs\AbstractVariableSniff::$phpReservedVars. Use PHP_CodeSniffer\Sniffs\AbstractVariableSniff::PHP_RESERVED_VARS instead.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::$magicMethods. Use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::MAGIC_METHODS instead.
    • This also affects the PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff class which extends the CamelCapsFunctionNameSniff.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::$methodsDoubleUnderscore. Use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::DOUBLE_UNDERSCORE_METHODS instead.
    • This also affects the PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff class which extends the CamelCapsFunctionNameSniff.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::$magicFunctions. Use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff::MAGIC_FUNCTIONS instead.
    • This also affects the PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff class which extends the CamelCapsFunctionNameSniff.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\Files\ByteOrderMarkSniff::$bomDefinitions. Use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\ByteOrderMarkSniff::BOM_DEFINITIONS instead.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\Files\InlineHTMLSniff::$bomDefinitions. Use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\InlineHTMLSniff::BOM_DEFINITIONS instead.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\CharacterBeforePHPOpeningTagSniff::$bomDefinitions. Use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\CharacterBeforePHPOpeningTagSniff::BOM_DEFINITIONS instead.
  • PHP_CodeSniffer\Standards\Generic\Sniffs\VersionControl\SubversionPropertiesSniff::$properties. Use PHP_CodeSniffer\Standards\Generic\Sniffs\VersionControl\SubversionPropertiesSniff::REQUIRED_PROPERTIES instead.
  • PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff::$tags. Use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff::EXPECTED_TAGS instead.
    • This also affects the PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff class which extends the FileCommentSniff.
  • PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff::$magicMethods. Use PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff::MAGIC_METHODS instead.
    • This also affects the PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidFunctionNameSniff class which extends the PEAR ValidFunctionNameSniff.
  • PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff::$magicFunctions. Use PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff::MAGIC_FUNCTIONS instead.
    • This also affects the PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidFunctionNameSniff class which extends the PEAR ValidFunctionNameSniff.
  • PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP\DisallowSizeFunctionsInLoopsSniff::$forbiddenFunctions. Use PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP\DisallowSizeFunctionsInLoopsSniff::FORBIDDEN_FUNCTIONS instead.

jrfnl added 9 commits April 21, 2025 06:09
As the property was in the public API, the class constant is also public and the property has been deprecated, to be removed in PHPCS 5.0.

Includes changing the array format from a list of strings to an associative array with the key and value for each entry holding the same string to allow for using `isset()` instead of `in_array()`.
Note: there are a couple of other properties in this class, which only contain static information. These, by nature, should be constants. however, those properties are overloading empty array properties from the abstract parent `Tokenizer` class, so I'm leaving those alone for now.
As the property was in the public API, the class constant is also `protected` and the property has been deprecated, to be removed in PHPCS 5.0.
Note: this also affects the `PEAR.Commenting.ClassComment` sniff which extends this sniff.
That sniff doesn't access the deprecated property directly, but if other sniffs `extend` the `ClassComment` sniff, they may be affected.
Note: this also affects the `Squiz.NamingConventions.ValidFunctionName` sniff which extends this sniff.
That sniff doesn't access the deprecated property directly, but if other sniffs `extend` the Squiz `ValidFunctionName` sniff, they may be affected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant