Skip to content

Commit d71128c

Browse files
authored
Merge pull request #735 from PHPCSStandards/develop
Release 1.2.1
2 parents fa82d14 + 65dfeb3 commit d71128c

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ This projects adheres to [Keep a CHANGELOG](https://keepachangelog.com/) and use
1010
_Nothing yet._
1111

1212

13+
## [1.2.1] - 2025-11-17
14+
15+
### Fixed
16+
17+
#### Abstract Sniffs
18+
19+
* The `AbstractArrayDeclarationSniff::getActualArrayKey()` method could cause deprecation notices and even fatal errors, when a PHPCS scan would be run on a different PHP version than the "code under scan" is targetting and the "code under scan" contained deprecated/removed type casts. [#733]
20+
21+
[#733]: https://github.com/PHPCSStandards/PHPCSUtils/pull/733
22+
23+
1324
## [1.2.0] - 2025-11-11
1425

1526
### Added
@@ -30,7 +41,7 @@ _Nothing yet._
3041

3142
#### Other
3243

33-
* Dropped support for [PHP_CodeSniffer] < 3.13.5/<4.00. [#729]
44+
* Dropped support for [PHP_CodeSniffer] < 3.13.5/4.0.1. [#729]
3445
Please ensure you run `composer update phpcsstandards/phpcsutils --with-dependencies` to benefit from this.
3546
* Various housekeeping.
3647

@@ -1322,6 +1333,7 @@ This initial alpha release contains the following utility classes:
13221333

13231334

13241335
[Unreleased]: https://github.com/PHPCSStandards/PHPCSUtils/compare/stable...HEAD
1336+
[1.2.1]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.2.0...1.2.1
13251337
[1.2.0]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.1.3...1.2.0
13261338
[1.1.3]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.1.2...1.1.3
13271339
[1.1.2]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.1.1...1.1.2

PHPCSUtils/AbstractSniffs/AbstractArrayDeclarationSniff.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ public function getActualArrayKey(File $phpcsFile, $startPtr, $endPtr)
493493
}
494494
}
495495

496-
if (isset($this->acceptedTokens[$this->tokens[$i]['code']]) === false) {
496+
if (isset($this->acceptedTokens[$this->tokens[$i]['code']]) === false
497+
|| \T_UNSET_CAST === $this->tokens[$i]['code']
498+
) {
497499
// This is not a key we can evaluate. Might be a variable or constant.
498500
return;
499501
}
@@ -506,6 +508,40 @@ public function getActualArrayKey(File $phpcsFile, $startPtr, $endPtr)
506508
continue;
507509
}
508510

511+
/*
512+
* Make sure that when new/deprecated/removed casts are used in the code under scan and the sniff is run
513+
* on a PHP version which doesn't support the cast, the eval() won't cause a deprecation notice,
514+
* borking the scan of the file.
515+
*
516+
* - (unset) was deprecated in PHP 7.2 and removed in PHP 8.0;
517+
* - (real) was deprecated in PHP 7.4 and removed in PHP 8.0;
518+
* - (boolean) was deprecated in PHP 8.5 and will be removed in PHP 9.0;
519+
* - (integer) was deprecated in PHP 8.5 and will be removed in PHP 9.0;
520+
* - (double) was deprecated in PHP 8.5 and will be removed in PHP 9.0;
521+
* - (string) was deprecated in PHP 8.5 and will be removed in PHP 9.0;
522+
*/
523+
if (\T_DOUBLE_CAST === $this->tokens[$i]['code']) {
524+
$content .= '(float)';
525+
continue;
526+
}
527+
528+
if (\PHP_VERSION_ID >= 80500) {
529+
if (\T_INT_CAST === $this->tokens[$i]['code']) {
530+
$content .= '(int)';
531+
continue;
532+
}
533+
534+
if (\T_BOOL_CAST === $this->tokens[$i]['code']) {
535+
$content .= '(bool)';
536+
continue;
537+
}
538+
539+
if (\T_BINARY_CAST === $this->tokens[$i]['code']) {
540+
$content .= '(string)';
541+
continue;
542+
}
543+
}
544+
509545
// Account for heredoc with vars.
510546
if ($this->tokens[$i]['code'] === \T_START_HEREDOC) {
511547
$text = TextStrings::getCompleteTextString($phpcsFile, $i);

Tests/AbstractSniffs/AbstractArrayDeclaration/GetActualArrayKeyTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ EOD
2626
$var text
2727
EOD
2828
=> 'excluded',
29+
(unset) false => 'excluded', // Type cast deprecated per PHP 7.2, removed 8.0.
2930
];
3031

3132
/* testAllEmptyString */
@@ -34,6 +35,7 @@ $emptyStringKey = array(
3435
null => 'null',
3536
(string) false => 'false',
3637
\null => 'null',
38+
(binary) false => 'binary false', // Type cast deprecated per PHP 8.5.
3739
);
3840

3941
/* testAllZero */
@@ -61,6 +63,10 @@ $everythingZero = [
6163
(true) ? 0 : 1 => 't',
6264
! true => 'u',
6365
\false => 'v',
66+
(integer) '' => 'w', // Type cast deprecated per PHP 8.5.
67+
(float) 0.14 => 'x',
68+
(real) 0.2 => 'y', // Type cast deprecated per PHP 7.4, removed 8.0.
69+
(double) 0.005 => 'z', // Type cast deprecated per PHP 8.5.
6470
];
6571

6672
/* testAllOne */
@@ -89,6 +95,11 @@ $everythingOne = [
8995
! false => 't',
9096
(string) true => 'u',
9197
\true => 'v',
98+
(boolean) 1 => 'w', // Type cast deprecated per PHP 8.5.
99+
(binary) true => 'x',
100+
(float) 1.14 => 'y',
101+
(real) 1.2 => 'z', // Type cast deprecated per PHP 7.4, removed 8.0.
102+
(double) 1.005 => 'aa', // Type cast deprecated per PHP 8.5.
92103
];
93104

94105
/* testAllEleven */
@@ -110,6 +121,7 @@ $everythingEleven = [
110121
"11" => 'o',
111122
11. => 'p',
112123
35 % 12 => 'q',
124+
(integer) '11' => 'r', // Type cast deprecated per PHP 8.5.
113125
];
114126

115127
/* testAllStringAbc */

0 commit comments

Comments
 (0)