Skip to content

Commit ba259ea

Browse files
authored
Merge pull request #479 from PHPCSStandards/develop
Release 1.0.6
2 parents 0cfef51 + 864d5c2 commit ba259ea

30 files changed

+485
-183
lines changed

.github/SECURITY.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
The latest patch version of the `1.x` release series is supported for security updates.
6+
7+
## Reporting a Vulnerability
8+
9+
PHPCSUtils is a developer tool and should generally not be used in a production (web accessible) environment.
10+
11+
Having said that, responsible disclosure of security issues is highly appreciated.
12+
13+
**Please do not report or discuss security vulnerabilities through public GitHub issues, discussions, or pull requests.**
14+
15+
Issues can be reported privately to the maintainers by opening a [Security vulnerability report](https://github.com/PHPCSStandards/PHPCSUtils/security/advisories/new).
16+
17+
### Preferences
18+
19+
* Please provide detailed reports with reproducible steps and a clearly defined impact.
20+
* Include the version number of the vulnerable package in your report.
21+
* Fixes are most welcome.
22+
A private PR can be created from the security report to work on and discuss the patch.

.github/workflows/basics.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ jobs:
143143
remark-preset-lint-markdown-style-guide
144144
remark-lint-checkbox-content-indent
145145
remark-lint-linebreak-style
146+
remark-lint-no-dead-urls
146147
remark-lint-no-duplicate-defined-urls
147148
remark-lint-no-empty-url
148149
remark-lint-no-heading-like-paragraph
@@ -154,7 +155,6 @@ jobs:
154155
remark-lint-list-item-punctuation
155156
remark-lint-match-punctuation
156157
remark-lint-no-hr-after-heading
157-
remark-lint-are-links-valid-alive
158158
remark-lint-are-links-valid-duplicate
159159
remark-validate-links
160160

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ jobs:
347347
- name: Upload coverage results to Coveralls
348348
if: ${{ success() }}
349349
env:
350-
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
350+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
351351
COVERALLS_PARALLEL: true
352352
COVERALLS_FLAG_NAME: php-${{ matrix.php }}-phpcs-${{ matrix.phpcs_version }}
353353
run: php-coveralls -v -x build/logs/clover.xml
@@ -362,5 +362,5 @@ jobs:
362362
- name: Coveralls Finished
363363
uses: coverallsapp/github-action@v2
364364
with:
365-
github-token: ${{ secrets.GITHUB_TOKEN }}
365+
github-token: ${{ secrets.COVERALLS_TOKEN }}
366366
parallel-finished: true

.remarkrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
["remark-lint-linebreak-style", "unix"],
99
["remark-lint-link-title-style", "\""],
1010
["remark-lint-ordered-list-marker-style", "."],
11+
"remark-lint-no-dead-urls",
1112
"remark-lint-no-duplicate-defined-urls",
1213
"remark-lint-no-duplicate-definitions",
1314
"remark-lint-no-empty-url",
@@ -29,7 +30,6 @@
2930
"remark-lint-list-item-punctuation",
3031
"remark-lint-match-punctuation",
3132
"remark-lint-no-hr-after-heading",
32-
"remark-lint-are-links-valid-alive",
3333
"remark-lint-are-links-valid-duplicate",
3434
"remark-validate-links"
3535
]

