|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Custom rule to discourage using WordPress constants fetchable via functions. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace SzepeViktor\PHPStan\WordPress; |
| 10 | + |
| 11 | +use PhpParser\Node; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | + |
| 15 | +use function array_key_exists; |
| 16 | +use function sprintf; |
| 17 | + |
| 18 | +/** |
| 19 | + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\ConstFetch> |
| 20 | + */ |
| 21 | +final class WpConstantFetchRule implements \PHPStan\Rules\Rule |
| 22 | +{ |
| 23 | + protected const REPLACEMENTS = [ |
| 24 | + 'BACKGROUND_COLOR' => "get_theme_support('custom-background')", |
| 25 | + 'BACKGROUND_IMAGE' => "get_theme_support('custom-background')", |
| 26 | + 'DISALLOW_FILE_MODS' => 'wp_is_file_mod_allowed()', |
| 27 | + 'DOING_AJAX' => 'wp_doing_ajax()', |
| 28 | + 'DOING_CRON' => 'wp_doing_cron()', |
| 29 | + 'FORCE_SSL_ADMIN' => 'force_ssl_admin()', |
| 30 | + 'FORCE_SSL_LOGIN' => 'force_ssl_admin()', |
| 31 | + 'FS_METHOD' => 'get_filesystem_method()', |
| 32 | + 'HEADER_IMAGE' => "get_theme_support('custom-header\)", |
| 33 | + 'HEADER_IMAGE_WIDTH' => "get_theme_support('custom-header')", |
| 34 | + 'HEADER_IMAGE_HEIGHT' => "get_theme_support('custom-header')", |
| 35 | + 'HEADER_TEXTCOLOR' => "get_theme_support('custom-header')", |
| 36 | + 'MULTISITE' => 'is_multisite()', |
| 37 | + 'NO_HEADER_TEXT' => "get_theme_support('custom-header')", |
| 38 | + 'STYLESHEETPATH' => 'get_stylesheet_directory()', |
| 39 | + 'SUBDOMAIN_INSTALL' => 'is_subdomain_install()', |
| 40 | + 'TEMPLATEPATH' => 'get_template_directory()', |
| 41 | + 'UPLOADS' => 'wp_get_upload_dir() or wp_upload_dir(null, false)', |
| 42 | + 'VHOST' => 'is_subdomain_install()', |
| 43 | + 'WP_ADMIN' => 'is_admin()', |
| 44 | + 'WP_BLOG_ADMIN' => 'is_blog_admin()', |
| 45 | + 'WP_CONTENT_URL' => 'content_url()', |
| 46 | + 'WP_DEVELOPMENT_MODE' => 'wp_get_development_mode()', |
| 47 | + 'WP_HOME' => 'home_url() or get_home_url()', |
| 48 | + 'WP_INSTALLING' => 'wp_installing()', |
| 49 | + 'WP_NETWORK_ADMIN' => 'is_network_admin()', |
| 50 | + 'WP_PLUGIN_URL' => "plugins_url() or plugin_dir_url('')", |
| 51 | + 'WP_POST_REVISIONS' => 'wp_revisions_to_keep()', |
| 52 | + 'WP_SITEURL' => 'site_url() or get_site_url()', |
| 53 | + 'WP_USER_ADMIN' => 'is_user_admin()', |
| 54 | + 'WP_USE_THEMES' => 'wp_using_themes()', |
| 55 | + 'WPMU_PLUGIN_URL' => 'plugins_url()', |
| 56 | + ]; |
| 57 | + |
| 58 | + public function getNodeType(): string |
| 59 | + { |
| 60 | + return Node\Expr\ConstFetch::class; |
| 61 | + } |
| 62 | + |
| 63 | + public function processNode(Node $node, Scope $scope): array |
| 64 | + { |
| 65 | + if (! $this->isDiscouragedConstant($node->name)) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + return [ |
| 70 | + RuleErrorBuilder::message( |
| 71 | + sprintf( |
| 72 | + 'Found usage of constant %s. Use %s instead.', |
| 73 | + (string)$node->name, |
| 74 | + $this->getReplacement($node->name) |
| 75 | + ) |
| 76 | + ) |
| 77 | + ->identifier('phpstanWP.wpConstant.fetch') |
| 78 | + ->build(), |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + private function isDiscouragedConstant(Node\Name $constantName): bool |
| 83 | + { |
| 84 | + return array_key_exists((string)$constantName, self::REPLACEMENTS); |
| 85 | + } |
| 86 | + |
| 87 | + private function getReplacement(Node\Name $constantName): string |
| 88 | + { |
| 89 | + return self::REPLACEMENTS[(string)$constantName]; |
| 90 | + } |
| 91 | +} |
0 commit comments