Skip to content

Commit

Permalink
Merge pull request #760 from PHPCompatibility/feature/newtypecasts-ph…
Browse files Browse the repository at this point in the history
…pcs-3.4.0-compat

NewTypeCasts: updated for compatibility with PHPCS 3.4.0
  • Loading branch information
wimg committed Dec 9, 2018
2 parents fd0cb3e + eed9f61 commit 8ac4b12
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
12 changes: 9 additions & 3 deletions PHPCompatibility/Sniffs/TypeCasts/NewTypeCastsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHPCompatibility\Sniffs\TypeCasts;

use PHPCompatibility\AbstractNewFeatureSniff;
use PHPCompatibility\PHPCSHelper;

/**
* \PHPCompatibility\Sniffs\TypeCasts\NewTypeCastsSniff.
Expand Down Expand Up @@ -62,11 +63,14 @@ public function register()
*
* - (binary) cast is incorrectly tokenized as T_STRING_CAST by PHP and PHPCS.
* - b"something" binary cast is incorrectly tokenized as T_CONSTANT_ENCAPSED_STRING by PHP and PHPCS.
* - Since PHPCS 3.4.0, PHPCS *will* tokenize these correctly.
*
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1574
*/
$tokens[] = T_STRING_CAST;
$tokens[] = T_CONSTANT_ENCAPSED_STRING;
if (version_compare(PHPCSHelper::getVersion(), '3.4.0', '<') === true) {
$tokens[] = T_STRING_CAST;
$tokens[] = T_CONSTANT_ENCAPSED_STRING;
}

return $tokens;
}
Expand Down Expand Up @@ -99,7 +103,9 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
break;

case 'T_CONSTANT_ENCAPSED_STRING':
if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {
if ((strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"')
|| (strpos($tokenContent, "b'") === 0 && substr($tokenContent, -1) === "'")
) {
$tokenType = 'T_BINARY_CAST';
} else {
return;
Expand Down
3 changes: 2 additions & 1 deletion PHPCompatibility/Tests/TypeCasts/NewTypeCastsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
(unset) $a;
(binary) 1234;
$binary = b"binary string";
$binary = b"binary $string";
$also_binary = b'124';

// Verify space & case independency.
( unset ) $a;
Expand All @@ -16,5 +18,4 @@ $binary = b"binary string";
( BINARY ) 1234;

// Just making sure / no false positives.
$not_binary = b'124';
$ordinary = 'b"something"';
13 changes: 6 additions & 7 deletions PHPCompatibility/Tests/TypeCasts/NewTypeCastsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NewTypeCastsUnitTest extends BaseSniffTest
/**
* testNewTypeCasts
*
* @dataProvider dataNewFunction
* @dataProvider dataNewTypeCasts
*
* @param string $castDescription The type of type cast.
* @param string $lastVersionBefore The PHP version just *before* the type cast was introduced.
Expand Down Expand Up @@ -56,15 +56,15 @@ public function testNewTypeCasts($castDescription, $lastVersionBefore, $lines, $
/**
* Data provider.
*
* @see testNewFunction()
* @see testNewTypeCasts()
*
* @return array
*/
public function dataNewFunction()
public function dataNewTypeCasts()
{
return array(
array('The unset cast', '4.4', array(8, 13, 15), '5.0'),
array('The binary cast', '5.2.0', array(9, 10, 14, 16), '5.3', '5.2'), // Test (global) namespaced function.
array('The unset cast', '4.4', array(8, 15, 17), '5.0'),
array('The binary cast', '5.2.0', array(9, 10, 11, 12, 16, 18), '5.3', '5.2'), // Test (global) namespaced function.
);
}

Expand Down Expand Up @@ -96,8 +96,7 @@ public function dataNoFalsePositives()
return array(
array(4),
array(5),
array(19),
array(20),
array(21),
);
}

Expand Down

0 comments on commit 8ac4b12

Please sign in to comment.