Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/Util/ClassUtil/PicoAnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* The `PicoAnnotationParser` is particularly useful in frameworks or libraries
* that rely on annotations for configuration, routing, or metadata purposes.
*
*
* @author Kamshory
* @package MagicObject\Util\ClassUtil
* @link https://github.com/Planetbiru/MagicObject
Expand Down Expand Up @@ -348,8 +348,8 @@ public function getParameter($key)
* Get the first parameter for a given key from the parsed annotations.
*
* This method retrieves the first value associated with the specified key.
* If the parameter does not exist or is null, it returns null.
* If the parameter is an array, it returns the first string element.
* If the parameter does not exist or is null, it returns null.
* If the parameter is an array, it returns the first string element.
* Otherwise, it returns the value directly.
*
* @param string $key The key for which to retrieve the first parameter.
Expand All @@ -375,7 +375,7 @@ public function getFirstParameter($key)
/**
* Combine and merge two arrays, where the first array contains keys and the second contains values.
*
* This method checks if both arrays are set and are of the correct type.
* This method checks if both arrays are set and are of the correct type.
* It combines them into a new associative array and returns the merged result.
*
* @param array $matches An array of matched keys and values.
Expand All @@ -399,8 +399,8 @@ private function combineAndMerge($matches, $pair)
/**
* Parse key-value pairs from parameters string.
*
* This method extracts key-value pairs from parameters string, which may contain
* attributes with or without quotes. Numeric attributes will have an underscore
* This method extracts key-value pairs from parameters string, which may contain
* attributes with or without quotes. Numeric attributes will have an underscore
* prefix. Throws an exception if the input is invalid.
*
* @param string $parametersString The parameters string to parse.
Expand All @@ -419,8 +419,8 @@ public function parseKeyValue($parametersString)
}

// For every modification, please test regular expression with https://regex101.com/

// parse attributes with quotes

$pattern1 = '/([_\-\w+]+)\=\"([a-zA-Z0-9\-\+ _,.\(\)\{\}\`\~\!\@\#\$\%\^\*\\\|\<\>\[\]\/&%?=:;\'\t\r\n|\r|\n]+)\"/m'; // NOSONAR
preg_match_all($pattern1, $parametersString, $matches1);
$pair1 = array_combine($matches1[1], $matches1[2]);
Expand All @@ -432,8 +432,12 @@ public function parseKeyValue($parametersString)
$pair3 = $this->combineAndMerge($matches2, $pair1);

// parse attributes without any value
$pattern3 = '/([\w\=\-\_"]+)/m'; // NOSONAR
preg_match_all($pattern3, $parametersString, $matches3);
// 🔴 FIX: remove quoted values before parsing boolean attributes
$cleaned = preg_replace($pattern1, '', $parametersString);

// parse attributes without any value (boolean flags)
$pattern3 = '/\b([A-Za-z_][A-Za-z0-9_\-]*)\b/m'; // NOSONAR
preg_match_all($pattern3, $cleaned, $matches3);

$pair4 = array();
if(isset($matches3) && isset($matches3[0]) && is_array($matches3[0]))
Expand Down Expand Up @@ -474,13 +478,13 @@ private function matchArgs($keys, $val)
{
return stripos($val, '=') === false && stripos($val, '"') === false && stripos($val, "'") === false && !in_array($val, $keys);
}

/**
* Parse parameters from parameters string and return them as a PicoGenericObject.
*
* This method transforms the key-value pairs parsed from the parameters string
* into an instance of PicoGenericObject. All numeric attributes will be
* prefixed with an underscore.
* into an instance of PicoGenericObject. All numeric attributes will be
* prefixed with an underscore.
*
* @param string $parametersString The parameters string to parse.
* @return PicoGenericObject An object containing the parsed key-value pairs.
Expand Down