Skip to content

PHP 8.0 compatibility: adjust sniffs to allow for new "namespaced name" tokens. #1226

@jrfnl

Description

@jrfnl

In PHP 8.0 identifier name tokenization will change as outline in the accepted RFC "Treat namespaced names as single token".

Aside from adding new sniffs to detect code affected by this change, the codebase of PHPCompatibility will need to be updated for this as well.

All sniffs which, in one way or another, look at T_STRING tokens should be reviewed to make sure they will work correctly on PHP 8.0 and most of them will need adjusting to take the new tokens into account.

PHPCSUtils has been updated already for this change and all current functionality in PHPCSUtils take the new tokens into account as of version 1.0.0-alpha4 (expected soon).

That version will also contain some helpers, most notably:

  • A new Collections::nameTokens() method which, in most cases, code looking for T_STRING should be switched to.
  • A new UtilityMethodTestCase::usesPhp8NameTokens() method to help tests figure out which type of tokenization to expect.

All functionality which will be added to PHPCSUtils after this should be compatible with the new tokens, so using PHPCSUtils solves half the problem.

Note: this is a complex change as we have to account for all the different tokenization possibilities.

At this time the PHP 8 tokenization should be expected on all PHPCS versions when run on PHP 8.

PHP PHPCS 2.x PHPCS 3.x PHPCS 4.x
PHP 5, 7
PHP >= 8 ✔️ ✔️ ✔️

However, this is expected to change in the near future.

Once the above mentioned PRs have been merged, the matrix of when to expect which tokenization will change to this:

PHP PHPCS 2.x PHPCS 3.x < 3.5.7 PHPCS 3.x >= 3.5.7 PHPCS 4.x
PHP 5, 7 ✔️
PHP >= 8 ✔️ ✔️ ✔️

Refs:


Update:

In the mean time the PR for PHPCS 3.x was merged and has been released as part of PHPCS 3.5.7.
The PR for PHPCS 4.x has also been pulled and merged by now, so will be included in PHPCS 4.0.0.

PHPCSUtils 1.0.0-alpha4 is fully prepared for PHPCS cross-version compat regarding these tokens for PHPCS 2.6.0 - 4.x.

Ref:


Task list (not necessarily complete):

To do/to review:

  • Sniff
  • AbstractFunctionCallParameterSniff
  • PHPCompatibility.Classes.NewClasses
  • PHPCompatibility.Classes.NewLateStaticBinding
  • PHPCompatibility.Classes.RemovedClasses
  • PHPCompatibility.ControlStructures.NewExecutionDirectives
  • PHPCompatibility.Extensions.RemovedExtensions
  • PHPCompatibility.FunctionUse.NewFunctionParameters
  • PHPCompatibility.FunctionUse.NewNamedParameters
  • PHPCompatibility.FunctionUse.RemovedFunctionParameters
  • PHPCompatibility.FunctionUse.RequiredToOptionalFunctionParameters
  • PHPCompatibility.Generators.NewGeneratorReturn
  • PHPCompatibility.IniDirectives.NewIniDirectives
  • PHPCompatibility.IniDirectives.RemovedIniDirectives
  • PHPCompatibility.InitialValue.NewConstantArraysUsingDefine
  • PHPCompatibility.MethodUse.ForbiddenToStringParameters
  • PHPCompatibility.MethodUse.NewDirectCallsToClone
  • PHPCompatibility.Miscellaneous.NewPHPOpenTagEOF
  • PHPCompatibility.Numbers.ValidIntegers
  • PHPCompatibility.ParameterValues.NewHashAlgorithms
  • PHPCompatibility.ParameterValues.RemovedHashAlgorithms
  • PHPCompatibility.Syntax.NewFlexibleHeredocNowdoc
  • PHPCompatibility.Syntax.NewFunctionCallTrailingComma
  • PHPCompatibility.Syntax.NewNestedStaticAccess

Changes prepped to support PHPCS 4.0 (to be pulled soonish (Aug 2025)):

  • PHPCompatibility\Helpers\MiscHelper::isUseOfGlobalConstant()
  • PHPCompatibility.Constants.NewConstants
  • PHPCompatibility.Constants.NewMagicClassConstant
  • PHPCompatibility.Constants.RemovedConstants
  • PHPCompatibility.ControlStructures.ForbiddenBreakContinueVariableArguments
  • PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue
  • PHPCompatibility.FunctionUse.ArgumentFunctionsUsage
  • PHPCompatibility.FunctionUse.NewFunctions
  • PHPCompatibility.FunctionUse.RemovedFunctions
  • PHPCompatibility.InitialValue.NewConstantScalarExpressions
  • PHPCompatibility.Interfaces.NewInterfaces
  • PHPCompatibility.Keywords.ForbiddenNames
  • PHPCompatibility.Keywords.NewKeywords
  • PHPCompatibility.LanguageConstructs.NewLanguageConstructs
  • PHPCompatibility.Numbers.RemovedHexadecimalNumericStrings
  • PHPCompatibility.ParameterValues.ForbiddenStripTagsSelfClosingXHTML
  • PHPCompatibility.ParameterValues.NewArrayReduceInitialType
  • PHPCompatibility.ParameterValues.NewFopenModes
  • PHPCompatibility.ParameterValues.NewPackFormat
  • PHPCompatibility.ParameterValues.NewProcOpenCmdArray
  • PHPCompatibility.ParameterValues.NewStripTagsAllowableTagsArray
  • PHPCompatibility.ParameterValues.RemovedImplodeFlexibleParamOrder
  • PHPCompatibility.ParameterValues.RemovedLdapConnectSignatures
  • PHPCompatibility.ParameterValues.RemovedMbstringModifiers
  • PHPCompatibility.ParameterValues.RemovedSetlocaleString
  • PHPCompatibility.Syntax.ForbiddenCallTimePassByReference
  • PHPCompatibility.Syntax.NewClassMemberAccess
  • PHPCompatibility.Syntax.NewDynamicAccessToStatic
  • PHPCompatibility.Syntax.NewFunctionArrayDereferencing
  • PHPCompatibility.Syntax.RemovedCurlyBraceArrayAccess
  • PHPCompatibility.Variables.RemovedIndirectModificationOfGlobals

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions