From 291eb02851ddac3b2812a60b708f2135e15e51cb Mon Sep 17 00:00:00 2001 From: Jagepard Date: Fri, 9 Oct 2026 12:39:20 +0300 Subject: [PATCH] refactor: decouple ParamsExtractor via DI and use private delimiter properties --- src/Annotation.php | 25 +++++++------------------ src/ParamsExtractor.php | 8 ++++++-- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Annotation.php b/src/Annotation.php index cdc6361..a97c193 100755 --- a/src/Annotation.php +++ b/src/Annotation.php @@ -13,21 +13,10 @@ class Annotation implements AnnotationInterface { - /** - * Parameter separator - * - * in the line ',', example: key='param', key2='param2' - * in the array ';', example: {key:'param'; key2:'param2'} - */ - public const array DELIMITER = ['string' => ',', 'array' => ';']; - - /** - * Assignment mark - * - * in the line '=', example: key='param' - * in the array ':', example: {key:'param'} - */ - public const array ASSIGNMENT = ['string' => '=', 'array' => ':']; + private string $lineDelimiter = ','; + private string $lineAssignment = '='; + private string $arrayDelimiter = ';'; + private string $arrayAssignment = ':'; /** * Each parameter must be on its own line. @@ -86,13 +75,13 @@ private function parseAnnotations(string $docBlock): array */ if (preg_match_all('/@([A-Za-z0-9_-]+)\((.*?)\)/', $docBlock, $matches)) { $count = count($matches[0]); - $extractor = new ParamsExtractor(); + $extractor = new ParamsExtractor($this->arrayDelimiter, $this->arrayAssignment); // $annotations = ['Annotation' => [[0 => 'param1', 'param2' => 'param2', 'param3' => ['param1', 'param2' => 'param2']]]] for ($i = 0; $i < $count; $i++) { $annotations[$matches[1][$i]][] = $extractor->getParams( - str_getcsv(trim($matches[2][$i]), self::DELIMITER['string'], '"', ''), - self::ASSIGNMENT['string'] + str_getcsv(trim($matches[2][$i]), $this->lineDelimiter, '"', '\\'), + $this->lineAssignment ); } } diff --git a/src/ParamsExtractor.php b/src/ParamsExtractor.php index b122056..0570d00 100755 --- a/src/ParamsExtractor.php +++ b/src/ParamsExtractor.php @@ -13,6 +13,10 @@ class ParamsExtractor { + public function __construct(private string $delimiter, private string $assignment) + { + } + /** * Parses an array of parameter strings into an associative array * @@ -48,8 +52,8 @@ private function handleData(string $data, array $exploded): ?array if (preg_match('/=\s*{/', $data) && preg_match('/{(.*)}/', $exploded[1], $matches)) { return [ trim($exploded[0]) => $this->getParams( - explode(Annotation::DELIMITER['array'], trim($matches[1])), - Annotation::ASSIGNMENT['array'] + explode($this->delimiter, trim($matches[1])), + $this->assignment ), ]; }