Skip to content
Permalink
Browse files

Add support for simple default arrays in @method docblocks

Fixes #2455
  • Loading branch information
muglug committed Dec 11, 2019
1 parent 79f3a5d commit e564d67214bfb3d59e2c779687e50eb8bc74c010
Showing with 37 additions and 0 deletions.
  1. +5 −0 src/Psalm/Internal/Analyzer/CommentAnalyzer.php
  2. +32 −0 tests/MagicMethodAnnotationTest.php
@@ -908,6 +908,8 @@ public static function extractClassLikeDocblockInfo(

$method_entry = trim(preg_replace('/\/\/.*/', '', $method_entry));

$method_entry = preg_replace('/array\(([0-9a-zA-Z_\'\" ]+,)*([0-9a-zA-Z_\'\" ]+)\)/', '[]', $method_entry);

$end_of_method_regex = '/(?<!array\()\) ?(\: ?(\??[\\\\a-zA-Z0-9_]+))?/';

if (preg_match($end_of_method_regex, $method_entry, $matches, PREG_OFFSET_CAPTURE)) {
@@ -917,6 +919,9 @@ public static function extractClassLikeDocblockInfo(
$method_entry = str_replace([', ', '( '], [',', '('], $method_entry);
$method_entry = preg_replace('/ (?!(\$|\.\.\.|&))/', '', trim($method_entry));

// replace array bracket contents
$method_entry = preg_replace('/\[([0-9a-zA-Z_\'\" ]+,)*([0-9a-zA-Z_\'\" ]+)\]/', '[]', $method_entry);

try {
$method_tree = ParseTree::createFromTokens(
Type::fixUpLocalType(
@@ -217,6 +217,38 @@ class Child extends ParentClass {}
$child->setArray(["boo"]);
$child->setArray(["boo"], 8);',
],
'validAnnotationWithNonEmptyDefaultArray' => [
'<?php
class ParentClass {
public function __call(string $name, array $args) {}
}
/**
* @method void setArray(array $arr = [1, 2, 3]) with some more text
*/
class Child extends ParentClass {}
$child = new Child();
$child->setArray(["boo"]);
$child->setArray(["boo"]);',
],
'validAnnotationWithNonEmptyDefaultOldStyleArray' => [
'<?php
class ParentClass {
public function __call(string $name, array $args) {}
}
/**
* @method void setArray(array $arr = array(1, 2, 3)) with some more text
*/
class Child extends ParentClass {}
$child = new Child();
$child->setArray(["boo"]);
$child->setArray(["boo"]);',
],
'validStaticAnnotationWithDefault' => [
'<?php
class ParentClass {

0 comments on commit e564d67

Please sign in to comment.
You can’t perform that action at this time.