Skip to content

Commit d655404

Browse files
committed
Code Quality: Mark current_user_can() and chmod() as "impure".
PHPStan infers `current_user_can()` and `WP_Filesystem_Base::chmod()` as pure, so it remembers what each returned and reuses that value at a later call in the same scope, reporting the second check as always true or always false. Yet a capability check can legitimately answer differently the second time, since it reads the current user and applies the `user_has_cap` filter, so a plugin hooked in between (for example during the `admin-header.php` load in `wp-admin/link-manager.php`) can change the result. Similarly, `chmod()` changes the permissions that a following `is_writable()` reports. The annotation goes on the call that invalidates the remembered value rather than on read-only accessors like `is_writable()`, and on `current_user_can()` itself since annotating `WP_User::has_cap()` cleared nothing. This resolves 13 baselined errors across five identifiers. Developed in #13074. Follow-up to r63023, r63301, r63357, r64335. Props tstokes8040. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@64336 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4be1db5 commit d655404

7 files changed

Lines changed: 4 additions & 55 deletions

File tree

‎src/wp-admin/includes/class-wp-filesystem-base.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@ public function chgrp( $file, $group, $recursive = false ) {
606606
* @param bool $recursive Optional. If set to true, changes file permissions recursively.
607607
* Default false.
608608
* @return bool True on success, false on failure.
609+
*
610+
* @phpstan-impure
609611
*/
610612
public function chmod( $file, $mode = false, $recursive = false ) {
611613
return false;

‎src/wp-includes/capabilities.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,8 @@ function map_meta_cap( $cap, $user_id, ...$args ) {
909909
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
910910
* @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
911911
* passed, whether the current user has the given meta capability for the given object.
912+
*
913+
* @phpstan-impure
912914
*/
913915
function current_user_can( $capability, ...$args ) {
914916
return user_can( wp_get_current_user(), $capability, ...$args );

‎tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^Left side of && is always true\.$#'
23-
identifier: booleanAnd.leftAlwaysTrue
24-
count: 1
25-
path: ../../../src/wp-admin/network/users.php
2621
-
2722
message: '#^Left side of && is always true\.$#'
2823
identifier: booleanAnd.leftAlwaysTrue

‎tests/phpstan/baselines/booleanNot.alwaysFalse.neon‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^Negated boolean expression is always false\.$#'
23-
identifier: booleanNot.alwaysFalse
24-
count: 1
25-
path: ../../../src/wp-admin/includes/theme.php
26-
-
27-
message: '#^Negated boolean expression is always false\.$#'
28-
identifier: booleanNot.alwaysFalse
29-
count: 1
30-
path: ../../../src/wp-admin/link-manager.php
31-
-
32-
message: '#^Negated boolean expression is always false\.$#'
33-
identifier: booleanNot.alwaysFalse
34-
count: 2
35-
path: ../../../src/wp-admin/network/users.php
36-
-
37-
message: '#^Negated boolean expression is always false\.$#'
38-
identifier: booleanNot.alwaysFalse
39-
count: 1
40-
path: ../../../src/wp-admin/plugins.php
4121
-
4222
message: '#^Negated boolean expression is always false\.$#'
4323
identifier: booleanNot.alwaysFalse

‎tests/phpstan/baselines/booleanNot.alwaysTrue.neon‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ parameters:
2323
identifier: booleanNot.alwaysTrue
2424
count: 1
2525
path: ../../../src/wp-admin/includes/class-custom-image-header.php
26-
-
27-
message: '#^Negated boolean expression is always true\.$#'
28-
identifier: booleanNot.alwaysTrue
29-
count: 1
30-
path: ../../../src/wp-admin/includes/class-language-pack-upgrader.php
31-
-
32-
message: '#^Negated boolean expression is always true\.$#'
33-
identifier: booleanNot.alwaysTrue
34-
count: 1
35-
path: ../../../src/wp-admin/includes/class-wp-upgrader.php
36-
-
37-
message: '#^Negated boolean expression is always true\.$#'
38-
identifier: booleanNot.alwaysTrue
39-
count: 1
40-
path: ../../../src/wp-admin/includes/file.php
4126
-
4227
message: '#^Negated boolean expression is always true\.$#'
4328
identifier: booleanNot.alwaysTrue

‎tests/phpstan/baselines/if.alwaysTrue.neon‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^If condition is always true\.$#'
23-
identifier: if.alwaysTrue
24-
count: 1
25-
path: ../../../src/wp-admin/my-sites.php
26-
-
27-
message: '#^If condition is always true\.$#'
28-
identifier: if.alwaysTrue
29-
count: 2
30-
path: ../../../src/wp-admin/upload.php
3121
-
3222
message: '#^If condition is always true\.$#'
3323
identifier: if.alwaysTrue

‎tests/phpstan/baselines/ternary.alwaysTrue.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ parameters:
2323
identifier: ternary.alwaysTrue
2424
count: 1
2525
path: ../../../src/wp-admin/menu-header.php
26-
-
27-
message: '#^Ternary operator condition is always true\.$#'
28-
identifier: ternary.alwaysTrue
29-
count: 1
30-
path: ../../../src/wp-admin/theme-install.php

0 commit comments

Comments
 (0)