Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7136: Discourage use of update_option() #7139

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 225 additions & 0 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ parameters:
- %currentWorkingDirectory%/inc/classes/subscriber/third-party/
- %currentWorkingDirectory%/inc/3rd-party/
rules:
- WP_Rocket\Tests\phpstan\Rules\DiscourageUpdateOptionUsage
- WP_Rocket\Tests\phpstan\Rules\DiscourageApplyFilters
34 changes: 34 additions & 0 deletions tests/phpstan/Rules/DiscourageUpdateOptionUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace WP_Rocket\Tests\phpstan\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

class DiscourageUpdateOptionUsage implements Rule
{
public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode( Node $node, Scope $scope ): array
{
if (!$node instanceof FuncCall) {
return [];
}

if ( $node->name instanceof Node\Name && $node->name->toString() === 'update_option' ) {
return [
RuleErrorBuilder::message( 'Usage of update_option() is discouraged. Use the Option object instead.' )
->identifier( 'custom.rules.discourageApplyFilters' )
->build(),
];
}

return [];
}
}
28 changes: 28 additions & 0 deletions tests/phpstan/tests/Rules/DiscourageUpdateOptionUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace WP_Rocket\Tests\phpstan\tests\Rules;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use WP_Rocket\Tests\phpstan\Rules\DiscourageUpdateOptionUsage;

class DiscourageUpdateOptionUsageTest extends RuleTestCase {

protected function getRule(): Rule {
return new DiscourageUpdateOptionUsage();
}

public function testValidShouldNotHaveErrors() {
$this->analyse([__DIR__ . '/../data/DiscourageUpdateOptionUsageTest/valid.php'], [
]);
}

public function testShouldGetError() {
$this->analyse([__DIR__ . '/../data/DiscourageUpdateOptionUsageTest/not-valid.php'], [
[
"Usage of update_option() is discouraged. Use the Option object instead.",
38
]
]);
}
}
3 changes: 3 additions & 0 deletions tests/phpstan/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../../../vendor/autoload.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace WP_Rocket\Engine\Admin;

use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\ThirdParty\ReturnTypesTrait;

class ActionSchedulerSubscriber implements Subscriber_Interface {

use ReturnTypesTrait;

/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'action_scheduler_check_pastdue_actions' => 'return_false',
'action_scheduler_extra_action_counts' => [
['hide_pastdue_status_filter'],
['hide_pastdue_status_filter', 10, 3],
],
];
}

/**
* Hide past-due from status filter in Action Scheduler tools page.
*
* @param array $extra_actions Array with format action_count_identifier => action count.
*
* @return array
*/
public function hide_pastdue_status_filter( array $extra_actions ) {
if ( ! isset( $extra_actions['past-due'] ) ) {
return $extra_actions;
}
update_option( 'test_option', $extra_actions['past-due'] );

unset( $extra_actions['past-due'] );


return $extra_actions;
}
}
55 changes: 55 additions & 0 deletions tests/phpstan/tests/data/DiscourageUpdateOptionUsageTest/valid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace WP_Rocket\Engine\Admin;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\ThirdParty\ReturnTypesTrait;

class ActionSchedulerSubscriber implements Subscriber_Interface {

/**
* @var Options_Data
*/
private $option;

use ReturnTypesTrait;

public function __construct( Options_Data $option ) {
$this->option = $option;
}

/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'action_scheduler_check_pastdue_actions' => 'return_false',
'action_scheduler_extra_action_counts' => [
['hide_pastdue_status_filter'],
['hide_pastdue_status_filter', 10, 3],
],
];
}

/**
* Hide past-due from status filter in Action Scheduler tools page.
*
* @param array $extra_actions Array with format action_count_identifier => action count.
*
* @return array
*/
public function hide_pastdue_status_filter( array $extra_actions ) {
if ( ! isset( $extra_actions['past-due'] ) ) {
return $extra_actions;
}
$this->option->set( 'test_option', $extra_actions['past-due'] );

unset( $extra_actions['past-due'] );


return $extra_actions;
}
}
27 changes: 27 additions & 0 deletions tests/phpstan/tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>Rules</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">Rules</directory>
</include>
</coverage>
</phpunit>
Loading