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

Add WP_Theme magic properties #216

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
19 changes: 18 additions & 1 deletion functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,24 @@
'WP_REST_Request::get_params' => ['T'],
'WP_REST_Request::set_param' => ['void', '@phpstan-template' => 'TOffset of key-of<T>', 'key' => 'TOffset', 'value' => 'T[TOffset]'],
'WP_REST_Request::has_param' => [null, 'key' => 'key-of<T>'],
'WP_Theme' => [null, '@phpstan-type' => "ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'"],
'WP_Theme' => [
null,
'@phpstan-type' => "ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'",
'@phpstan-property-read string $name' => '',
'@phpstan-property-read string $title' => '',
'@phpstan-property-read string $version' => '',
'@phpstan-property-read string $parent_theme' => '',
'@phpstan-property-read string $template_dir' => '',
'@phpstan-property-read string $stylesheet_dir' => '',
'@phpstan-property-read string $template' => '',
'@phpstan-property-read string $stylesheet' => '',
'@phpstan-property-read string $screenshot' => '',
'@phpstan-property-read string $description' => '',
'@phpstan-property-read string $author' => '',
'@phpstan-property-read list<string> $tags' => '',
'@phpstan-property-read string $theme_root' => '',
'@phpstan-property-read string $theme_root_uri' => '',
],
'WP_Theme::get' => ["(\$header is 'Name'|'ThemeURI'|'Description'|'Author'|'AuthorURI'|'Version'|'Template'|'Status'|'Tags'|'TextDomain'|'DomainPath'|'RequiresWP'|'RequiresPHP'|'UpdateURI' ? (\$header is 'Tags' ? string[] : string) : false)"],
'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'],
'WP_Theme::offsetGet' => ['($offset is ThemeKey ? mixed : null)'],
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion">
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps">
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="Squiz.PHP.CommentedOutCode">
<exclude-pattern>tests/</exclude-pattern>
</rule>
Expand Down
19 changes: 18 additions & 1 deletion tests/data/wp_theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@
use function PHPStan\Testing\assertType;

/** @var \WP_Theme */
$theme = $theme;
$theme = $_GET['theme'];

// WP_Theme::__get()
assertType('string', $theme->name);
assertType('string', $theme->title);
assertType('string', $theme->version);
assertType('string', $theme->parent_theme);
assertType('string', $theme->template_dir);
assertType('string', $theme->stylesheet_dir);
assertType('string', $theme->template);
assertType('string', $theme->stylesheet);
assertType('string', $theme->screenshot);
assertType('string', $theme->description);
assertType('string', $theme->author);
assertType('list<string>', $theme->tags);
assertType('string', $theme->theme_root);
assertType('string', $theme->theme_root_uri);
assertType('*ERROR*', $theme->unknown_property);

// WP_Theme::get()
assertType('string', $theme->get('Name'));
Expand Down
14 changes: 14 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -54509,6 +54509,20 @@ protected static function get_valid_block_style_variations()
* @subpackage Theme
* @since 3.4.0
* @phpstan-type ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'
* @phpstan-property-read string $name
* @phpstan-property-read string $title
* @phpstan-property-read string $version
* @phpstan-property-read string $parent_theme
* @phpstan-property-read string $template_dir
* @phpstan-property-read string $stylesheet_dir
* @phpstan-property-read string $template
* @phpstan-property-read string $stylesheet
* @phpstan-property-read string $screenshot
* @phpstan-property-read string $description
* @phpstan-property-read string $author
* @phpstan-property-read list<string> $tags
* @phpstan-property-read string $theme_root
* @phpstan-property-read string $theme_root_uri
*/
#[\AllowDynamicProperties]
final class WP_Theme implements \ArrayAccess
Expand Down