Skip to content

Commit 3202749

Browse files
committed
Performance/CacheValueOverride: add some more defensive coding
... to guard against the sniff throwing PHP notices/warnings due to parse errors/live coding. Any such PHP notice/warning stops the PHPCS run for a file, so should be avoided. Includes tests.
1 parent 336f566 commit 3202749

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

WordPressVIPMinimum/Sniffs/Performance/CacheValueOverrideSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,16 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
101101

102102
$rightAfterNextVariableOccurence = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextVariableOccurrence + 1, $searchEnd, true, null, true );
103103

104-
if ( $this->tokens[ $rightAfterNextVariableOccurence ]['code'] !== T_EQUAL ) {
104+
if ( $rightAfterNextVariableOccurence === false
105+
|| $this->tokens[ $rightAfterNextVariableOccurence ]['code'] !== T_EQUAL
106+
) {
105107
// Not a value override.
106108
return;
107109
}
108110

109111
$valueAfterEqualSign = $this->phpcsFile->findNext( Tokens::$emptyTokens, $rightAfterNextVariableOccurence + 1, $searchEnd, true, null, true );
110112

111-
if ( $this->tokens[ $valueAfterEqualSign ]['code'] === T_FALSE ) {
113+
if ( $valueAfterEqualSign !== false && $this->tokens[ $valueAfterEqualSign ]['code'] === T_FALSE ) {
112114
$message = 'Obtained cached value in `%s` is being overridden. Disabling caching?';
113115
$data = [ $variableName ];
114116
$this->phpcsFile->addError( $message, $nextVariableOccurrence, 'CacheValueOverride', $data );
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Parse error/live coding test.
4+
// This should be the only test in the file.
5+
$liveCoding = wp_cache_get();
6+
$liveCoding
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Parse error/live coding test.
4+
// This should be the only test in the file.
5+
$liveCoding = wp_cache_get();
6+
$liveCoding =

0 commit comments

Comments
 (0)