diff --git a/functionMap.php b/functionMap.php index 1434603..1a505e1 100644 --- a/functionMap.php +++ b/functionMap.php @@ -8,6 +8,10 @@ * * '' => ['', ''=>''] * + * For classes: + * + * '' => [null, ''=>''] + * * @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php */ return [ @@ -48,4 +52,17 @@ 'wp_slash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], 'wp_unschedule_event' => ['bool|WP_Error', 'args'=>$cronArgsType], 'wp_unslash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], + 'WP_REST_Request' => [null, '@phpstan-template'=>'T of array', '@phpstan-implements'=>'ArrayAccess, value-of>'], + 'WP_REST_Request::offsetExists' => ['bool', 'offset'=>'@param key-of'], + 'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template'=>'TOffset of key-of', 'offset'=>'TOffset'], + 'WP_REST_Request::offsetSet' => ['void', '@phpstan-template'=>'TOffset of key-of', 'offset'=>'TOffset', 'value'=>'T[TOffset]'], + 'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template'=>'TOffset of key-of', 'offset'=>'TOffset'], + '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::offsetExists' => ['($offset is ThemeKey ? true : false)'], + 'WP_Theme::offsetGet' => ['($offset is ThemeKey ? mixed : null)'], + 'WP_Block_List' => [null, '@phpstan-implements'=>'ArrayAccess'], + 'WP_Block_List::offsetExists' => ['bool', 'offset'=>'int'], + 'WP_Block_List::offsetGet' => ['WP_Block|null', 'offset'=>'int'], + 'WP_Block_List::offsetSet' => ['void', 'offset'=>'int|null'], + 'WP_Block_List::offsetUnset' => ['void', 'offset'=>'int'], ]; diff --git a/visitor.php b/visitor.php index e854414..08e93a1 100644 --- a/visitor.php +++ b/visitor.php @@ -10,6 +10,8 @@ use phpDocumentor\Reflection\Type; use PhpParser\Comment\Doc; use PhpParser\Node; +use PhpParser\Node\Identifier; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Property; @@ -262,7 +264,7 @@ public function enterNode(Node $node) { parent::enterNode($node); - if (!($node instanceof Function_) && !($node instanceof ClassMethod) && !($node instanceof Property)) { + if (!($node instanceof Function_) && !($node instanceof ClassMethod) && !($node instanceof Property) && !($node instanceof Class_)) { return null; } @@ -305,7 +307,7 @@ public function enterNode(Node $node) private static function getNodeName(Node $node): string { - if (($node instanceof Function_) || ($node instanceof ClassMethod)) { + if ((($node instanceof Function_) || ($node instanceof ClassMethod) || ($node instanceof Class_)) && $node->name instanceof Identifier) { return $node->name->name; } @@ -341,7 +343,7 @@ private function postProcessNode(Node $node): void } } - if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod) && ! ($node instanceof Property)) { + if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod) && ! ($node instanceof Property) && ! ($node instanceof Class_)) { return; } @@ -646,10 +648,12 @@ private function getAdditionalTagsFromMap(string $symbolName): array ); } - $additions[] = sprintf( - '@phpstan-return %s', - $returnType - ); + if ($returnType) { + $additions[] = sprintf( + '@phpstan-return %s', + $returnType + ); + } return $additions; } diff --git a/wordpress-stubs.php b/wordpress-stubs.php index f435a14..9157cb6 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -30855,6 +30855,7 @@ public function __construct(array $settings = array()) * Class representing a list of block instances. * * @since 5.5.0 + * @phpstan-implements ArrayAccess */ #[\AllowDynamicProperties] class WP_Block_List implements \Iterator, \ArrayAccess, \Countable @@ -30907,6 +30908,8 @@ public function __construct($blocks, $available_context = array(), $registry = \ * * @param string $index Index of block to check. * @return bool Whether block exists. + * @phpstan-param int $offset + * @phpstan-return bool */ #[\ReturnTypeWillChange] public function offsetExists($index) @@ -30921,6 +30924,8 @@ public function offsetExists($index) * * @param string $index Index of block value to retrieve. * @return mixed|null Block value if exists, or null. + * @phpstan-param int $offset + * @phpstan-return WP_Block|null */ #[\ReturnTypeWillChange] public function offsetGet($index) @@ -30935,6 +30940,8 @@ public function offsetGet($index) * * @param string $index Index of block value to set. * @param mixed $value Block value. + * @phpstan-param int|null $offset + * @phpstan-return void */ #[\ReturnTypeWillChange] public function offsetSet($index, $value) @@ -30948,6 +30955,8 @@ public function offsetSet($index, $value) * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php * * @param string $index Index of block value to unset. + * @phpstan-param int $offset + * @phpstan-return void */ #[\ReturnTypeWillChange] public function offsetUnset($index) @@ -51815,6 +51824,7 @@ public function set_spacing_sizes() * @package WordPress * @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' */ #[\AllowDynamicProperties] final class WP_Theme implements \ArrayAccess @@ -51904,6 +51914,7 @@ public function offsetUnset($offset) * * @param mixed $offset * @return bool + * @phpstan-return ($offset is ThemeKey ? true : false) */ #[\ReturnTypeWillChange] public function offsetExists($offset) @@ -51923,6 +51934,7 @@ public function offsetExists($offset) * * @param mixed $offset * @return mixed + * @phpstan-return ($offset is ThemeKey ? mixed : null) */ #[\ReturnTypeWillChange] public function offsetGet($offset) @@ -62032,6 +62044,8 @@ public function merge_with(&$other) * @since 4.4.0 * * @link https://www.php.net/manual/en/class.arrayaccess.php + * @phpstan-template T of array + * @phpstan-implements ArrayAccess, value-of> */ #[\AllowDynamicProperties] class WP_REST_Request implements \ArrayAccess @@ -62565,6 +62579,8 @@ public function has_valid_params() * * @param string $offset Parameter name. * @return bool Whether the parameter is set. + * @phpstan-param @param key-of $offset + * @phpstan-return bool */ #[\ReturnTypeWillChange] public function offsetExists($offset) @@ -62577,6 +62593,9 @@ public function offsetExists($offset) * * @param string $offset Parameter name. * @return mixed|null Value if set, null otherwise. + * @phpstan-template TOffset of key-of + * @phpstan-param TOffset $offset + * @phpstan-return T[TOffset] */ #[\ReturnTypeWillChange] public function offsetGet($offset) @@ -62589,6 +62608,10 @@ public function offsetGet($offset) * * @param string $offset Parameter name. * @param mixed $value Parameter value. + * @phpstan-template TOffset of key-of + * @phpstan-param TOffset $offset + * @phpstan-param T[TOffset] $value + * @phpstan-return void */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) @@ -62600,6 +62623,9 @@ public function offsetSet($offset, $value) * @since 4.4.0 * * @param string $offset Parameter name. + * @phpstan-template TOffset of key-of + * @phpstan-param TOffset $offset + * @phpstan-return void */ #[\ReturnTypeWillChange] public function offsetUnset($offset)