Skip to content

Commit 2fc44b2

Browse files
committed
Fix tests
1 parent 4ebaf58 commit 2fc44b2

File tree

5 files changed

+49
-39
lines changed

5 files changed

+49
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/composer.lock
12
/vendor/

composer.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
{
22
"require-dev": {
3-
"wp-coding-standards/wpcs": "^2.1.0",
4-
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
5-
"szepeviktor/phpstan-wordpress": "^0.2.0",
6-
"phpstan/phpstan-shim": "^0.11.16",
7-
"php-stubs/wordpress-stubs": "^4.9"
3+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
4+
"php-stubs/wordpress-stubs": "^4.9",
5+
"szepeviktor/phpstan-wordpress": "^1.0",
6+
"wp-coding-standards/wpcs": "^2.1"
7+
},
8+
"config": {
9+
"allow-plugins": {
10+
"dealerdirect/phpcodesniffer-composer-installer": true
11+
}
812
},
913
"scripts": {
10-
"phpcs": "vendor/bin/phpcs",
11-
"phpstan": "vendor/bin/phpstan analyze",
14+
"phpcs": "phpcs",
15+
"phpstan": "phpstan analyze",
1216
"test": [
1317
"@phpcs",
1418
"@phpstan"

phpcs.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
</properties>
2020
</rule>
2121
<rule ref="WordPress-Core">
22+
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
23+
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
2224
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/>
2325
</rule>
24-
<!-- <rule ref="WordPress-Docs"/> -->
25-
<rule ref="WordPress-Extra"/>
26-
2726
<!--
28-
<rule ref="WordPress.PHP.DiscouragedPHPFunctions">
29-
<properties>
30-
<property name="exclude" value="obfuscation"/>
31-
</properties>
32-
</rule>
27+
<rule ref="WordPress-Docs"/>
3328
-->
29+
<rule ref="WordPress-Extra"/>
3430
</ruleset>

phpstan.neon

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
includes:
2-
- phar://phpstan.phar/conf/bleedingEdge.neon
32
- vendor/szepeviktor/phpstan-wordpress/extension.neon
43
parameters:
54
level: max
65
paths:
7-
- %currentWorkingDirectory%/tiny-cache.php
8-
- %currentWorkingDirectory%/tiny-nav-menu-cache.php
9-
- %currentWorkingDirectory%/tiny-translation-cache.php
10-
autoload_files:
11-
- %currentWorkingDirectory%/tiny-cache.php
12-
- %currentWorkingDirectory%/tiny-nav-menu-cache.php
13-
- %currentWorkingDirectory%/tiny-translation-cache.php
6+
- tiny-cache.php
7+
- tiny-nav-menu-cache.php
8+
- tiny-translation-cache.php
149
ignoreErrors:
15-
- '#^Function do_action invoked with [345] parameters, 1-2 required\.$#'
10+
# - '#^Function do_action invoked with [345] parameters, 1-2 required\.$#'
1611
- '#^Function apply_filters invoked with [345] parameters, 2 required\.$#'
12+
-
13+
path: tiny-nav-menu-cache.php
14+
message: '#^Access to an undefined property object::\$menu_id\.$#'
15+
count: 1
16+

tiny-nav-menu-cache.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Tiny_Nav_Menu_Cache {
1414
private const GROUP = 'navmenu';
1515

1616
/**
17-
* @var array List of whitelisted query string fields (these do not prevent cache write).
17+
* @var array<string> List of whitelisted query string fields (these do not prevent cache write).
1818
*/
1919
private $whitelisted_query_string_fields = [
2020
// https://support.google.com/searchads/answer/7342044
@@ -35,6 +35,9 @@ public function __construct() {
3535
add_action( 'init', array( $this, 'init' ) );
3636
}
3737

38+
/**
39+
* @return void
40+
*/
3841
public function init() {
3942

4043
// Detect object cache
@@ -49,22 +52,22 @@ public function init() {
4952
add_action( 'split_shared_term', array( $this, 'flush_all' ) );
5053

5154
// Learned from W3TC Page Cache rules and WP Super Cache rules
52-
if ( is_user_logged_in() /* User is logged in */
53-
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) /* Not a GET request */ // WPCS: input var OK.
55+
if ( is_user_logged_in() /* User is logged in */
56+
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) /* Not a GET request */ // WPCS: input var OK.
5457
|| ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
5558
) {
5659
return;
5760
}
5861

59-
// Add user-defined query parameters to the whitelist. Define the parameters you
60-
// want whitelisted in wp-config in the following way:
61-
//
62-
// define('TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS', 'XDEBUG_TRIGGER|do_xhprof_profile');
62+
// Add user-defined query parameters to the whitelist. Define the parameters you
63+
// want whitelisted in wp-config in the following way:
64+
//
65+
// define('TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS', 'XDEBUG_TRIGGER|do_xhprof_profile');
6366

64-
if ( defined( 'TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS' ) ) {
65-
$fields = array_map( 'trim', explode( '|', TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS) );
66-
$this->whitelisted_query_string_fields = array_merge( $this->whitelisted_query_string_fields, $fields );
67-
}
67+
if ( defined( 'TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS' ) ) {
68+
$fields = array_map( 'trim', explode( '|', TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ) );
69+
$this->whitelisted_query_string_fields = array_merge( $this->whitelisted_query_string_fields, $fields );
70+
}
6871

6972
add_filter( 'pre_wp_nav_menu', array( $this, 'get_nav_menu' ), 30, 2 );
7073
add_filter( 'wp_nav_menu', array( $this, 'save_nav_menu' ), PHP_INT_MAX, 2 );
@@ -81,6 +84,7 @@ public function get_nav_menu( $nav_menu_html, $args ) {
8184
$found = null;
8285
$cache = wp_cache_get( $this->get_cache_key( $args ), self::GROUP, false, $found );
8386
if ( $found ) {
87+
/** @var string $cache */
8488
return $cache;
8589
}
8690
}
@@ -104,6 +108,9 @@ public function save_nav_menu( $nav_menu_html, $args ) {
104108
return $nav_menu_html;
105109
}
106110

111+
/**
112+
* @return void
113+
*/
107114
public function flush_all() {
108115

109116
foreach ( $this->get_all_keys() as $key ) {
@@ -114,23 +121,25 @@ public function flush_all() {
114121

115122
/**
116123
* @param string $key
124+
* @return void
117125
*/
118126
private function remember_key( $key ) {
119127

120128
// @TODO Not atomic
121-
$found = false;
129+
$found = false;
122130
$key_list = wp_cache_get( 'key_list', self::GROUP, false, $found );
123131

124132
$key_list = $found ? $key_list . '|' . $key : $key;
125133
wp_cache_set( 'key_list', $key_list, self::GROUP, DAY_IN_SECONDS );
126134
}
127135

128136
/**
129-
* @return array
137+
* @return array<string>
130138
*/
131139
private function get_all_keys() {
132140

133-
$found = null;
141+
$found = null;
142+
/** @var string $key_list */
134143
$key_list = wp_cache_get( 'key_list', self::GROUP, false, $found );
135144
if ( ! $found ) {
136145
$key_list = '';
@@ -161,7 +170,7 @@ private function is_enabled( $args ) {
161170

162171
// Do not cache requests with query string except whitelisted ones.
163172
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
164-
if ( [] !== array_diff( array_keys( $_GET ), $this->whitelisted_query_string_fields) ) {
173+
if ( [] !== array_diff( array_keys( $_GET ), $this->whitelisted_query_string_fields ) ) {
165174
return false;
166175
}
167176

0 commit comments

Comments
 (0)