Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#33 | 2714815-33.patch | 466 bytes | quietone |
| |||
#11 | 2714815_11.patch | 2.56 KB | Mile23 |
#2 | 2714815_2.patch | 2.16 KB | Mile23 |
Comments
Comment #2
Mile23Drupal.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.
Comment #3
klausiWe can only change constant names in drupal 9.
Comment #4
alexpott@klausi good point - also we should look to move as many constants as possible to interfaces / classes.
Comment #5
andypost9.x
Comment #6
pfrenssenComment #7
alexpottActually 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.xComment #8
klausiWe 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.
Comment #9
alexpott@klausi - I wonder how PHP optimises that - these constants are not the smallest in the world.
Comment #10
Mile23Added a follow-up: #2716741: Update search module's PREG_CLASS_NUMBERS, PREG_CLASS_PUNCTUATION, PREG_CLASS_CJK to CS
Comment #11
Mile23It 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.Comment #12
jhodgdonRather 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?
Comment #13
Mile23So you're saying have two sets of constants which are identical but have different names?
Comment #14
jhodgdonYes. 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.
Comment #15
catchIf 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.
Comment #16
alexpottLet'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
Comment #17
jhodgdonSo we should postpone this?
Comment #28
quietone CreditAttribution: quietone as a volunteer commentedThis 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.
Comment #29
andypostComment #33
quietone CreditAttribution: quietone at PreviousNext commentedThe constants in the search.module were removed in #3261239: Remove deprecations from search module. I think that the sniff can be enabled now.
Comment #34
quietone CreditAttribution: quietone at PreviousNext commentedComment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedAssuming 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.
Comment #36
quietone CreditAttribution: quietone at PreviousNext commentedThe 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