Skip to content

Commit

Permalink
Improve phpdoc magic method param parsing (quotes were left on strings)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Jun 3, 2024
1 parent 2434eb3 commit 5f936f9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
9 changes: 4 additions & 5 deletions src/Debug/Abstraction/Object/MethodParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,10 @@ private function getParamTypeHint(ReflectionParameter $refParameter, $phpDocType
*/
private function phpDocConstant($defaultValue, $className, array $matches)
{
if ($matches[1] && \defined($className . '::' . $matches[2])) {
// self
if ($matches['classname'] === 'self' && \defined($className . '::' . $matches['const'])) {
return new Abstraction(Type::TYPE_CONST, array(
'name' => $matches[0],
'value' => \constant($className . '::' . $matches[2]),
'name' => $defaultValue,
'value' => \constant($className . '::' . $matches['const']),
));
}
if (\defined($defaultValue)) {
Expand Down Expand Up @@ -299,7 +298,7 @@ private function phpDocParamValue(array $param, $className = null)
return array();
}
$matches = array();
if (\preg_match('/^(self::)?([^\(\)\[\]]+)$/i', $defaultValue, $matches)) {
if (\preg_match('/^(:?(?P<classname>\S+)::)?(?P<const>[^()\[\]\'"]+)$/i', $defaultValue, $matches)) {
// appears to be a constant
return $this->phpDocConstant($defaultValue, $className, $matches);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Debug/Fixture/ConfusableIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function baꜱe𐊗hing()
* CƖass <b onhover="alert('xss')">[𐑈]um</b>mary.
* CƖass <b onhover="alert('xss')">[𝖽]esc</b>ription
*
* @method bool mаgicMethod(string $𝕡аram = 'vаlսe') T[е]st <b onhover="alert('xss')">method</b>
* @method bool mаgicMethod(string $𝕡аram = 'vаlսe', integer $int = 1, $bool = true, $null = null, $arr = array(), $const=self::ᖴOO) T[е]st <b onhover="alert('xss')">method</b>
*
* @see http://ᴜrl.com <b onhover="alert('xss')">Super</b> [Η]elpful
* @link http://ᴜrl.com [Ⅼ]ink <b onhover="alert('xss')">Rot</b>
Expand Down
3 changes: 2 additions & 1 deletion tests/Debug/Type/StringEncodedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ public static function providerTestMethod()
$json = \file_get_contents(TEST_DIR . '/../composer.json');
$jsonPrettified = Debug::getInstance()->stringUtil->prettyJson($json);
self::assertSame(\strlen($jsonPrettified), $entry['args'][0]['strlen']);
self::assertSame(\substr($jsonPrettified, 0, 123), $entry['args'][0]['value']);
$expect = \substr($jsonPrettified, 0, 123);
self::assertSame($expect, $entry['args'][0]['value']);
},
'html' => static function ($html) {
$json = \file_get_contents(TEST_DIR . '/../composer.json');
Expand Down
62 changes: 61 additions & 1 deletion tests/Debug/data/ConfusableIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
'params' => array(
array(
'attributes' => array(),
'defaultValue' => '\'vаlսe\'',
'defaultValue' => 'vаlսe',
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
Expand All @@ -157,6 +157,66 @@
'name' => '𝕡аram',
'type' => Type::TYPE_STRING,
),
array(
'attributes' => array(),
'defaultValue' => 1,
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
'isPromoted' => false,
'isVariadic' => false,
'name' => 'int',
'type' => Type::TYPE_INT,
),
array(
'attributes' => array(),
'defaultValue' => true,
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
'isPromoted' => false,
'isVariadic' => false,
'name' => 'bool',
'type' => null,
),
array(
'attributes' => array(),
'defaultValue' => null,
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
'isPromoted' => false,
'isVariadic' => false,
'name' => 'null',
'type' => null,
),
array(
'attributes' => array(),
'defaultValue' => array(),
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
'isPromoted' => false,
'isVariadic' => false,
'name' => 'arr',
'type' => null,
),
array(
'attributes' => array(),
'defaultValue' => array(
'debug' => Abstracter::ABSTRACTION,
'name' => 'self::ᖴOO',
'type' => Type::TYPE_CONST,
'value' => 'fσo',
),
'desc' => null,
'isOptional' => false,
'isPassedByReference' => false,
'isPromoted' => false,
'isVariadic' => false,
'name' => 'const',
'type' => null,
),
),
'phpDoc' => array(
'desc' => null,
Expand Down
Loading

0 comments on commit 5f936f9

Please sign in to comment.