diff --git a/src/Annotation.php b/src/Annotation.php index 106aaf8..cdc6361 100755 --- a/src/Annotation.php +++ b/src/Annotation.php @@ -19,7 +19,7 @@ class Annotation implements AnnotationInterface * in the line ',', example: key='param', key2='param2' * in the array ';', example: {key:'param'; key2:'param2'} */ - public const array DELIMITER = ["string" => ',', "array" => ';']; + public const array DELIMITER = ['string' => ',', 'array' => ';']; /** * Assignment mark @@ -27,7 +27,7 @@ class Annotation implements AnnotationInterface * in the line '=', example: key='param' * in the array ':', example: {key:'param'} */ - public const array ASSIGNMENT = ["string" => '=', "array" => ':']; + public const array ASSIGNMENT = ['string' => '=', 'array' => ':']; /** * Each parameter must be on its own line. @@ -84,15 +84,15 @@ private function parseAnnotations(string $docBlock): array * $matches[1][0] - Annotation * $matches[2][0] - param1, param2 = 'param2', param3={param1;param2:'param2'} */ - if (preg_match_all("/@([A-Za-z0-9_-]+)\((.*?)\)/", $docBlock, $matches)) { + if (preg_match_all('/@([A-Za-z0-9_-]+)\((.*?)\)/', $docBlock, $matches)) { $count = count($matches[0]); $extractor = new ParamsExtractor(); - // $annotations = ["Annotation" => [[0 => "param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]]] + // $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]), self::DELIMITER['string'], '"', ''), + self::ASSIGNMENT['string'] ); } } diff --git a/src/ParamsExtractor.php b/src/ParamsExtractor.php index a3b1dd2..b122056 100755 --- a/src/ParamsExtractor.php +++ b/src/ParamsExtractor.php @@ -16,8 +16,8 @@ class ParamsExtractor /** * Parses an array of parameter strings into an associative array * - * `from: "param1, param2 = 'param2', param3={param1;param2:'param2'}"` - * `to: ["param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]` + * `from: 'param1, param2 = 'param2', param3={param1;param2:'param2'}'` + * `to: ['param1', 'param2' => 'param2', 'param3' => ['param1', 'param2' => 'param2']]` */ public function getParams(array $exploded, string $assignment): array { @@ -45,11 +45,11 @@ public function getParams(array $exploded, string $assignment): array private function handleData(string $data, array $exploded): ?array { // If in data an array of type param3={param1;param2:'param2'} - if (preg_match("/=\s*{/", $data) && preg_match("/{(.*)}/", $exploded[1], $matches)) { + 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(Annotation::DELIMITER['array'], trim($matches[1])), + Annotation::ASSIGNMENT['array'] ), ]; } diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index a377d90..3169b64 100755 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -29,41 +29,41 @@ class AnnotationTest extends \PHPUnit\Framework\TestCase */ "; private array $result = [ - "Routing" => [["url" => ""]], - "Defaults" => [ + 'Routing' => [['url' => '']], + 'Defaults' => [ [ - "name" => "user1", - "lastname" => "sample", - "age" => "0", - "address" => [ - "country" => "Russia", - "state" => "Tambov", + 'name' => 'user1', + 'lastname' => 'sample', + 'age' => '0', + 'address' => [ + 'country' => 'Russia', + 'state' => 'Tambov', ], - "phone" => "000-00000000", + 'phone' => '000-00000000', ], ], - "assertResult" => [["false"]], - "Validate" => [ + 'assertResult' => [['false']], + 'Validate' => [ [ - "name" => "min:150", - "phone" => "max:9", + 'name' => 'min:150', + 'phone' => 'max:9', ], ], - "Middleware" => [ + 'Middleware' => [ [ "'Middleware'", - "params" => [ - "int1" => "123", + 'params' => [ + 'int1' => '123', ], ], ], - "Annotation" => [ + 'Annotation' => [ [ - "param1", - "param2" => "param2", - "param3" => [ - "param1", - "param2" => "param2", + 'param1', + 'param2' => 'param2', + 'param3' => [ + 'param1', + 'param2' => 'param2', ], ], ], @@ -85,7 +85,7 @@ private function getMethod(string $name): \ReflectionMethod public function testParseAnnotations(): void { - $parseAnnotations = $this->getMethod("parseAnnotations"); + $parseAnnotations = $this->getMethod('parseAnnotations'); $this->assertEquals($this->result, $parseAnnotations->invokeArgs($this->annotation, [$this->docBlock])); } @@ -96,7 +96,7 @@ public function testGetClassAnnotations(): void public function testGetMethodAnnotations(): void { - $this->assertEquals($this->result, $this->annotation->getAnnotations(PageController::class, "indexAction")); + $this->assertEquals($this->result, $this->annotation->getAnnotations(PageController::class, 'indexAction')); } public function testGetClassAttributes(): void @@ -106,12 +106,12 @@ public function testGetClassAttributes(): void public function testGetMethodAttributes(): void { - $this->assertEquals($this->result, $this->annotation->getAttributes(PageController::class, "secondAction")); + $this->assertEquals($this->result, $this->annotation->getAttributes(PageController::class, 'secondAction')); } public function testGetMethodWithoutAnnotations(): void { - $this->assertEquals([], $this->annotation->getAnnotations(PageController::class, "withoutDocblock")); + $this->assertEquals([], $this->annotation->getAnnotations(PageController::class, 'withoutDocblock')); } public function testParseAnnotationsWithEqualsInValue(): void @@ -124,20 +124,20 @@ public function testParseAnnotationsWithEqualsInValue(): void "; $expected = [ - "Routing" => [ + 'Routing' => [ [ - "url" => "site/com?a=1&b=2", - "method" => "GET", + 'url' => 'site/com?a=1&b=2', + 'method' => 'GET', ], ], - "Validate" => [ + 'Validate' => [ [ - "pattern" => "^[a-z]+=[0-9]+$", + 'pattern' => '^[a-z]+=[0-9]+$', ], ], ]; - $parseAnnotations = $this->getMethod("parseAnnotations"); + $parseAnnotations = $this->getMethod('parseAnnotations'); $this->assertEquals($expected, $parseAnnotations->invokeArgs($this->annotation, [$docBlock])); } } diff --git a/tests/Stub/PageController.php b/tests/Stub/PageController.php index ba2448a..2c99aa8 100755 --- a/tests/Stub/PageController.php +++ b/tests/Stub/PageController.php @@ -24,7 +24,7 @@ #[assertResult('false')] #[Validate(name:'min:150', phone:'max:9')] #[Middleware("'Middleware'", params:['int1' => '123'])] -#[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])] +#[Annotation('param1', param2:'param2', param3:['param1', 'param2' => 'param2'])] class PageController { /** @@ -44,7 +44,7 @@ public function indexAction() #[assertResult('false')] #[Validate(name:'min:150', phone:'max:9')] #[Middleware("'Middleware'", params:['int1' => '123'])] - #[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])] + #[Annotation('param1', param2:'param2', param3:['param1', 'param2' => 'param2'])] public function secondAction() { }