Skip to content
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

phpcbf crashes where there are 2 blank lines after the last use statement #212

Closed
1 of 7 tasks
xalopp opened this issue Dec 14, 2024 · 1 comment
Closed
1 of 7 tasks
Assignees
Labels

Comments

@xalopp
Copy link
Contributor

xalopp commented Dec 14, 2024

Type of issue

  • Bug in MO4 sniffer rule.
  • Bug in MO4 auto-fixer.
  • Bug in MO4 documentation.
  • Request to extend MO4 sniffer with specific rule.
  • Request to extend MO4 auto-fixer for specific rule.
  • Request to extend MO4 documentation.
  • Other (explain):

Description

The file integrationtests/B.php contains following code

<?php

namespace T;

use A;


class B
{
  /**
   * B's constructor
   */
    public function __construct()
    {
        $a = new A();
        $a->b();
    }
}

The style violation consists of 2 blank lines between the last use statement and the class declaration.

$ vendor/bin/phpcs --standard=MO4 -s integrationtests/B.php

FILE: /home/x/mo4-coding-standard/integrationtests/B.php
-----------------------------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------
 5 | ERROR | [x] Expected 1 line after last use statement, found 2.
   |       |     (SlevomatCodingStandard.Namespaces.UseSpacing.IncorrectLinesCountAfterLastUse)
 5 | ERROR | [x] There must be one blank line after the last USE statement; 2 found;
   |       |     (MO4.Formatting.AlphabeticalUseStatements.SpaceAfterLastUse)
 5 | ERROR | [x] Header blocks must be separated by a single blank line (PSR12.Files.FileHeader.SpacingAfterBlock)
 6 | ERROR | [x] Multiple empty lines should not exist in a row; found 2 consecutive empty lines
   |       |     (MO4.WhiteSpace.MultipleEmptyLines.MultipleEmptyLines)
-----------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------------

Time: 39ms; Memory: 10MB

We see, that more than on sniff are complaining.

Using the autofixer fails, with an exception in a non relevant sniff MO4.Formatting.UnnecessaryNamespaceUsage

$ vendor/bin/phpcbf --standard=MO4 -s integrationtests/B.php
PHP Fatal error:  Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Uninitialized string offset 0 in /home/x/mo4-coding-standard/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php on line 313 in /home/x/Projekte/PHP/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Runner.php:624
Stack trace:
#0 /home/x/mo4-coding-standard/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php(313): PHP_CodeSniffer\Runner->handleErrors()
#1 /home/x/mo4-coding-standard/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php(258): MO4\Sniffs\Formatting\UnnecessaryNamespaceUsageSniff->getFullyQualifiedClassName()
#2 /home/x/mo4-coding-standard/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php(92): MO4\Sniffs\Formatting\UnnecessaryNamespaceUsageSniff->getUseStatements()
#3 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Files/File.php(519): MO4\Sniffs\Formatting\UnnecessaryNamespaceUsageSniff->process()
#4 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
#5 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Fixer.php(175): PHP_CodeSniffer\Files\LocalFile->process()
#6 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php(53): PHP_CodeSniffer\Fixer->fixFile()
#7 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Reporter.php(285): PHP_CodeSniffer\Reports\Cbf->generateFileReport()
#8 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Runner.php(714): PHP_CodeSniffer\Reporter->cacheFileReport()
#9 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Runner.php(454): PHP_CodeSniffer\Runner->processFile()
#10 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Runner.php(216): PHP_CodeSniffer\Runner->run()
#11 /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/bin/phpcbf(14): PHP_CodeSniffer\Runner->runPHPCBF()
#12 /home/x/mo4-coding-standard/vendor/bin/phpcbf(119): include('...')
#13 {main}
  thrown in /home/x/mo4-coding-standard/vendor/squizlabs/php_codesniffer/src/Runner.php on line 624
$ vendor/bin/phpcbf --standard=MO4 -s integrationtests/B.php --exclude=MO4.Formatting.UnnecessaryNamespaceUsage

works.

Technical details

  • PHP version: PHP 8.3.6 (cli) (built: Sep 30 2024 15:17:17) (NTS)
  • PHPCS version: squizlabs/php_codesniffer 3.11.2
  • MO4 version: Relse 10.0.0 commit a7b983b
  • OS: Ubuntu 24.04.1 LTS
@xalopp xalopp self-assigned this Dec 14, 2024
@xalopp xalopp added the bug label Dec 14, 2024
@xalopp xalopp changed the title phpcbf crashes where there are 2 blank lines after the last use statement phcbf crashes where there are 2 blank lines after the last use statement Dec 14, 2024
@xalopp xalopp changed the title phcbf crashes where there are 2 blank lines after the last use statement phpcbf crashes where there are 2 blank lines after the last use statement Dec 14, 2024
xalopp added a commit to xalopp/mo4-coding-standard that referenced this issue Dec 14, 2024
xalopp added a commit to xalopp/mo4-coding-standard that referenced this issue Dec 14, 2024
xalopp added a commit to xalopp/mo4-coding-standard that referenced this issue Dec 15, 2024
xalopp added a commit to xalopp/mo4-coding-standard that referenced this issue Dec 15, 2024
@xalopp
Copy link
Contributor Author

xalopp commented Dec 15, 2024

I was able to identify the root cause of the issue.

When the use and class are on the same line, then the sniff triggers an error:

<?php

namespace T;

use A;class B
{
  /**
   * B's constructor
   */
    public function __construct()
    {
        $a = new A();
        $a->b();
    }
}
$ vendor/bin/phpcs -s --standard=MO4 integrationtests/C.php

FILE: /home/x/Projekte/PHP/mo4-coding-standard/integrationtests/C.php
-----------------------------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | [ ] An error occurred during processing; checking has been aborted. The error message was: Uninitialized string offset 0 in
   |       |     /home/x/mo4-coding-standard/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php on line 313
   |       |     The error originated in the MO4.Formatting.UnnecessaryNamespaceUsage sniff on line 313. (Internal.Exception)
 5 | ERROR | [x] Expected 1 line after last use statement, found -1.
   |       |     (SlevomatCodingStandard.Namespaces.UseSpacing.IncorrectLinesCountAfterLastUse)
 5 | ERROR | [x] There must be one blank line after the last USE statement; 0 found;
   |       |     (MO4.Formatting.AlphabeticalUseStatements.SpaceAfterLastUse)
 5 | ERROR | [x] Header blocks must be separated by a single blank line (PSR12.Files.FileHeader.SpacingAfterBlock)
-----------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------------

PR #213 will solve this

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

No branches or pull requests

1 participant