Skip to content

Commit ac0c474

Browse files
authored
Add annotations for classes extending ArrayAccess (#66)
* Add annotations for classes extending `ArrayAccess` * Update `getNodeName()` * Update `postProcessNode()` * Regenerate stubs * name can be null * Update map * Update null check * Stay positive
1 parent db1e23a commit ac0c474

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

functionMap.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*
99
* '<function_name>' => ['<return_type>', '<arg_name>'=>'<arg_type>']
1010
*
11+
* For classes:
12+
*
13+
* '<class_name>' => [null, '<arg_name>'=>'<arg_type>']
14+
*
1115
* @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php
1216
*/
1317
return [
@@ -48,4 +52,17 @@
4852
'wp_slash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
4953
'wp_unschedule_event' => ['bool|WP_Error', 'args'=>$cronArgsType],
5054
'wp_unslash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
55+
'WP_REST_Request' => [null, '@phpstan-template'=>'T of array', '@phpstan-implements'=>'ArrayAccess<key-of<T>, value-of<T>>'],
56+
'WP_REST_Request::offsetExists' => ['bool', 'offset'=>'@param key-of<T>'],
57+
'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template'=>'TOffset of key-of<T>', 'offset'=>'TOffset'],
58+
'WP_REST_Request::offsetSet' => ['void', '@phpstan-template'=>'TOffset of key-of<T>', 'offset'=>'TOffset', 'value'=>'T[TOffset]'],
59+
'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template'=>'TOffset of key-of<T>', 'offset'=>'TOffset'],
60+
'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'"],
61+
'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'],
62+
'WP_Theme::offsetGet' => ['($offset is ThemeKey ? mixed : null)'],
63+
'WP_Block_List' => [null, '@phpstan-implements'=>'ArrayAccess<int, WP_Block>'],
64+
'WP_Block_List::offsetExists' => ['bool', 'offset'=>'int'],
65+
'WP_Block_List::offsetGet' => ['WP_Block|null', 'offset'=>'int'],
66+
'WP_Block_List::offsetSet' => ['void', 'offset'=>'int|null'],
67+
'WP_Block_List::offsetUnset' => ['void', 'offset'=>'int'],
5168
];

visitor.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use phpDocumentor\Reflection\Type;
1111
use PhpParser\Comment\Doc;
1212
use PhpParser\Node;
13+
use PhpParser\Node\Identifier;
14+
use PhpParser\Node\Stmt\Class_;
1315
use PhpParser\Node\Stmt\ClassMethod;
1416
use PhpParser\Node\Stmt\Function_;
1517
use PhpParser\Node\Stmt\Property;
@@ -262,7 +264,7 @@ public function enterNode(Node $node)
262264
{
263265
parent::enterNode($node);
264266

265-
if (!($node instanceof Function_) && !($node instanceof ClassMethod) && !($node instanceof Property)) {
267+
if (!($node instanceof Function_) && !($node instanceof ClassMethod) && !($node instanceof Property) && !($node instanceof Class_)) {
266268
return null;
267269
}
268270

@@ -305,7 +307,7 @@ public function enterNode(Node $node)
305307

306308
private static function getNodeName(Node $node): string
307309
{
308-
if (($node instanceof Function_) || ($node instanceof ClassMethod)) {
310+
if ((($node instanceof Function_) || ($node instanceof ClassMethod) || ($node instanceof Class_)) && $node->name instanceof Identifier) {
309311
return $node->name->name;
310312
}
311313

@@ -341,7 +343,7 @@ private function postProcessNode(Node $node): void
341343
}
342344
}
343345

344-
if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod) && ! ($node instanceof Property)) {
346+
if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod) && ! ($node instanceof Property) && ! ($node instanceof Class_)) {
345347
return;
346348
}
347349

@@ -646,10 +648,12 @@ private function getAdditionalTagsFromMap(string $symbolName): array
646648
);
647649
}
648650

649-
$additions[] = sprintf(
650-
'@phpstan-return %s',
651-
$returnType
652-
);
651+
if ($returnType) {
652+
$additions[] = sprintf(
653+
'@phpstan-return %s',
654+
$returnType
655+
);
656+
}
653657

654658
return $additions;
655659
}

