Skip to content

Commit b615084

Browse files
authored
Merge pull request #180 from Planetbiru/feature/version-3.21.3
Bug Fix Annotation Parser
2 parents 03a20a9 + da40e6c commit b615084

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

src/Util/ClassUtil/PicoAnnotationParser.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* The `PicoAnnotationParser` is particularly useful in frameworks or libraries
2424
* that rely on annotations for configuration, routing, or metadata purposes.
25-
*
25+
*
2626
* @author Kamshory
2727
* @package MagicObject\Util\ClassUtil
2828
* @link https://github.com/Planetbiru/MagicObject
@@ -348,8 +348,8 @@ public function getParameter($key)
348348
* Get the first parameter for a given key from the parsed annotations.
349349
*
350350
* This method retrieves the first value associated with the specified key.
351-
* If the parameter does not exist or is null, it returns null.
352-
* If the parameter is an array, it returns the first string element.
351+
* If the parameter does not exist or is null, it returns null.
352+
* If the parameter is an array, it returns the first string element.
353353
* Otherwise, it returns the value directly.
354354
*
355355
* @param string $key The key for which to retrieve the first parameter.
@@ -375,7 +375,7 @@ public function getFirstParameter($key)
375375
/**
376376
* Combine and merge two arrays, where the first array contains keys and the second contains values.
377377
*
378-
* This method checks if both arrays are set and are of the correct type.
378+
* This method checks if both arrays are set and are of the correct type.
379379
* It combines them into a new associative array and returns the merged result.
380380
*
381381
* @param array $matches An array of matched keys and values.
@@ -399,8 +399,8 @@ private function combineAndMerge($matches, $pair)
399399
/**
400400
* Parse key-value pairs from parameters string.
401401
*
402-
* This method extracts key-value pairs from parameters string, which may contain
403-
* attributes with or without quotes. Numeric attributes will have an underscore
402+
* This method extracts key-value pairs from parameters string, which may contain
403+
* attributes with or without quotes. Numeric attributes will have an underscore
404404
* prefix. Throws an exception if the input is invalid.
405405
*
406406
* @param string $parametersString The parameters string to parse.
@@ -419,8 +419,8 @@ public function parseKeyValue($parametersString)
419419
}
420420

421421
// For every modification, please test regular expression with https://regex101.com/
422-
423422
// parse attributes with quotes
423+
424424
$pattern1 = '/([_\-\w+]+)\=\"([a-zA-Z0-9\-\+ _,.\(\)\{\}\`\~\!\@\#\$\%\^\*\\\|\<\>\[\]\/&%?=:;\'\t\r\n|\r|\n]+)\"/m'; // NOSONAR
425425
preg_match_all($pattern1, $parametersString, $matches1);
426426
$pair1 = array_combine($matches1[1], $matches1[2]);
@@ -432,8 +432,12 @@ public function parseKeyValue($parametersString)
432432
$pair3 = $this->combineAndMerge($matches2, $pair1);
433433

434434
// parse attributes without any value
435-
$pattern3 = '/([\w\=\-\_"]+)/m'; // NOSONAR
436-
preg_match_all($pattern3, $parametersString, $matches3);
435+
// 🔴 FIX: remove quoted values before parsing boolean attributes
436+
$cleaned = preg_replace($pattern1, '', $parametersString);
437+
438+
// parse attributes without any value (boolean flags)
439+
$pattern3 = '/\b([A-Za-z_][A-Za-z0-9_\-]*)\b/m'; // NOSONAR
440+
preg_match_all($pattern3, $cleaned, $matches3);
437441

438442
$pair4 = array();
439443
if(isset($matches3) && isset($matches3[0]) && is_array($matches3[0]))
@@ -474,13 +478,13 @@ private function matchArgs($keys, $val)
474478
{
475479
return stripos($val, '=') === false && stripos($val, '"') === false && stripos($val, "'") === false && !in_array($val, $keys);
476480
}
477-
481+
478482
/**
479483
* Parse parameters from parameters string and return them as a PicoGenericObject.
480484
*
481485
* This method transforms the key-value pairs parsed from the parameters string
482-
* into an instance of PicoGenericObject. All numeric attributes will be
483-
* prefixed with an underscore.
486+
* into an instance of PicoGenericObject. All numeric attributes will be
487+
* prefixed with an underscore.
484488
*
485489
* @param string $parametersString The parameters string to parse.
486490
* @return PicoGenericObject An object containing the parsed key-value pairs.

0 commit comments

Comments
 (0)