Skip to content

Commit

Permalink
Fix checking the docblock type for a parameter, where a previous para…
Browse files Browse the repository at this point in the history
…meter starts with the same string (#60)
  • Loading branch information
bram123 authored Aug 7, 2024
1 parent 1b488d2 commit f967dfc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Constraint/Typehint/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public function getParamTypehint(string $parameterName, string $docComment): ?st
return null;
}

preg_match('/\*\s*@(?:phpstan|psalm)-param\s+(.*?)\s*(?:\.\.\.)?' . preg_quote('$' . $parameterName, '/') . '/i', $docComment, $matches);
preg_match('/\*\s*@(?:phpstan|psalm)-param\s+(.*?)\s*(?:\.\.\.)?' . preg_quote('$' . $parameterName, '/') . '\W/i', $docComment, $matches);
if (isset($matches[1])) {
return $this->normalizeDocblock((string)$matches[1]);
}

preg_match('/\*\s*@param\s+(.*?)\s*(?:\.\.\.)?' . preg_quote('$' . $parameterName, '/') . '/i', $docComment, $matches);
preg_match('/\*\s*@param\s+(.*?)\s*(?:\.\.\.)?' . preg_quote('$' . $parameterName, '/') . '\W/i', $docComment, $matches);
if (isset($matches[1])) {
return $this->normalizeDocblock((string)$matches[1]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\success\Regular\Constructor;

class MultiParamNameOverlap
{
/** @var string */
private $item;

/** @var string[] */
private $itemList;

/**
* @param string[] $itemList
*/
public function __construct(string $item, array $itemList)
{
$this->item = $item;
$this->itemList = $itemList;
}

/**
* @return string
*/
public function getItem(): string
{
return $this->item;
}

/**
* @return string[]
*/
public function getItemList(): array
{
return $this->itemList;
}
}
3 changes: 3 additions & 0 deletions tests/Unit/Constraint/Typehint/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public static function paramTypehintProvider(): Generator
yield ['/** @param array$param */', 'array'];
yield ['/**@param array$param*/', 'array'];
yield ["/**\n *@param array\$param\n */", 'array'];

// Don't match parameters where the name is a substring of another parameter
yield ['/** @param string $paramList */', null];
}

/**
Expand Down

0 comments on commit f967dfc

Please sign in to comment.