-
Notifications
You must be signed in to change notification settings - Fork 227
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
Miraeld
wants to merge
9
commits into
develop
Choose a base branch
from
enhancement/7126-discourage-wp-option-function
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12e9733
Refactor Update Option rule
Miraeld 20b724b
Update tests
Miraeld cd63845
Update baseline
Miraeld 2424863
simplify the message generation
wordpressfan a62afff
try changing the command to show the whole details about the error
wordpressfan 943fec4
Merge branch 'develop' into enhancement/7126-discourage-wp-option-fun…
wordpressfan 991ae9c
update the baseline after merging develop
wordpressfan 9b7d05f
fix file formatting
wordpressfan 90cbdf3
Merge branch 'develop' into enhancement/7126-discourage-wp-option-fun…
wordpressfan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
tests/phpstan/tests/Rules/DiscourageUpdateOptionUsageTest.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
]); | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
tests/phpstan/tests/data/DiscourageUpdateOptionUsageTest/not-valid.php
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
tests/phpstan/tests/data/DiscourageWPOptionUsageTest/use-delete-option.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/phpstan/tests/data/DiscourageWPOptionUsageTest/use-get-option.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/phpstan/tests/data/DiscourageWPOptionUsageTest/use-update-option.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?