From 9a9987bf5b3b6b316d3223fbf36a358459366f39 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 20 Nov 2023 04:46:06 -0500 Subject: [PATCH] codesniffer: Enable `MediaWiki.AlternativeSyntax.UnicodeEscape` sniff (#34194) Now that we're on PHP 7.0+ we can use Unicode escapes in strings. This sniff will check for a standard format for such escapes, four to six uppercase hexits with no unnecessary leading zeros. So like `\u{00A0}` and `\u{1F389}` rather than `\u{a0}` or `\u{01f389}`. Bonus, since we haven't been able to use these until now, there are none to fix. --- .../packages/codesniffer/Jetpack/ruleset.xml | 2 +- ...Wiki.AlternativeSyntax.UnicodeEscape-sniff | 4 ++ .../tests/files/mediawiki-imports.php.fixed | 3 ++ .../tests/files/mediawiki-imports.php.report | 37 ++++++++++--------- .../tests/files/mediawiki-imports.php.tolint | 3 ++ 5 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 projects/packages/codesniffer/changelog/add-codesniffer-MediaWiki.AlternativeSyntax.UnicodeEscape-sniff diff --git a/projects/packages/codesniffer/Jetpack/ruleset.xml b/projects/packages/codesniffer/Jetpack/ruleset.xml index bcd23f8e8d90e..a8e084fbe01e0 100644 --- a/projects/packages/codesniffer/Jetpack/ruleset.xml +++ b/projects/packages/codesniffer/Jetpack/ruleset.xml @@ -8,6 +8,7 @@ + @@ -25,7 +26,6 @@ diff --git a/projects/packages/codesniffer/changelog/add-codesniffer-MediaWiki.AlternativeSyntax.UnicodeEscape-sniff b/projects/packages/codesniffer/changelog/add-codesniffer-MediaWiki.AlternativeSyntax.UnicodeEscape-sniff new file mode 100644 index 0000000000000..93b086c91b450 --- /dev/null +++ b/projects/packages/codesniffer/changelog/add-codesniffer-MediaWiki.AlternativeSyntax.UnicodeEscape-sniff @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Enable `MediaWiki.AlternativeSyntax.UnicodeEscape` sniff. diff --git a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.fixed b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.fixed index 01d5494bf25ca..a2229e80ccfc0 100644 --- a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.fixed +++ b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.fixed @@ -34,6 +34,9 @@ class Dummy_Class { // MediaWiki.AlternativeSyntax.LeadingZeroInFloat. $u->dummy = 0.5; + // MediaWiki.AlternativeSyntax.UnicodeEscape + $u->unicode = "\u{00A0}\u{00A0}\u{1F389}\u{1F389}"; + // MediaWiki.ExtraCharacters.ParenthesesAroundKeyword. require_once 'bar.php'; $u->clone = clone $x; diff --git a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.report b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.report index f4b9aefc7f08b..31c7181322b88 100644 --- a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.report +++ b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.report @@ -3,21 +3,22 @@ 16 | WARNING | [x] Unnecessary use statement "Unnecessary" in the same namespace (MediaWiki.Classes.UnusedUseStatement.UnnecessaryUse) 17 | WARNING | [x] Unused use statement "Unused" (MediaWiki.Classes.UnusedUseStatement.UnusedUse) 38 | WARNING | [x] Floats should have a leading 0 (MediaWiki.AlternativeSyntax.LeadingZeroInFloat.Found) - 41 | WARNING | [x] require_once keyword must not be used as a function. (MediaWiki.ExtraCharacters.ParenthesesAroundKeyword.ParenthesesAroundKeywords) - 41 | ERROR | [ ] Absolute include path must be used. Use `get_template_directory()`, `get_stylesheet_directory()` or `plugin_dir_path()`. - | | (WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath) - 42 | WARNING | [x] clone keyword must not be used as a function. (MediaWiki.ExtraCharacters.ParenthesesAroundKeyword.ParenthesesAroundKeywords) - 45 | WARNING | [x] Use (bool) instead of !! (MediaWiki.Usage.DoubleNotOperator.DoubleNotOperator) - 48 | WARNING | [x] Use rtrim() instead of chop (MediaWiki.Usage.ForbiddenFunctions.chop) - 49 | WARNING | [x] Use mb_strlen() instead of mbstrlen (MediaWiki.Usage.ForbiddenFunctions.mbstrlen) - 54 | ERROR | [ ] Found slow in_array( …, array_keys() ), should be array_key_exists() or isset() (MediaWiki.Usage.InArrayUsage.Found) - 55 | ERROR | [ ] Found slow in_array( …, array_flip() ), should be array_key_exists() or isset() (MediaWiki.Usage.InArrayUsage.Found) - 62 | WARNING | [ ] Avoid use of __METHOD__ magic constant in closure (MediaWiki.Usage.MagicConstantClosure.FoundConstantMethod) - 62 | WARNING | [ ] Avoid use of __FUNCTION__ magic constant in closure (MediaWiki.Usage.MagicConstantClosure.FoundConstantFunction) - 68 | ERROR | [ ] Function nested is nested inside of another function or closure (MediaWiki.Usage.NestedFunctions.NestedFunction) - 72 | ERROR | [ ] Use "." for string concat (MediaWiki.Usage.PlusStringConcat.Found) - 73 | ERROR | [ ] Use ".=" for string concat (MediaWiki.Usage.PlusStringConcat.Found) - 76 | ERROR | [ ] The ampersand in "&$this" must be removed. If you plan to get back another instance of this class, assign $this to a temporary variable. (MediaWiki.Usage.ReferenceThis.Found) - 79 | ERROR | [x] Multiple empty lines should not exist in a row; found 2 consecutive empty lines (MediaWiki.WhiteSpace.MultipleEmptyLines.MultipleEmptyLines) - 82 | ERROR | [x] Expected 1 space after FUNCTION keyword; 0 found (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction) - 89 | ERROR | [x] Multiple empty lines should not exist in a row; found 2 consecutive empty lines (MediaWiki.WhiteSpace.MultipleEmptyLines.MultipleEmptyLines) + 41 | WARNING | [x] Unicode code points should be expressed using four to six uppercase hex digits, with leading zeros used only as necessary for \u{0FFF} and below + | | (MediaWiki.AlternativeSyntax.UnicodeEscape.DigitsNotNormalized) + 44 | WARNING | [x] require_once keyword must not be used as a function. (MediaWiki.ExtraCharacters.ParenthesesAroundKeyword.ParenthesesAroundKeywords) + 44 | ERROR | [ ] Absolute include path must be used. Use `get_template_directory()`, `get_stylesheet_directory()` or `plugin_dir_path()`. (WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath) + 45 | WARNING | [x] clone keyword must not be used as a function. (MediaWiki.ExtraCharacters.ParenthesesAroundKeyword.ParenthesesAroundKeywords) + 48 | WARNING | [x] Use (bool) instead of !! (MediaWiki.Usage.DoubleNotOperator.DoubleNotOperator) + 51 | WARNING | [x] Use rtrim() instead of chop (MediaWiki.Usage.ForbiddenFunctions.chop) + 52 | WARNING | [x] Use mb_strlen() instead of mbstrlen (MediaWiki.Usage.ForbiddenFunctions.mbstrlen) + 57 | ERROR | [ ] Found slow in_array( …, array_keys() ), should be array_key_exists() or isset() (MediaWiki.Usage.InArrayUsage.Found) + 58 | ERROR | [ ] Found slow in_array( …, array_flip() ), should be array_key_exists() or isset() (MediaWiki.Usage.InArrayUsage.Found) + 65 | WARNING | [ ] Avoid use of __METHOD__ magic constant in closure (MediaWiki.Usage.MagicConstantClosure.FoundConstantMethod) + 65 | WARNING | [ ] Avoid use of __FUNCTION__ magic constant in closure (MediaWiki.Usage.MagicConstantClosure.FoundConstantFunction) + 71 | ERROR | [ ] Function nested is nested inside of another function or closure (MediaWiki.Usage.NestedFunctions.NestedFunction) + 75 | ERROR | [ ] Use "." for string concat (MediaWiki.Usage.PlusStringConcat.Found) + 76 | ERROR | [ ] Use ".=" for string concat (MediaWiki.Usage.PlusStringConcat.Found) + 79 | ERROR | [ ] The ampersand in "&$this" must be removed. If you plan to get back another instance of this class, assign $this to a temporary variable. (MediaWiki.Usage.ReferenceThis.Found) + 82 | ERROR | [x] Multiple empty lines should not exist in a row; found 2 consecutive empty lines (MediaWiki.WhiteSpace.MultipleEmptyLines.MultipleEmptyLines) + 85 | ERROR | [x] Expected 1 space after FUNCTION keyword; 0 found (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction) + 92 | ERROR | [x] Multiple empty lines should not exist in a row; found 2 consecutive empty lines (MediaWiki.WhiteSpace.MultipleEmptyLines.MultipleEmptyLines) diff --git a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.tolint b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.tolint index 332fa763391e4..fcece79d431a9 100644 --- a/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.tolint +++ b/projects/packages/codesniffer/tests/php/tests/files/mediawiki-imports.php.tolint @@ -37,6 +37,9 @@ class Dummy_Class { // MediaWiki.AlternativeSyntax.LeadingZeroInFloat. $u->dummy = .5; + // MediaWiki.AlternativeSyntax.UnicodeEscape + $u->unicode = "\u{a0}\u{000a0}\u{1f389}\u{01F389}"; + // MediaWiki.ExtraCharacters.ParenthesesAroundKeyword. require_once( 'bar.php' ); $u->clone = clone( $x );