Skip to content

Commit

Permalink
Add WP_Theme magic properties (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Sep 2, 2024
1 parent ccde227 commit f5231f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
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

0 comments on commit f5231f6

Please sign in to comment.