Skip to content

Commit

Permalink
Add annotations for classes extending ArrayAccess (#66)
Browse files Browse the repository at this point in the history
* Add annotations for classes extending `ArrayAccess`

* Update `getNodeName()`

* Update `postProcessNode()`

* Regenerate stubs

* name can be null

* Update map

* Update null check

* Stay positive
  • Loading branch information
swissspidy committed Mar 6, 2023
1 parent db1e23a commit ac0c474
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
17 changes: 17 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*
* '<function_name>' => ['<return_type>', '<arg_name>'=>'<arg_type>']
*
* For classes:
*
* '<class_name>' => [null, '<arg_name>'=>'<arg_type>']
*
* @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php
*/
return [
Expand Down Expand Up @@ -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<key-of<T>, value-of<T>>'],
'WP_REST_Request::offsetExists' => ['bool', 'offset'=>'@param key-of<T>'],
'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template'=>'TOffset of key-of<T>', 'offset'=>'TOffset'],
'WP_REST_Request::offsetSet' => ['void', '@phpstan-template'=>'TOffset of key-of<T>', 'offset'=>'TOffset', 'value'=>'T[TOffset]'],
'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template'=>'TOffset of key-of<T>', '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<int, WP_Block>'],
'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'],
];
18 changes: 11 additions & 7 deletions visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30855,6 +30855,7 @@ public function __construct(array $settings = array())
* Class representing a list of block instances.
*
* @since 5.5.0
* @phpstan-implements ArrayAccess<int, WP_Block>
*/
#[\AllowDynamicProperties]
class WP_Block_List implements \Iterator, \ArrayAccess, \Countable
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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<key-of<T>, value-of<T>>
*/
#[\AllowDynamicProperties]
class WP_REST_Request implements \ArrayAccess
Expand Down Expand Up @@ -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<T> $offset
* @phpstan-return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
Expand All @@ -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<T>
* @phpstan-param TOffset $offset
* @phpstan-return T[TOffset]
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
Expand All @@ -62589,6 +62608,10 @@ public function offsetGet($offset)
*
* @param string $offset Parameter name.
* @param mixed $value Parameter value.
* @phpstan-template TOffset of key-of<T>
* @phpstan-param TOffset $offset
* @phpstan-param T[TOffset] $value
* @phpstan-return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
Expand All @@ -62600,6 +62623,9 @@ public function offsetSet($offset, $value)
* @since 4.4.0
*
* @param string $offset Parameter name.
* @phpstan-template TOffset of key-of<T>
* @phpstan-param TOffset $offset
* @phpstan-return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
Expand Down

0 comments on commit ac0c474

Please sign in to comment.