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 9 commits into
base: develop
Choose a base branch
from
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.

42 changes: 42 additions & 0 deletions tests/phpstan/Rules/DiscourageWPOptionUsage.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is inconsistency in code styling/formatting in this file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, Fixed.
Do u think we need to include this folder into the coverage of phpcs to catch those things?

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' => 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;
}
}
Loading