Skip to content

Commit 72be213

Browse files
committed
Modernize: Generic/various sniffs: use class constant for constant array
1 parent 251385e commit 72be213

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ class ByteOrderMarkSniff implements Sniff
2121
*
2222
* Use encoding names as keys and hex BOM representations as values.
2323
*
24-
* @var array
24+
* @var array<string, string>
2525
*/
26-
protected $bomDefinitions = [
26+
protected const BOM_DEFINITIONS = [
2727
'UTF-8' => 'efbbbf',
2828
'UTF-16 (BE)' => 'feff',
2929
'UTF-16 (LE)' => 'fffe',
3030
];
3131

32+
/**
33+
* List of supported BOM definitions.
34+
*
35+
* @var array<string, string>
36+
*
37+
* @deprecated 4.0.0 Use the ByteOrderMarkSniff::BOM_DEFINITIONS constant instead.
38+
*/
39+
protected $bomDefinitions = self::BOM_DEFINITIONS;
40+
3241

3342
/**
3443
* Returns an array of tokens this test wants to listen for.
@@ -60,7 +69,7 @@ public function process(File $phpcsFile, $stackPtr)
6069

6170
$tokens = $phpcsFile->getTokens();
6271

63-
foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
72+
foreach (static::BOM_DEFINITIONS as $bomName => $expectedBomHex) {
6473
$bomByteLength = (strlen($expectedBomHex) / 2);
6574
$htmlBomHex = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength));
6675
if ($htmlBomHex === $expectedBomHex) {

src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ class InlineHTMLSniff implements Sniff
2020
*
2121
* Use encoding names as keys and hex BOM representations as values.
2222
*
23-
* @var array
23+
* @var array<string, string>
2424
*/
25-
protected $bomDefinitions = [
25+
protected const BOM_DEFINITIONS = [
2626
'UTF-8' => 'efbbbf',
2727
'UTF-16 (BE)' => 'feff',
2828
'UTF-16 (LE)' => 'fffe',
2929
];
3030

31+
/**
32+
* List of supported BOM definitions.
33+
*
34+
* @var array<string, string>
35+
*
36+
* @deprecated 4.0.0 Use the InlineHTMLSniff::BOM_DEFINITIONS constant instead.
37+
*/
38+
protected $bomDefinitions = self::BOM_DEFINITIONS;
39+
3140

3241
/**
3342
* Returns an array of tokens this test wants to listen for.
@@ -54,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
5463
{
5564
// Allow a byte-order mark.
5665
$tokens = $phpcsFile->getTokens();
57-
foreach ($this->bomDefinitions as $expectedBomHex) {
66+
foreach (static::BOM_DEFINITIONS as $expectedBomHex) {
5867
$bomByteLength = (strlen($expectedBomHex) / 2);
5968
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
6069
if ($htmlBomHex === $expectedBomHex && strlen($tokens[0]['content']) === $bomByteLength) {

src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ class CharacterBeforePHPOpeningTagSniff implements Sniff
2020
*
2121
* Use encoding names as keys and hex BOM representations as values.
2222
*
23-
* @var array
23+
* @var array<string, string>
2424
*/
25-
protected $bomDefinitions = [
25+
protected const BOM_DEFINITIONS = [
2626
'UTF-8' => 'efbbbf',
2727
'UTF-16 (BE)' => 'feff',
2828
'UTF-16 (LE)' => 'fffe',
2929
];
3030

31+
/**
32+
* List of supported BOM definitions.
33+
*
34+
* @var array<string, string>
35+
*
36+
* @deprecated 4.0.0 Use the CharacterBeforePHPOpeningTagSniff::BOM_DEFINITIONS constant instead.
37+
*/
38+
protected $bomDefinitions = self::BOM_DEFINITIONS;
39+
3140

3241
/**
3342
* Returns an array of tokens this test wants to listen for.
@@ -56,7 +65,7 @@ public function process(File $phpcsFile, $stackPtr)
5665
if ($stackPtr > 0) {
5766
// Allow a byte-order mark.
5867
$tokens = $phpcsFile->getTokens();
59-
foreach ($this->bomDefinitions as $expectedBomHex) {
68+
foreach (static::BOM_DEFINITIONS as $expectedBomHex) {
6069
$bomByteLength = (strlen($expectedBomHex) / 2);
6170
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
6271
if ($htmlBomHex === $expectedBomHex) {

0 commit comments

Comments
 (0)