Skip to content

Commit

Permalink
Merge 8f6c2f8 into 567d785
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 10, 2024
2 parents 567d785 + 8f6c2f8 commit 8c548e4
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 0 deletions.
92 changes: 92 additions & 0 deletions PHPCSUtils/Utils/FileInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Utils;

use PHP_CodeSniffer\Files\File;

/**
* Utility functions to retrieve information about the file under scan.
*
* @since 1.1.0
*/
final class FileInfo
{

/**
* List of supported BOM definitions.
*
* Use encoding names as keys and hex BOM representations as values.
*
* @since 1.1.0
*
* @var array<string, string>
*/
private static $bomDefinitions = [
'UTF-8' => 'efbbbf',
'UTF-16 (BE)' => 'feff',
'UTF-16 (LE)' => 'fffe',
];

/**
* Determine whether the file under scan has a byte-order mark at the start.
*
* Inspired by similar code being used in a couple of PHPCS native sniffs.
*
* @since 1.1.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
*
* @return string|false Name of the type of BOM found or FALSE when the file does not start with a BOM.
*/
public static function hasByteOrderMark(File $phpcsFile)
{
$tokens = $phpcsFile->getTokens();

if ($tokens[0]['code'] !== \T_INLINE_HTML) {
return false;
}

foreach (self::$bomDefinitions as $bomName => $expectedBomHex) {
$bomByteLength = (int) (\strlen($expectedBomHex) / 2);
$htmlBomHex = \bin2hex(\substr($tokens[0]['content'], 0, $bomByteLength));
if ($htmlBomHex === $expectedBomHex) {
return $bomName;
}
}

return false;
}

/**
* Determine whether the file under scan has a shebang line at the start.
*
* @since 1.1.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
*
* @return bool
*/
public static function hasSheBang(File $phpcsFile)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[0]['code'] !== \T_INLINE_HTML) {
return false;
}

$start = 0;
$hasByteOrderMark = self::hasByteOrderMark($phpcsFile);
if ($hasByteOrderMark !== false) {
$start = (int) (\strlen(self::$bomDefinitions[$hasByteOrderMark]) / 2);
}

return (\substr($tokens[0]['content'], $start, 2) === '#!');
}
}
3 changes: 3 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkAnsiTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'This file does not have a byte order mark and uses ANSI encoding';
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkAnsiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasByteOrderMark() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasByteOrderMark
*
* @since 1.1.0
*/
final class HasByteOrderMarkAnsiTest extends UtilityMethodTestCase
{

/**
* Test whether a file without byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasByteOrderMark()
{
$this->assertFalse(FileInfo::hasByteOrderMark(self::$phpcsFile));
}
}
Binary file added Tests/Utils/FileInfo/HasByteOrderMarkUTF16BETest.inc
Binary file not shown.
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF16BETest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasByteOrderMark() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasByteOrderMark
*
* @since 1.1.0
*/
final class HasByteOrderMarkUTF16BETest extends UtilityMethodTestCase
{

/**
* Test whether a byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasByteOrderMark()
{
$this->assertSame('UTF-16 (BE)', FileInfo::hasByteOrderMark(self::$phpcsFile));
}
}
Binary file added Tests/Utils/FileInfo/HasByteOrderMarkUTF16LETest.inc
Binary file not shown.
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF16LETest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasByteOrderMark() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasByteOrderMark
*
* @since 1.1.0
*/
final class HasByteOrderMarkUTF16LETest extends UtilityMethodTestCase
{

/**
* Test whether a byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasByteOrderMark()
{
$this->assertSame('UTF-16 (LE)', FileInfo::hasByteOrderMark(self::$phpcsFile));
}
}
3 changes: 3 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF8NoBomTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'This file does not have a byte order mark and uses UTF-8 encoding';
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF8NoBomTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasByteOrderMark() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasByteOrderMark
*
* @since 1.1.0
*/
final class HasByteOrderMarkUTF8NoBomTest extends UtilityMethodTestCase
{

/**
* Test whether a file without byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasByteOrderMark()
{
$this->assertFalse(FileInfo::hasByteOrderMark(self::$phpcsFile));
}
}
3 changes: 3 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF8WithBomTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'This file does has a UTF-8 byte order mark';
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasByteOrderMarkUTF8WithBomTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasByteOrderMark() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasByteOrderMark
*
* @since 1.1.0
*/
final class HasByteOrderMarkUTF8WithBomTest extends UtilityMethodTestCase
{

/**
* Test whether a byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasByteOrderMark()
{
$this->assertSame('UTF-8', FileInfo::hasByteOrderMark(self::$phpcsFile));
}
}
4 changes: 4 additions & 0 deletions Tests/Utils/FileInfo/HasSheBangEnvPHPTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php

echo 'This file has a PHP shebang line';
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasSheBangEnvPHPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasSheBang() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasSheBang
*
* @since 1.1.0
*/
final class HasSheBangEnvPHPTest extends UtilityMethodTestCase
{

/**
* Test whether a byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasSheBang()
{
$this->assertTrue(FileInfo::hasSheBang(self::$phpcsFile));
}
}
3 changes: 3 additions & 0 deletions Tests/Utils/FileInfo/HasSheBangNoneTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'This file does not have a PHP shebang line';
35 changes: 35 additions & 0 deletions Tests/Utils/FileInfo/HasSheBangNoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2024 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Utils\FileInfo;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\FileInfo;

/**
* Tests for the \PHPCSUtils\Utils\FileInfo::hasSheBang() method.
*
* @covers \PHPCSUtils\Utils\FileInfo::hasSheBang
*
* @since 1.1.0
*/
final class HasSheBangNoneTest extends UtilityMethodTestCase
{

/**
* Test whether a byte order mark at the start of the file is correctly recognized.
*
* @return void
*/
public function testHasSheBang()
{
$this->assertFalse(FileInfo::hasSheBang(self::$phpcsFile));
}
}
4 changes: 4 additions & 0 deletions Tests/Utils/FileInfo/HasSheBangPHPAndUTF8BomTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/php
<?php

echo 'This file has a PHP shebang line and is encoded in UTF-8 with a Byte order mark';

0 comments on commit 8c548e4

Please sign in to comment.