-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
ef0a839
commit f45f50e
Showing
11 changed files
with
635 additions
and
113 deletions.
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,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
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.