CHANGELOG.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@ This projects adheres to [Keep a CHANGELOG](https://keepachangelog.com/) and use
99

1010
_Nothing yet._
1111

12+
13+
## [1.0.6] - 2023-05-27
14+
15+
### Changed
16+
17+
#### PHPCS BackCompat
18+
19+
* `BCFile::getClassProperties()`: sync with PHPCS 3.8.0 - support for PHP 8.2 `readonly` classes. [#470]
20+
* `BCFile::getMethodParameters()`: sync with PHPCS 3.8.0 - support for constructor property promotion with `readonly` properties without explicit visibility. [#472]
21+
22+
#### Utils
23+
24+
* The results of the following methods will now (also) be cached for improved performance when multiple sniffs call these functions for the same token during a PHPCS run. [#464], [#466]
25+
- `FunctionDeclarations::getProperties()`
26+
- `Variables::getMemberProperties()`
27+
* Additionally, the results of the `UseStatements::splitImportUseStatement()` method will be cached more often and the cache checked earlier. [#467]
28+
* The return value of the `ControlStructures::getCaughtExceptions()` method will no longer contain "empty" entries for catch statements without a named exception. It will return an empty array instead. [#474]
29+
30+
#### Other
31+
32+
* Various small housekeeping and maintenance updates.
33+
34+
### Fixed
35+
36+
### Abstract Sniffs
37+
38+
* `AbstractArrayDeclarationSniff`: fixed a potential "Trying to access array offset on value of type bool" PHP notice. [#476]
39+
* `AbstractArrayDeclarationSniff`: the abstract will no longer trigger potentially available magic `__get()`/`__set()` etc methods. [#477]
40+
41+
[#464]: https://github.com/PHPCSStandards/PHPCSUtils/pull/464
42+
[#466]: https://github.com/PHPCSStandards/PHPCSUtils/pull/466
43+
[#467]: https://github.com/PHPCSStandards/PHPCSUtils/pull/467
44+
[#470]: https://github.com/PHPCSStandards/PHPCSUtils/pull/470
45+
[#472]: https://github.com/PHPCSStandards/PHPCSUtils/pull/472
46+
[#474]: https://github.com/PHPCSStandards/PHPCSUtils/pull/474
47+
[#476]: https://github.com/PHPCSStandards/PHPCSUtils/pull/476
48+
[#477]: https://github.com/PHPCSStandards/PHPCSUtils/pull/477
49+
50+
1251
## [1.0.5] - 2023-04-17
1352

1453
### Fixed
@@ -850,6 +889,7 @@ This initial alpha release contains the following utility classes:
850889

851890

852891
[Unreleased]: https://github.com/PHPCSStandards/PHPCSUtils/compare/stable...HEAD
892+
[1.0.6]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.5...1.0.6
853893
[1.0.5]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.4...1.0.5
854894
[1.0.4]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.3...1.0.4
855895
[1.0.3]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.2...1.0.3

PHPCSUtils/AbstractSniffs/AbstractArrayDeclarationSniff.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,14 @@ final public function process(File $phpcsFile, $stackPtr)
184184
return;
185185
}
186186

187+
$openClose = Arrays::getOpenClose($phpcsFile, $stackPtr, true);
188+
if ($openClose === false) {
189+
// Parse error or live coding.
190+
return;
191+
}
192+
187193
$this->stackPtr = $stackPtr;
188194
$this->tokens = $phpcsFile->getTokens();
189-
$openClose = Arrays::getOpenClose($phpcsFile, $stackPtr, true);
190195
$this->arrayOpener = $openClose['opener'];
191196
$this->arrayCloser = $openClose['closer'];
192197
$this->itemCount = \count($this->arrayItems);
@@ -199,7 +204,8 @@ final public function process(File $phpcsFile, $stackPtr)
199204
$this->processArray($phpcsFile);
200205

201206
// Reset select properties between calls to this sniff to lower memory usage.
202-
unset($this->tokens, $this->arrayItems);
207+
$this->tokens = [];
208+
$this->arrayItems = [];
203209
}
204210

205211
/**

PHPCSUtils/BackCompat/BCFile.php

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,25 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
138138
*
139139
* Parameters declared using PHP 8 constructor property promotion, have these additional array indexes:
140140
* ```php
141-
* 'property_visibility' => string, // The property visibility as declared.
142-
* 'visibility_token' => integer, // The stack pointer to the visibility modifier token.
143-
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
144-
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
145-
* // This index will only be set if the property is readonly.
141+
* 'property_visibility' => string, // The property visibility as declared.
142+
* 'visibility_token' => integer,|false // The stack pointer to the visibility modifier token.
143+
* // or FALSE if the visibility is not explicitly declared.
144+
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
145+
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
146+
* // This index will only be set if the property is readonly.
146147
* ```
147148
*
148149
* PHPCS cross-version compatible version of the `File::getMethodParameters()` method.
149150
*
150151
* Changelog for the PHPCS native function:
151152
* - Introduced in PHPCS 0.0.5.
152-
* - The upstream method has received no significant updates since PHPCS 3.7.1.
153+
* - PHPCS 3.8.0: Added support for constructor property promotion with readonly without explicit visibility.
153154
*
154155
* @see \PHP_CodeSniffer\Files\File::getMethodParameters() Original source.
155156
* @see \PHPCSUtils\Utils\FunctionDeclarations::getParameters() PHPCSUtils native improved version.
156157
*
157158
* @since 1.0.0
159+
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly properties without explicit visibility. PHPCS#3801.
158160
*
159161
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
160162
* @param int $stackPtr The position in the stack of the function token
@@ -380,15 +382,20 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
380382
$vars[$paramCount]['type_hint_end_token'] = $typeHintEndToken;
381383
$vars[$paramCount]['nullable_type'] = $nullableType;
382384

383-
if ($visibilityToken !== null) {
384-
$vars[$paramCount]['property_visibility'] = $tokens[$visibilityToken]['content'];
385-
$vars[$paramCount]['visibility_token'] = $visibilityToken;
385+
if ($visibilityToken !== null || $readonlyToken !== null) {
386+
$vars[$paramCount]['property_visibility'] = 'public';
387+
$vars[$paramCount]['visibility_token'] = false;
386388
$vars[$paramCount]['property_readonly'] = false;
387-
}
388389

389-
if ($readonlyToken !== null) {
390-
$vars[$paramCount]['property_readonly'] = true;
391-
$vars[$paramCount]['readonly_token'] = $readonlyToken;
390+
if ($visibilityToken !== null) {
391+
$vars[$paramCount]['property_visibility'] = $tokens[$visibilityToken]['content'];
392+
$vars[$paramCount]['visibility_token'] = $visibilityToken;
393+
}
394+
395+
if ($readonlyToken !== null) {
396+
$vars[$paramCount]['property_readonly'] = true;
397+
$vars[$paramCount]['readonly_token'] = $readonlyToken;
398+
}
392399
}
393400

394401
if ($tokens[$i]['code'] === T_COMMA) {
@@ -527,19 +534,21 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
527534
* array(
528535
* 'is_abstract' => boolean, // TRUE if the abstract keyword was found.
529536
* 'is_final' => boolean, // TRUE if the final keyword was found.
537+
* 'is_readonly' => false, // TRUE if the readonly keyword was found.
530538
* );
531539
* ```
532540
*
533541
* PHPCS cross-version compatible version of the `File::getClassProperties()` method.
534542
*
535543
* Changelog for the PHPCS native function:
536544
* - Introduced in PHPCS 1.3.0.
537-
* - The upstream method has received no significant updates since PHPCS 3.7.1.
545+
* - PHPCS 3.8.0: Added support for PHP 8.2 `readonly` classes.
538546
*
539547
* @see \PHP_CodeSniffer\Files\File::getClassProperties() Original source.
540548
* @see \PHPCSUtils\Utils\ObjectDeclarations::getClassProperties() PHPCSUtils native improved version.
541549
*
542550
* @since 1.0.0
551+
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly classes. PHPCS#3686.
543552
*
544553
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
545554
* @param int $stackPtr The position in the stack of the `T_CLASS`
@@ -552,7 +561,50 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
552561
*/
553562
public static function getClassProperties(File $phpcsFile, $stackPtr)
554563
{
555-
return $phpcsFile->getClassProperties($stackPtr);
564+
$tokens = $phpcsFile->getTokens();
565+
566+
if ($tokens[$stackPtr]['code'] !== T_CLASS) {
567+
throw new RuntimeException('$stackPtr must be of type T_CLASS');
568+
}
569+
570+
$valid = [
571+
T_FINAL => T_FINAL,
572+
T_ABSTRACT => T_ABSTRACT,
573+
T_READONLY => T_READONLY,
574+
T_WHITESPACE => T_WHITESPACE,
575+
T_COMMENT => T_COMMENT,
576+
T_DOC_COMMENT => T_DOC_COMMENT,
577+
];
578+
579+
$isAbstract = false;
580+
$isFinal = false;
581+
$isReadonly = false;
582+
583+
for ($i = ($stackPtr - 1); $i > 0; $i--) {
584+
if (isset($valid[$tokens[$i]['code']]) === false) {
585+
break;
586+
}
587+
588+
switch ($tokens[$i]['code']) {
589+
case T_ABSTRACT:
590+
$isAbstract = true;
591+
break;
592+
593+
case T_FINAL:
594+
$isFinal = true;
595+
break;
596+
597+
case T_READONLY:
598+
$isReadonly = true;
599+
break;
600+
}
601+
}
602+
603+
return [
604+
'is_abstract' => $isAbstract,
605+
'is_final' => $isFinal,
606+
'is_readonly' => $isReadonly,
607+
];
556608
}
557609

558610
/**

PHPCSUtils/Utils/ControlStructures.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public static function isElseIf(File $phpcsFile, $stackPtr)
209209
* 'type_end_token' => integer, // The stack pointer to the end of the type declaration.
210210
* )
211211
* ```
212+
* In case of an invalid catch structure, the array may be empty.
212213
*
213214
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified `$stackPtr` is not of
214215
* type `T_CATCH` or doesn't exist.
@@ -243,12 +244,14 @@ public static function getCaughtExceptions(File $phpcsFile, $stackPtr)
243244
}
244245

245246
if (isset(Collections::namespacedNameTokens()[$tokens[$i]['code']]) === false) {
246-
// Add the current exception to the result array.
247-
$exceptions[] = [
248-
'type' => $foundName,
249-
'type_token' => $firstToken,
250-
'type_end_token' => $lastToken,
251-
];
247+
// Add the current exception to the result array if one was found.
248+
if ($foundName !== '') {
249+
$exceptions[] = [
250+
'type' => $foundName,
251+
'type_token' => $firstToken,
252+
'type_end_token' => $lastToken,
253+
];
254+
}
252255

253256
if ($tokens[$i]['code'] === \T_BITWISE_OR) {
254257
// Multi-catch. Reset and continue.

PHPCSUtils/Utils/FunctionDeclarations.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public static function getName(File $phpcsFile, $stackPtr)
148148
* - Defensive coding against incorrect calls to this method.
149149
* - More efficient checking whether a function has a body.
150150
* - Support for PHP 8.0 identifier name tokens in return types, cross-version PHP & PHPCS.
151-
* - Support for constructor property promotion with the PHP 8.1 readonly keyword without explicit visibility.
152151
* - Support for the PHP 8.2 `true` type.
152+
* - The results of this function call are cached during a PHPCS run for faster response times.
153153
*
154154
* @see \PHP_CodeSniffer\Files\File::getMethodProperties() Original source.
155155
* @see \PHPCSUtils\BackCompat\BCFile::getMethodProperties() Cross-version compatible version of the original.
@@ -193,6 +193,10 @@ public static function getProperties(File $phpcsFile, $stackPtr)
193193
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or an arrow function');
194194
}
195195

196+
if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) {
197+
return Cache::get($phpcsFile, __METHOD__, $stackPtr);
198+
}
199+
196200
if ($tokens[$stackPtr]['code'] === \T_FUNCTION) {
197201
$valid = Tokens::$methodPrefixes;
198202
} else {
@@ -292,7 +296,7 @@ public static function getProperties(File $phpcsFile, $stackPtr)
292296
$returnType = '?' . $returnType;
293297
}
294298

295-
return [
299+
$returnValue = [
296300
'scope' => $scope,
297301
'scope_specified' => $scopeSpecified,
298302
'return_type' => $returnType,
@@ -304,6 +308,9 @@ public static function getProperties(File $phpcsFile, $stackPtr)
304308
'is_static' => $isStatic,
305309
'has_body' => $hasBody,
306310
];
311+
312+
Cache::set($phpcsFile, __METHOD__, $stackPtr, $returnValue);
313+
return $returnValue;
307314
}
308315

309316
/**

PHPCSUtils/Utils/Namespaces.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static function getDeclaredName(File $phpcsFile, $stackPtr, $clean = true
208208
}
209209

210210
/**
211-
* Determine the namespace an arbitrary token lives in.
211+
* Find the stack pointer to the namespace declaration applicable for an arbitrary token.
212212
*
213213
* Take note:
214214
* 1. When a namespace declaration token or a token which is part of the namespace

0 commit comments

Comments
 (0)