From 1cfba97b86f29be4782b0b88729f520d83921c5b Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 3 Feb 2020 07:55:12 +1100 Subject: [PATCH] Fixed bug #2848 : PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header --- package.xml | 60 ++----------------- src/Config.php | 2 +- .../PSR12/Sniffs/Files/FileHeaderSniff.php | 18 +++++- .../Tests/Files/FileHeaderUnitTest.14.inc | 7 +++ .../Tests/Files/FileHeaderUnitTest.15.inc | 5 ++ 5 files changed, 34 insertions(+), 58 deletions(-) create mode 100644 src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.14.inc create mode 100644 src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.15.inc diff --git a/package.xml b/package.xml index 8f614ff5a5..a5a7c527c1 100644 --- a/package.xml +++ b/package.xml @@ -17,8 +17,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> 2020-01-31 - 3.5.4 - 3.5.4 + 3.5.5 + 3.5.5 stable @@ -26,59 +26,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> BSD 3-Clause License - - The PHP 7.4 numeric separator backfill now works correctly for more float formats - -- Thanks to Juliette Reinders Folmer for the patch - - The PHP 7.4 numeric separator backfill is no longer run on PHP version 7.4.0 or greater - - File::getCondition() now accepts a 3rd argument that allows for the closest matching token to be returned - -- By default, it continues to return the first matched token found from the top of the file - - Fixed detection of array return types for arrow functions - - Added Generic.PHP.DisallowRequestSuperglobal to ban the use of the $_REQUEST superglobal - -- Thanks to Morerice for the contribution - - Generic.ControlStructures.InlineControlStructure no longer shows errors for WHILE and FOR statements without a body - -- Previously it required these to have curly braces, but there were no statements to enclose in them - -- Thanks to Juliette Reinders Folmer for the patch - - PSR12.ControlStructures.BooleanOperatorPlacement can now be configured to enforce a specific operator position - -- By default, the sniff ensures that operators are all at the begining or end of lines, but not a mix of both - -- Set the allowOnly property to "first" to enforce all boolean operators to be at the start of a line - -- Set the allowOnly property to "last" to enforce all boolean operators to be at the end of a line - -- Thanks to Vincent Langlet for the patch - - PSR12.Files.ImportStatement now auto-fixes import statements by removing the leading slash - -- Thanks to Michał Bundyra for the patch - - Squiz.ControlStructures.ForLoopDeclaration now has a setting to ignore newline characters - -- Default remains FALSE, so newlines are not allowed within FOR definitions - -- Override the "ignoreNewlines" setting in a ruleset.xml file to change - - Squiz.PHP.InnerFunctions now handles multiple nested anon classes correctly - - Fixed bug #2497 : Sniff properties not set when referencing a sniff using relative paths or non-native slashes - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2657 : Squiz.WhiteSpace.FunctionSpacing can remove spaces between comment and first/last method during auto-fixing - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2688 : Case statements not tokenized correctly when switch is contained within ternary - - Fixed bug #2698 : PHPCS throws errors determining auto report width when shell_exec is disabled - -- Thanks to Matthew Peveler for the patch - - Fixed bug #2730 : PSR12.ControlStructures.ControlStructureSpacing does not ignore comments between conditions - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2732 : PSR12.Files.FileHeader misidentifies file header in mixed content file - - Fixed bug #2745 : AbstractArraySniff wrong indices when mixed coalesce and ternary values - -- Thanks to Michał Bundyra for the patch - - Fixed bug #2748 : Wrong end of statement for fn closures - -- Thanks to Michał Bundyra for the patch - - Fixed bug #2751 : Autoload relative paths first to avoid confusion with files from the global include path - -- Thanks to Klaus Purer for the patch - - Fixed bug #2763 : PSR12 standard reports errors for multi-line FOR definitions - - Fixed bug #2768 : Generic.Files.LineLength false positive for non-breakable strings at exactly the soft limit - -- Thanks to Alex Miles for the patch - - Fixed bug #2773 : PSR2.Methods.FunctionCallSignature false positive when arrow function has array return type - - Fixed bug #2790 : PSR12.Traits.UseDeclaration ignores block comments - -- Thanks to Vincent Langlet for the patch - - Fixed bug #2791 : PSR12.Functions.NullableTypeDeclaration false positive when ternary operator used with instanceof - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2802 : Can't specify a report file path using the tilde shortcut - - Fixed bug #2804 : PHP4-style typed properties not tokenized correctly - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2805 : Undefined Offset notice during live coding of arrow functions - -- Thanks to Juliette Reinders Folmer for the patch - - Fixed bug #2843 : Tokenizer does not support alternative syntax for declare statements - -- Thanks to Juliette Reinders Folmer for the patch + - Fixed bug #2848 : PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header @@ -1145,6 +1093,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + diff --git a/src/Config.php b/src/Config.php index b88e1435f0..b48c111db0 100644 --- a/src/Config.php +++ b/src/Config.php @@ -23,7 +23,7 @@ class Config * * @var string */ - const VERSION = '3.5.4'; + const VERSION = '3.5.5'; /** * Package stability; either stable, beta or alpha. diff --git a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php index 1ea1dedc93..4163832fe9 100644 --- a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php @@ -71,8 +71,22 @@ public function process(File $phpcsFile, $stackPtr) } while ($openTag !== false); if ($openTag === false) { - // We never found a proper file header - // so use the first one as the header. + // We never found a proper file header. + // If the file has multiple PHP open tags, we know + // that it must be a mix of PHP and HTML (or similar) + // so the header rules do not apply. + if (count($possibleHeaders) > 1) { + return $phpcsFile->numTokens; + } + + // There is only one possible header. + // If it is the first content in the file, it technically + // serves as the file header, and the open tag needs to + // have a newline after it. Otherwise, ignore it. + if ($stackPtr > 0) { + return $phpcsFile->numTokens; + } + $openTag = $stackPtr; } else if (count($possibleHeaders) > 1) { // There are other PHP blocks before the file header. diff --git a/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.14.inc b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.14.inc new file mode 100644 index 0000000000..71ebdd8cf8 --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.14.inc @@ -0,0 +1,7 @@ + +
  • My page
  • +
  • Write
  • +
  • Sign out
  • + +
  • Sign in
  • + diff --git a/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.15.inc b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.15.inc new file mode 100644 index 0000000000..11102beb65 --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.15.inc @@ -0,0 +1,5 @@ + + +