Skip to content

Commit

Permalink
Closes #7126: PHPStan ~ Discourage wp option function (#7155)
Browse files Browse the repository at this point in the history
Co-authored-by: WordPressFan <[email protected]>
Co-authored-by: WordPress Fan <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent ef0a839 commit f45f50e
Show file tree
Hide file tree
Showing 11 changed files with 635 additions and 113 deletions.
490 changes: 485 additions & 5 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

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

This file was deleted.

39 changes: 39 additions & 0 deletions tests/phpstan/Rules/DiscourageWPOptionUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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 DiscourageWPOptionUsage implements Rule {
public function getNodeType(): string {
return FuncCall::class;
}

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

$functionName = $node->name instanceof Node\Name ? $node->name->toString() : '';

$discouragedFunctions = [
'update_option' => true,
'get_option' => true,
'delete_option' => true,
];

if ( isset( $discouragedFunctions[ $functionName ] ) ) {
return [
RuleErrorBuilder::message( sprintf( 'Usage of %1$s() is discouraged. Use the Option object instead.', $functionName ) )
->identifier('custom.rules.discourageOptionUsage')
->build(),
];
}

return [];
}
}
28 changes: 0 additions & 28 deletions tests/phpstan/tests/Rules/DiscourageUpdateOptionUsageTest.php

This file was deleted.

46 changes: 46 additions & 0 deletions tests/phpstan/tests/Rules/DiscourageWPOptionUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace WP_Rocket\Tests\phpstan\tests\Rules;

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

class DiscourageWPOptionUsageTest extends RuleTestCase {

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

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

public function testShouldHaveErrorWithDeleteOption() {
$this->analyse([__DIR__ . '/../data/DiscourageWPOptionUsageTest/use-delete-option.php'], [
[
"Usage of delete_option() is discouraged. Use the Option object instead.",
19
]
]);
}

public function testShouldHaveErrorWithGetOption() {
$this->analyse([__DIR__ . '/../data/DiscourageWPOptionUsageTest/use-get-option.php'], [
[
"Usage of get_option() is discouraged. Use the Option object instead.",
19
]
]);
}

public function testShouldHaveErrorWithUpdateOption() {
$this->analyse([__DIR__ . '/../data/DiscourageWPOptionUsageTest/use-update-option.php'], [
[
"Usage of update_option() is discouraged. Use the Option object instead.",
19
]
]);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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;

public static function get_subscribed_events() {
return [
'hook' => 'callback',
];
}

public function callback() {
return delete_option('test_option' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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;

public static function get_subscribed_events() {
return [
'hook' => 'callback',
];
}

public function callback() {
return get_option('test_option', false );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

public static function get_subscribed_events() {
return [
'hook' => 'callback',
];
}

public function callback() {
update_option('test_option', false );
return true;
}
}

0 comments on commit f45f50e

Please sign in to comment.