Skip to content

Commit

Permalink
codesniffer: Enable MediaWiki.AlternativeSyntax.UnicodeEscape sniff (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
anomiex authored Nov 20, 2023
1 parent b22a850 commit 9a9987b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion projects/packages/codesniffer/Jetpack/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<!-- We only include some rules from mediawiki/mediawiki-codesniffer. -->
<rule ref="MediaWiki.AlternativeSyntax.LeadingZeroInFloat" />
<rule ref="MediaWiki.AlternativeSyntax.UnicodeEscape" />
<rule ref="MediaWiki.Classes.UnsortedUseStatements" />
<rule ref="MediaWiki.Classes.UnusedUseStatement" />
<rule ref="MediaWiki.ExtraCharacters.ParenthesesAroundKeyword" />
Expand All @@ -25,7 +26,6 @@
<rule ref="MediaWiki.WhiteSpace.MultipleEmptyLines" />
<rule ref="MediaWiki.Usage.IsNull" />
<!-- Maybe in the future
<rule ref="MediaWiki.AlternativeSyntax.UnicodeEscape" /> // syntax it checks requires PHP 7.0+
<rule ref="MediaWiki.Usage.NullableType" /> // syntax it enforces requires PHP 7.1+
-->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Enable `MediaWiki.AlternativeSyntax.UnicodeEscape` sniff.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 9a9987b

Please sign in to comment.