wordpress-stubs.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30855,6 +30855,7 @@ public function __construct(array $settings = array())
3085530855
* Class representing a list of block instances.
3085630856
*
3085730857
* @since 5.5.0
30858+
* @phpstan-implements ArrayAccess<int, WP_Block>
3085830859
*/
3085930860
#[\AllowDynamicProperties]
3086030861
class WP_Block_List implements \Iterator, \ArrayAccess, \Countable
@@ -30907,6 +30908,8 @@ public function __construct($blocks, $available_context = array(), $registry = \
3090730908
*
3090830909
* @param string $index Index of block to check.
3090930910
* @return bool Whether block exists.
30911+
* @phpstan-param int $offset
30912+
* @phpstan-return bool
3091030913
*/
3091130914
#[\ReturnTypeWillChange]
3091230915
public function offsetExists($index)
@@ -30921,6 +30924,8 @@ public function offsetExists($index)
3092130924
*
3092230925
* @param string $index Index of block value to retrieve.
3092330926
* @return mixed|null Block value if exists, or null.
30927+
* @phpstan-param int $offset
30928+
* @phpstan-return WP_Block|null
3092430929
*/
3092530930
#[\ReturnTypeWillChange]
3092630931
public function offsetGet($index)
@@ -30935,6 +30940,8 @@ public function offsetGet($index)
3093530940
*
3093630941
* @param string $index Index of block value to set.
3093730942
* @param mixed $value Block value.
30943+
* @phpstan-param int|null $offset
30944+
* @phpstan-return void
3093830945
*/
3093930946
#[\ReturnTypeWillChange]
3094030947
public function offsetSet($index, $value)
@@ -30948,6 +30955,8 @@ public function offsetSet($index, $value)
3094830955
* @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
3094930956
*
3095030957
* @param string $index Index of block value to unset.
30958+
* @phpstan-param int $offset
30959+
* @phpstan-return void
3095130960
*/
3095230961
#[\ReturnTypeWillChange]
3095330962
public function offsetUnset($index)
@@ -51815,6 +51824,7 @@ public function set_spacing_sizes()
5181551824
* @package WordPress
5181651825
* @subpackage Theme
5181751826
* @since 3.4.0
51827+
* @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'
5181851828
*/
5181951829
#[\AllowDynamicProperties]
5182051830
final class WP_Theme implements \ArrayAccess
@@ -51904,6 +51914,7 @@ public function offsetUnset($offset)
5190451914
*
5190551915
* @param mixed $offset
5190651916
* @return bool
51917+
* @phpstan-return ($offset is ThemeKey ? true : false)
5190751918
*/
5190851919
#[\ReturnTypeWillChange]
5190951920
public function offsetExists($offset)
@@ -51923,6 +51934,7 @@ public function offsetExists($offset)
5192351934
*
5192451935
* @param mixed $offset
5192551936
* @return mixed
51937+
* @phpstan-return ($offset is ThemeKey ? mixed : null)
5192651938
*/
5192751939
#[\ReturnTypeWillChange]
5192851940
public function offsetGet($offset)
@@ -62032,6 +62044,8 @@ public function merge_with(&$other)
6203262044
* @since 4.4.0
6203362045
*
6203462046
* @link https://www.php.net/manual/en/class.arrayaccess.php
62047+
* @phpstan-template T of array
62048+
* @phpstan-implements ArrayAccess<key-of<T>, value-of<T>>
6203562049
*/
6203662050
#[\AllowDynamicProperties]
6203762051
class WP_REST_Request implements \ArrayAccess
@@ -62565,6 +62579,8 @@ public function has_valid_params()
6256562579
*
6256662580
* @param string $offset Parameter name.
6256762581
* @return bool Whether the parameter is set.
62582+
* @phpstan-param @param key-of<T> $offset
62583+
* @phpstan-return bool
6256862584
*/
6256962585
#[\ReturnTypeWillChange]
6257062586
public function offsetExists($offset)
@@ -62577,6 +62593,9 @@ public function offsetExists($offset)
6257762593
*
6257862594
* @param string $offset Parameter name.
6257962595
* @return mixed|null Value if set, null otherwise.
62596+
* @phpstan-template TOffset of key-of<T>
62597+
* @phpstan-param TOffset $offset
62598+
* @phpstan-return T[TOffset]
6258062599
*/
6258162600
#[\ReturnTypeWillChange]
6258262601
public function offsetGet($offset)
@@ -62589,6 +62608,10 @@ public function offsetGet($offset)
6258962608
*
6259062609
* @param string $offset Parameter name.
6259162610
* @param mixed $value Parameter value.
62611+
* @phpstan-template TOffset of key-of<T>
62612+
* @phpstan-param TOffset $offset
62613+
* @phpstan-param T[TOffset] $value
62614+
* @phpstan-return void
6259262615
*/
6259362616
#[\ReturnTypeWillChange]
6259462617
public function offsetSet($offset, $value)
@@ -62600,6 +62623,9 @@ public function offsetSet($offset, $value)
6260062623
* @since 4.4.0
6260162624
*
6260262625
* @param string $offset Parameter name.
62626+
* @phpstan-template TOffset of key-of<T>
62627+
* @phpstan-param TOffset $offset
62628+
* @phpstan-return void
6260362629
*/
6260462630
#[\ReturnTypeWillChange]
6260562631
public function offsetUnset($offset)

0 commit comments

Comments
 (0)