Problem/Motivation

Part of #2571965: [meta] Fix PHP coding standards in core.

This sniff relates to Constants in the Coding Standards.

Steps to reproduce

Proposed resolution

Remaining tasks

Postponed on feedback from #3358592: Add checking 'const' to Drupal.Semantics.ConstantName.ConstantStart

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mile23 created an issue. See original summary.

Mile23’s picture

Status: Fixed » Needs review
FileSize
2.16 KB

Drupal.Semantics.ConstantName.ConstantStart demands that we start all constant names with the name of the module which declares them.

Only the search module suffers here, with two improperly-named constants.

klausi’s picture

Version: 8.1.x-dev » 9.x-dev

We can only change constant names in drupal 9.

alexpott’s picture

@klausi good point - also we should look to move as many constants as possible to interfaces / classes.

andypost’s picture

Status: Needs review » Postponed

9.x

pfrenssen’s picture

Issue summary: View changes
alexpott’s picture

Version: 9.x-dev » 8.1.x-dev
Status: Postponed » Needs work

Actually we could adopt the standards and wrap these constants in @codingStandardsIgnoreStart @codingStandardsIgnoreEnd comments. And file a follow up issue to remove and fix postponed to 9.x

klausi’s picture

We could also introduce new constants with the correct name and the old constants just take the value of the new constant. Plus wrapping the old constants with @codingStandardsIgnoreStart and @deprecated.

alexpott’s picture

@klausi - I wonder how PHP optimises that - these constants are not the smallest in the world.

Mile23’s picture

Mile23’s picture

Assigned: Mile23 » Unassigned
Status: Needs work » Needs review
Issue tags: -rc eligible
FileSize
2.56 KB

It seems to me these preg strings should live with the other unicode preg string on \Drupal\Component\Utility\Unicode.

I tried moving the preg strings there, but tests kept failing and it's dinner time. Also it's not what any maintainer suggested. :-) If that's a good idea, I can work on it.

Instead this patch adds some constants with SEARCH_ prepended, and mark the other constants as deprecated.

jhodgdon’s picture

Version: 8.1.x-dev » 8.2.x-dev
Component: other » search.module
Status: Needs review » Needs work

Rather than this patch and the follow-up, I think it would be MUCH better to do the following:

a) Add new constants with conforming names (with the same definitions).

b) Change the Core code to use the new constants instead of the badly-named ones.

c) Make a change record telling that these are deprecated so contrib code can start using the new constants.

d) Then the follow-up is just to remove the deprecated constants (no code needs to change), and can be handled as part of #2716163: [META] Remove deprecated classes, methods, procedural functions and code paths outside of deprecated modules on the Drupal 9 branch.

e) As this is all search.module, let's move this issue there instead of in "other". And I think it is 8.2.x not 8.1.x at this point, right?

Mile23’s picture

So you're saying have two sets of constants which are identical but have different names?

jhodgdon’s picture

Yes. Just as when we deprecate functions, we leave the deprecated functions in place even though there is a replacement available... this seems like the right approach. But maybe we should ask one of the Core Maintainers to be sure.

catch’s picture

If we only make the change in 9.x, then it's a bc break which you can't prepare for in 8.x

If we make it in 8.x, then 9.x just has to delete some lines.

So +1 to #12.

alexpott’s picture

Let's do this properly and improve the Search module first and then we can sort out the coding standards. See #2719961: Create SearchText service to deprecate procedural code in search.module

jhodgdon’s picture

Status: Needs work » Postponed

So we should postpone this?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue summary: View changes

This was postponed on #2719961: Create SearchText service to deprecate procedural code in search.module which was later marked as a duplicate of #3075703: Move search text processing to a service, which is fixed. Unpostponing.

I ran phpcs with the sniff and it only found 3 deprecated constants in search.module. So, it seems like this should stay postponed and add the sniff once these are removed in Drupal 10.

FILE: /var/www/html/core/modules/search/search.module
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 23 | WARNING | All constants defined by a module must be prefixed with the module's name, expected "SEARCH_PREG_CLASS_NUMBERS" but found "PREG_CLASS_NUMBERS"
 34 | WARNING | All constants defined by a module must be prefixed with the module's name, expected "SEARCH_PREG_CLASS_PUNCTUATION" but found "PREG_CLASS_PUNCTUATION"
 54 | WARNING | All constants defined by a module must be prefixed with the module's name, expected "SEARCH_PREG_CLASS_CJK" but found "PREG_CLASS_CJK"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
andypost’s picture

Status: Postponed » Needs work

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Needs review
FileSize
466 bytes

The constants in the search.module were removed in #3261239: Remove deprecations from search module. I think that the sniff can be enabled now.

quietone’s picture

Component: search.module » other
smustgrave’s picture

Status: Needs review » Needs work

Assuming I tested correctly

I applied the patch in #33

Added define('TEST', 'Hello'); to the action.module and ran the commit-code-check.sh script. It caught the error

But if I changed it to CONST TEST = 'Hello'; it does not catch it and shows it passed.

quietone’s picture

Issue summary: View changes
Status: Needs work » Postponed

The doc bloc for the sniff explains that it only checks define. And the earlier comments in the issue reflect that, there is no discussion about sniffing constants defined with 'const'.

I made an issue in coder to get some information. I am postponing this until there is feedback there to inform what to do next here.
#3358592: Add checking 'const' to Drupal.Semantics.ConstantName.ConstantStart

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.