Add sniff for declare(strict_types=1);

Created on 11 December 2023, 12 months ago
Updated 9 June 2024, 6 months ago

Problem/Motivation

#3303206: Define a standard for adding declare(strict_types=1) introduces a new coding standard for declare strict statement, enforcing a specific format and location.

Change record https://www.drupal.org/node/3402544

Proposed resolution

Introduce SlevomatCodingStandard.TypeHints.DeclareStrictTypes to check for this standard, only applying this sniff when a strict type is set, so we don't enforce the usage of strict typing.

Feature request
Status

Fixed

Version

8.3

Component

Coder Sniffer

Created by

🇳🇱Netherlands arkener

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @arkener
  • Status changed to Needs review 12 months ago
  • 🇳🇱Netherlands arkener

    MR: https://www.drupal.org/project/coder/issues/3407995 Add sniff for declare(strict_types=1); Needs review

  • 🇬🇧United Kingdom jonathan1055

    You linked back to this same issue :-)

    Here is your PR https://github.com/pfrenssen/coder/pull/217

  • 🇬🇧United Kingdom jonathan1055

    I have a question on the added line declare(strict_types=1); in test file . I don't think we need that, because declare(strict_types=1); is already added to the file. The comment for good.module says 'It is used to check sniffs that run on .module files but skip .php files' so we are already covered.

    Also regarding the tests, I know that its a third-party sniff, but would it be useful to add an incorrect 'declare' and check that it is fixed in the .fixed file? We don't need to do in-depth testing of the sniff, but I think it would be beneficial if our tests showed that it is being applied and the fixer works as expected.

    • Arkener authored eb31ae91 on 8.3.x
      feat(DeclareStrictTypes): Add Slevomat sniff for declare strict_types (#...
  • Status changed to Fixed 10 months ago
  • 🇦🇹Austria klausi 🇦🇹 Vienna

    Merged, thanks!

    We could be more fancy with the testing, but I think it is ok to rely on upstream Slevomat to do it fully.

  • 🇬🇧United Kingdom jonathan1055

    Thanks for merging.

    I'm fine with your answer to my second question in #4

    But did you have an opinion on my first question? That's been committed now, but I don't think that line was needed, and now seems confusing in that file.

  • 🇦🇹Austria klausi 🇦🇹 Vienna

    I think it is fine to have it also in good.module to mirror what is in bad.module, so can stay as is.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇦🇺Australia dpi Perth, Australia

    Note, the changeset introduced here prevents enforcing declare(strict_types) on files.

    Per

    <exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing"/>
    

    in https://git.drupalcode.org/project/coder/-/commit/eb31ae916dcb6221e8783a...

    To bring back the requirement, add this line:

        <rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing">
            <severity>5</severity>
        </rule>
    
  • 🇬🇧United Kingdom jonathan1055

    Hi @dpi, just for clarity, is this a hint for contrib modules who want to enforce the sniff? Or are you saying that instead of <exclude> the change to Coder's ruleset.xml should have been to use <severity>0</severity>? Looking at that file, every other sniff that is not enabled uses severity 0, this is the only one which uses exclude name=

    If there is work to be done, then a maintainer will have to re-open this issue, or maybe we start a new one?

  • 🇦🇺Australia dpi Perth, Australia

    its a hint for both contrib, but primarily private projects who extend coder.

    Private projects may choose to force strict types, and anyone who did so before this change will suddenly find that even if they have the rule declared, Coders' declaration overrides theirs. So projects and contrib need to make their declaration more specific, in order to override Coders.

    Its more a PSA than anything. But, perhaps (unlikely) there was a way for coder to introduce this change without using the exclude directive.

  • 🇬🇧United Kingdom jonathan1055

    Thanks for the clarity.

    But, perhaps (unlikely) there was a way for coder to introduce this change without using the exclude directive.

    Would using severity 0instead of exclude have been better for this situation?

  • 🇦🇺Australia dpi Perth, Australia

    Didnt work.

    I think somewhere behind the scenes, in PHPCS, exclude translate to a regular <rule ...><severity>0 declaration.

  • 🇬🇧United Kingdom jonathan1055

    Didnt work.

    What didn't work? Do you mean that you tried the suggested change to Coder's ruleset.xml in #12 and #14 and it was still not any better for your situation?

  • 🇦🇺Australia dpi Perth, Australia

    exclude didnt work. Like I said, it seems to just translate to a 0 declaration under the hood.

Production build 0.71.5 2024