Skip to content

New warning about function spacing since version 3.12.0 #945

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

Closed
3 of 4 tasks
ahtux opened this issue Apr 8, 2025 · 3 comments · Fixed by #946
Closed
3 of 4 tasks

New warning about function spacing since version 3.12.0 #945

ahtux opened this issue Apr 8, 2025 · 3 comments · Fixed by #946

Comments

@ahtux
Copy link

ahtux commented Apr 8, 2025

Describe the bug

Since version 3.12.0 PHP CodeSniffer complains about missing blank lines before a function.
You can see the minimalized code example below. There are clearly blank lines before the function.
Additionally it is the first function and due to the rule it also shouldn't complain.
Also see discussion

Code sample

<?php
/**
 * Description of file
 *
 * @package foo
 */


/**
 * Description of function
 */
function foo($x)
{
}

Custom ruleset

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="PHPCS rule set">
    <rule ref="Squiz.WhiteSpace.FunctionSpacing">
        <properties>
            <property name="spacingBeforeFirst" value="0" />
            <property name="spacingAfterLast" value="0" />
        </properties>
    </rule>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
foo.php:12:1: error - Expected 2 blank lines before function; 0 found

Expected behavior

With the code example CodeSniffer should not complain about missing blank lines.

Versions (please complete the following information)

Operating System [Linux Manjaro]
PHP version [8.3]
PHP_CodeSniffer version [3.12.0]
Standard [Squiz]
Install type [PHAR]

Additional context

This behaviour ist since version 3.12.0!
With version 3.11.3 this was not an issue.

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@jrfnl
Copy link
Member

jrfnl commented Apr 8, 2025

Thanks for reporting @ahtux ! That definitely is a regression. PR #946 should fix this. Testing appreciated.

@ahtux
Copy link
Author

ahtux commented Apr 8, 2025

First of all thank you very much for this quick fix!

I can confirm that it is solved as reported...
but...
I've modified the code a bit and believe that there are still two wrong cases:

First:

<?php
/**
 */


function foo()
{
}

Second:

<?php
/**
 */
function foo()
{
}

In both cases PHPCodeSniffer complains about missing blank lines...
In the first case version 3.11.3 was fine with it!
For the second case version 3.11.3 also complains missing blank lines!
Which is wrong from my point of view because it is the first function in the file... See the rules...

@jrfnl
Copy link
Member

jrfnl commented Apr 9, 2025

@ahtux I think these last two are based on a misunderstanding of the spacingBeforeFirst and spacingAfterLast properties.

As per the documentation (emphasis mine):

The spacingBeforeFirst property is used to determine how many blank lines are required before a function when it is the first block of code inside a statement group or control structure. This property is ignored when the function is outside one of these scopes, or if the function is preceded by member vars. If this property has not been set, the sniff will use whatever value has been set for the spacing property.

The spacingAfterLast property is used to determine how many blank lines are required after a function when it is the last block of code inside a statement group or control structure. This property is ignored when the function is outside one of these scopes, or if any member vars are placed after the function. If this property has not been set, the sniff will use whatever value has been set for the spacing property.

Ref: https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#squizwhitespacefunctionspacing

In other words, the sniff throwing errors in those last two cases looks correct to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants