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 #7126: PHPStan ~ Discourage wp option function #7155

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
475 changes: 475 additions & 0 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.

42 changes: 42 additions & 0 deletions tests/phpstan/Rules/DiscourageWPOptionUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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' => 'Usage of update_option() is discouraged. Use the Option object instead.',
'get_option' => 'Usage of get_option() is discouraged. Use the Option object instead.',
'delete_option' => 'Usage of delete_option() is discouraged. Use the Option object instead.',
];

if (isset($discouragedFunctions[$functionName])) {
return [
RuleErrorBuilder::message($discouragedFunctions[$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;
}
}
Loading